33const { ono } = require ( "@jsdevtools/ono" ) ;
44const url = require ( "./util/url" ) ;
55const plugins = require ( "./util/plugins" ) ;
6+ const { StoplightParserError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require ( "./util/errors" ) ;
67
78module . exports = parse ;
89
@@ -17,21 +18,21 @@ module.exports = parse;
1718 * The promise resolves with the parsed file contents, NOT the raw (Buffer) contents.
1819 */
1920async function parse ( path , $refs , options ) {
20- try {
21- // Remove the URL fragment, if any
22- path = url . stripHash ( path ) ;
21+ // Remove the URL fragment, if any
22+ path = url . stripHash ( path ) ;
2323
24- // Add a new $Ref for this file, even though we don't have the value yet.
25- // This ensures that we don't simultaneously read & parse the same file multiple times
26- let $ref = $refs . _add ( path ) ;
24+ // Add a new $Ref for this file, even though we don't have the value yet.
25+ // This ensures that we don't simultaneously read & parse the same file multiple times
26+ let $ref = $refs . _add ( path ) ;
2727
28- // This "file object" will be passed to all resolvers and parsers.
29- let file = {
30- url : path ,
31- extension : url . getExtension ( path ) ,
32- } ;
28+ // This "file object" will be passed to all resolvers and parsers.
29+ let file = {
30+ url : path ,
31+ extension : url . getExtension ( path ) ,
32+ } ;
3333
34- // Read the file and then parse the data
34+ // Read the file and then parse the data
35+ try {
3536 const resolver = await readFile ( file , options , $refs ) ;
3637 $ref . pathType = resolver . plugin . name ;
3738 file . data = resolver . result ;
@@ -41,8 +42,12 @@ async function parse (path, $refs, options) {
4142
4243 return parser . result ;
4344 }
44- catch ( e ) {
45- return Promise . reject ( e ) ;
45+ catch ( err ) {
46+ if ( isHandledError ( err ) ) {
47+ $ref . value = err ;
48+ }
49+
50+ throw err ;
4651 }
4752}
4853
@@ -71,13 +76,20 @@ function readFile (file, options, $refs) {
7176 . then ( resolve , onError ) ;
7277
7378 function onError ( err ) {
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.
85+ reject ( ono . syntax ( `Unable to resolve $ref pointer "${ file . url } "` ) ) ;
86+ }
7487 // Throw the original error, if it's one of our own (user-friendly) errors.
75- // Otherwise, throw a generic, friendly error.
76- if ( err && ! ( err instanceof SyntaxError ) ) {
77- reject ( err ) ;
88+ else if ( err . error instanceof ResolverError ) {
89+ reject ( err . error ) ;
7890 }
7991 else {
80- reject ( ono . syntax ( `Unable to resolve $ref pointer " ${ file . url } "` ) ) ;
92+ reject ( new ResolverError ( err , file . url ) ) ;
8193 }
8294 }
8395 } ) ) ;
@@ -112,7 +124,7 @@ function parseFile (file, options, $refs) {
112124 . then ( onParsed , onError ) ;
113125
114126 function onParsed ( parser ) {
115- if ( ! parser . plugin . allowEmpty && isEmpty ( parser . result ) ) {
127+ if ( ( ! options . failFast || ! parser . plugin . allowEmpty ) && isEmpty ( parser . result ) ) {
116128 reject ( ono . syntax ( `Error parsing "${ file . url } " as ${ parser . plugin . name } . \nParsed value is empty` ) ) ;
117129 }
118130 else {
@@ -121,13 +133,19 @@ function parseFile (file, options, $refs) {
121133 }
122134
123135 function onError ( err ) {
124- if ( err ) {
125- err = err instanceof Error ? err : new Error ( err ) ;
126- reject ( ono . syntax ( err , `Error parsing ${ file . url } ` ) ) ;
136+ if ( ! err && ! options . failFast ) {
137+ // No resolver could be matched
138+ reject ( new UnmatchedParserError ( file . url ) ) ;
127139 }
128- else {
140+ else if ( ! err || ! ( "error" in err ) ) {
129141 reject ( ono . syntax ( `Unable to parse ${ file . url } ` ) ) ;
130142 }
143+ else if ( err . error instanceof ParserError || err . error instanceof StoplightParserError ) {
144+ reject ( err . error ) ;
145+ }
146+ else {
147+ reject ( new ParserError ( err . error . message , file . url ) ) ;
148+ }
131149 }
132150 } ) ) ;
133151}
0 commit comments