diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-08-28 20:03:00 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-08-28 20:03:00 +0100 |
| commit | d333cee1c7a7f95cadf5b59e8dbb0124d73852df (patch) | |
| tree | f837b57e640eb9bad909416c6822f6edb9d192ac | |
| parent | b1ae23f544c479d2b71347bbf67df7baac40d78f (diff) | |
Add test explicitly for image error handling
| -rw-r--r-- | lib/document-to-html.js | 4 | ||||
| -rw-r--r-- | lib/promises.js | 1 | ||||
| -rw-r--r-- | test/document-to-html.tests.js | 22 |
3 files changed, 26 insertions, 1 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index e022946..6094803 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -193,7 +193,9 @@ function DocumentConversion(options, comments) { function recoveringConvertImage(convertImage) { return function(image, messages) { - return convertImage(image, messages).caught(function(error) { + return promises.attempt(function() { + return convertImage(image, messages); + }).caught(function(error) { messages.push(results.error(error)); return []; }); diff --git a/lib/promises.js b/lib/promises.js index 24dc34a..010631f 100644 --- a/lib/promises.js +++ b/lib/promises.js @@ -9,6 +9,7 @@ exports.props = bluebird.props; exports.reject = bluebird.reject; exports.promisify = bluebird.promisify; exports.mapSeries = bluebird.mapSeries; +exports.attempt = bluebird.attempt; exports.nfcall = function(func) { var args = Array.prototype.slice.call(arguments, 1); diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js index cc8aff5..59eba49 100644 --- a/test/document-to-html.tests.js +++ b/test/document-to-html.tests.js @@ -590,6 +590,28 @@ test('can add custom handler for images', function() { }); }); +test('when custom image handler throws error then error is stored in error message', function() { + var error = new Error("Failed to convert image"); + var image = new documents.Image({ + readImage: function(encoding) { + return promises.when(new Buffer().toString(encoding)); + }, + contentType: "image/png" + }); + var converter = new DocumentConverter({ + convertImage: function(element, messages) { + throw error; + } + }); + return converter.convertToHtml(image).then(function(result) { + assert.equal(result.value, ''); + assert.equal(result.messages.length, 1); + var message = result.messages[0]; + assert.equal("error", message.type); + assert.equal("Failed to convert image", message.message); + }); +}); + test('long documents do not cause stack overflow', function() { var paragraphs = []; for (var i = 0; i < 1000; i++) { |
