Skip to content

Commit

Permalink
Added additional debugging and error checks, bump version (0.1.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamonOehlman committed Dec 19, 2011
1 parent 540c332 commit e877d3e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/writer.js
Expand Up @@ -40,6 +40,7 @@ AttachmentWriter.prototype.end = function() {
debug('stream ended, uploading attachment');

// get the current version of the document from couch
debug('requesting current doc: ' + this.path);
request(this.path, function(err, res, body) {
if (! err) {
var currentDoc = {};
Expand All @@ -49,11 +50,16 @@ AttachmentWriter.prototype.end = function() {

// if the current doc has an error, then reset the current doc
if (currentDoc.error) {
debug('got error requesting current doc: ' + currentDoc.error);
currentDoc = {};
}
}
catch (e) {
// unable to parse, stick with the empty toc
writer.emit('error', 'Cannot parse existing doc');
}

if (currentDoc._rev) {
debug(writer.path + ' retrieved, current revision = ' + currentDoc._rev);
}

// ensure the current doc has an attachments member
Expand All @@ -73,7 +79,13 @@ AttachmentWriter.prototype.end = function() {
}

// if the current document does not have an id, then give it one
debug('putting document update @ ' + writer.path);
request({ url: writer.path, method: 'PUT', json: currentDoc }, function(err, res, body) {
// check if we have received a body error from the response
if (!err && typeof body == 'object' && body.error) {
err = body.error;
}

if (! err) {
debug('uploaded doc ' + (typeof body == 'object' ? body.id + '/' + body.rev : ''));
writer.emit('end');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -5,7 +5,7 @@
"couchdb"
],
"author": "Damon Oehlman <damon.oehlman@sidelab.com>",
"version": "0.0.5",
"version": "0.1.0",
"engines": {
"node": ">= 0.4.x < 0.7.0"
},
Expand Down

0 comments on commit e877d3e

Please sign in to comment.