Permalink
Browse files

if PUT db fails due to permissions when replicating from url, try a H…

…EAD request instead to see if db already exists
  • Loading branch information...
1 parent 7a4ba8b commit cdd1395efd3fcbb4dc4ecd5852dce74b53d2d65e @caolan committed Apr 10, 2012
Showing with 17 additions and 2 deletions.
  1. +17 −2 src/pouch.js
View
19 src/pouch.js
@@ -454,8 +454,23 @@ parseUri.options = {
};
- ajax({auth: host.auth, type: 'PUT', url: genUrl(host, '')}, function(err, ret) {
- if (!err || err.status === 412) {
+ var db_url = genUrl(host, '');
+ ajax({auth: host.auth, type: 'PUT', url: db_url}, function(err, ret) {
+ // the user may not have permission to PUT to a db url
+ if (err && err.status === 401) {
+ // test if the db already exists
+ ajax({auth: host.auth, type: 'HEAD', url: db_url}, function (err, ret) {
+ if (err) {
+ // still can't access db
+ call(callback, err);
+ }
+ else {
+ // continue
+ call(callback, null, db);
+ }
+ });
+ }
+ else if (!err || err.status === 412) {
call(callback, null, db);
}
});

0 comments on commit cdd1395

Please sign in to comment.