UNPKG

1.3 kBJavaScriptView Raw
1// YAML error class. http://stackoverflow.com/questions/8458984
2//
3'use strict';
4
5
6function formatError(exception, compact) {
7 var where = '', message = exception.reason || '(unknown reason)';
8
9 if (!exception.mark) return message;
10
11 if (exception.mark.name) {
12 where += 'in "' + exception.mark.name + '" ';
13 }
14
15 where += '(' + (exception.mark.line + 1) + ':' + (exception.mark.column + 1) + ')';
16
17 if (!compact && exception.mark.snippet) {
18 where += '\n\n' + exception.mark.snippet;
19 }
20
21 return message + ' ' + where;
22}
23
24
25function YAMLException(reason, mark) {
26 // Super constructor
27 Error.call(this);
28
29 this.name = 'YAMLException';
30 this.reason = reason;
31 this.mark = mark;
32 this.message = formatError(this, false);
33
34 // Include stack trace in error object
35 if (Error.captureStackTrace) {
36 // Chrome and NodeJS
37 Error.captureStackTrace(this, this.constructor);
38 } else {
39 // FF, IE 10+ and Safari 6+. Fallback for others
40 this.stack = (new Error()).stack || '';
41 }
42}
43
44
45// Inherit from Error
46YAMLException.prototype = Object.create(Error.prototype);
47YAMLException.prototype.constructor = YAMLException;
48
49
50YAMLException.prototype.toString = function toString(compact) {
51 return this.name + ': ' + formatError(this, compact);
52};
53
54
55module.exports = YAMLException;