1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 |
|
6 | function 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 |
|
25 | function YAMLException(reason, mark) {
|
26 |
|
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 |
|
35 | if (Error.captureStackTrace) {
|
36 |
|
37 | Error.captureStackTrace(this, this.constructor);
|
38 | } else {
|
39 |
|
40 | this.stack = (new Error()).stack || '';
|
41 | }
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | YAMLException.prototype = Object.create(Error.prototype);
|
47 | YAMLException.prototype.constructor = YAMLException;
|
48 |
|
49 |
|
50 | YAMLException.prototype.toString = function toString(compact) {
|
51 | return this.name + ': ' + formatError(this, compact);
|
52 | };
|
53 |
|
54 |
|
55 | module.exports = YAMLException;
|