|
3 | 3 | const { ono } = require("ono"); |
4 | 4 | const url = require("./util/url"); |
5 | 5 | const plugins = require("./util/plugins"); |
6 | | -const { StoplightParserError, ResolverError, ParserError } = require("./util/errors"); |
| 6 | +const { StoplightParserError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors"); |
7 | 7 |
|
8 | 8 | module.exports = parse; |
9 | 9 |
|
@@ -42,14 +42,12 @@ async function parse (path, $refs, options) { |
42 | 42 |
|
43 | 43 | return parser.result; |
44 | 44 | } |
45 | | - catch (ex) { |
46 | | - if (!("error" in ex)) { |
47 | | - throw ex; |
48 | | - } |
49 | | - else { |
50 | | - $ref.value = ex.error; |
51 | | - throw ex.error; |
| 45 | + catch (err) { |
| 46 | + if (isHandledError(err)) { |
| 47 | + $ref.value = err; |
52 | 48 | } |
| 49 | + |
| 50 | + throw err; |
53 | 51 | } |
54 | 52 | } |
55 | 53 |
|
@@ -78,17 +76,20 @@ function readFile (file, options, $refs) { |
78 | 76 | .then(resolve, onError); |
79 | 77 |
|
80 | 78 | function onError (err) { |
81 | | - // Throw the original error, if it's one of our own (user-friendly) errors. |
82 | | - // Otherwise, throw a generic, friendly error. |
83 | | - if (!err || !(err instanceof SyntaxError)) { |
| 79 | + if (!err && !options.failFast) { |
| 80 | + // No resolver could be matched |
| 81 | + reject(new UnmatchedResolverError(file.url)); |
| 82 | + } |
| 83 | + else if (!err || !("error" in err)) { |
| 84 | + // Throw a generic, friendly error. |
84 | 85 | reject(ono.syntax(`Unable to resolve $ref pointer "${file.url}"`)); |
85 | 86 | } |
| 87 | + // Throw the original error, if it's one of our own (user-friendly) errors. |
86 | 88 | else if (err.error instanceof ResolverError) { |
87 | | - reject(err); |
| 89 | + reject(err.error); |
88 | 90 | } |
89 | 91 | else { |
90 | | - err.error = new ResolverError(err, file.url); |
91 | | - reject(err); |
| 92 | + reject(new ResolverError(err, file.url)); |
92 | 93 | } |
93 | 94 | } |
94 | 95 | })); |
@@ -132,15 +133,18 @@ function parseFile (file, options, $refs) { |
132 | 133 | } |
133 | 134 |
|
134 | 135 | function onError (err) { |
135 | | - if (!err || !("error" in err)) { |
| 136 | + if (!err && !options.failFast) { |
| 137 | + // No resolver could be matched |
| 138 | + reject(new UnmatchedParserError(file.url)); |
| 139 | + } |
| 140 | + else if (!err || !("error" in err)) { |
136 | 141 | reject(ono.syntax(`Unable to parse ${file.url}`)); |
137 | 142 | } |
138 | 143 | else if (err.error instanceof ParserError || err.error instanceof StoplightParserError) { |
139 | | - reject(err); |
| 144 | + reject(err.error); |
140 | 145 | } |
141 | 146 | else { |
142 | | - err.error = new ParserError(err.error.message, file.url); |
143 | | - reject(err); |
| 147 | + reject(new ParserError(err.error.message, file.url)); |
144 | 148 | } |
145 | 149 | } |
146 | 150 | })); |
|
0 commit comments