diff --git a/lib/js-yaml/exception.js b/lib/js-yaml/exception.js index cf4e6252..b744a1ee 100644 --- a/lib/js-yaml/exception.js +++ b/lib/js-yaml/exception.js @@ -6,6 +6,11 @@ function YAMLException(reason, mark) { // Super constructor Error.call(this); + this.name = 'YAMLException'; + this.reason = reason; + this.mark = mark; + this.message = (this.reason || '(unknown reason)') + (this.mark ? ' ' + this.mark.toString() : ''); + // Include stack trace in error object if (Error.captureStackTrace) { // Chrome and NodeJS @@ -14,11 +19,6 @@ function YAMLException(reason, mark) { // FF, IE 10+ and Safari 6+. Fallback for others this.stack = (new Error()).stack || ''; } - - this.name = 'YAMLException'; - this.reason = reason; - this.mark = mark; - this.message = (this.reason || '(unknown reason)') + (this.mark ? ' ' + this.mark.toString() : ''); } diff --git a/test/issues/0351.js b/test/issues/0351.js new file mode 100644 index 00000000..58fc65a9 --- /dev/null +++ b/test/issues/0351.js @@ -0,0 +1,17 @@ +'use strict'; + + +var assert = require('assert'); +var yaml = require('../..'); +var readFileSync = require('fs').readFileSync; + + +test('should include the error message in the error stack', function () { + try { + yaml.safeLoad(readFileSync(require('path').join(__dirname, '/0351.yml'), 'utf8')); + } catch (err) { + assert(err.stack.startsWith('YAMLException: end of the stream or a document separator is expected')); + return; + } + assert.fail(null, null, 'Expected an error to be thrown'); +}); diff --git a/test/issues/0351.yml b/test/issues/0351.yml new file mode 100644 index 00000000..8ab27b9a --- /dev/null +++ b/test/issues/0351.yml @@ -0,0 +1,4 @@ +# intentionally invalid yaml + + foo: bar +baz: qux