Skip to content

Commit

Permalink
Don't throw exceptions on warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Jul 25, 2015
1 parent 7eb60e9 commit 1b8173b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
6 changes: 1 addition & 5 deletions lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,8 @@ function throwError(state, message) {
}

function throwWarning(state, message) {
var error = generateError(state, message);

if (state.onWarning) {
state.onWarning.call(null, error);
} else {
throw error;
state.onWarning.call(null, generateError(state, message));
}
}

Expand Down
10 changes: 9 additions & 1 deletion test/11-load-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ suite('Load errors', function () {
var yamlSource = fs.readFileSync(yamlFile, { encoding: 'utf8' });

assert.throws(function () {
yaml.loadAll(yamlSource, function () {}, { filename: yamlFile, schema: TEST_SCHEMA });
yaml.loadAll(
yamlSource,
function () {},
{
filename: yamlFile,
schema: TEST_SCHEMA,
onWarning: function (e) { throw e; }
}
);
}, yaml.YAMLException, yamlFile);
});
});
Expand Down
21 changes: 21 additions & 0 deletions test/issues/0194.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';


var assert = require('assert');
var yaml = require('../../');
var readFileSync = require('fs').readFileSync;


test('Don\'t throw on warning', function () {
var src = readFileSync(require('path').join(__dirname, '/0194.yml'), 'utf8'),
warnings = [],
data;

data = yaml.safeLoad(src);

assert.deepEqual(data, { foo: { bar: true } });

yaml.safeLoad(src, { onWarning: function (e) { warnings.push(e); } });

assert.equal(warnings.length, 1);
});
3 changes: 3 additions & 0 deletions test/issues/0194.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo: {
bar: true
}

0 comments on commit 1b8173b

Please sign in to comment.