Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix async file operations that error.

  • Loading branch information...
commit 808773757c020b401d788c0153be222d75c7c5f7 1 parent cfb21b1
@stephank authored
Showing with 26 additions and 7 deletions.
  1. +24 −3 index.js
  2. +2 −4 tests/badinput.js
View
27 index.js
@@ -312,8 +312,21 @@ YAML.readFile = function(filename, tagHandlers, callback) {
}
fs.readFile(filename, 'utf-8', function(err, data) {
- if (err) callback(err, null);
- else callback(null, YAML.parse(data, tagHandlers));
+ if (err) {
+ callback(err, null);
+ return;
+ }
+
+ var documents;
+ try {
+ documents = YAML.parse(data, tagHandlers);
+ }
+ catch (err) {
+ callback(err, null);
+ return;
+ }
+
+ callback(null, documents);
});
};
@@ -409,7 +422,15 @@ YAML.writeFile = function(filename) {
if (numDocuments !== 0 && typeof documents[numDocuments - 1] === 'function')
callback = documents.pop();
- var data = YAML.stringify.apply(this, documents);
+ var data;
+ try {
+ data = YAML.stringify.apply(this, documents);
+ }
+ catch (err) {
+ callback(err, null);
+ return;
+ }
+
fs.writeFile(filename, data, callback);
};
View
6 tests/badinput.js
@@ -17,10 +17,8 @@ test('bad parser input async', function(t) {
t.plan(1);
YAML.readFile(testutil.inputPath('badinput'), function(err) {
- t.similar(err, {
- name: "Error",
- message: "did not find expected key, while parsing a block mapping, on line 2"
- });
+ t.equal(err.message,
+ "did not find expected key, while parsing a block mapping, on line 2");
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.