Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure stack is present for custom errors (fixes #351) #360

Merged
merged 1 commit into from Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/js-yaml/exception.js
Expand Up @@ -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
Expand All @@ -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() : '');
}


Expand Down
17 changes: 17 additions & 0 deletions 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');
});
4 changes: 4 additions & 0 deletions test/issues/0351.yml
@@ -0,0 +1,4 @@
# intentionally invalid yaml

foo: bar
baz: qux