@@ -18,21 +18,21 @@ module.exports = parse;
1818 * The promise resolves with the parsed file contents, NOT the raw (Buffer) contents.
1919 */
2020async function parse ( path , $refs , options ) {
21- try {
22- // Remove the URL fragment, if any
23- path = url . stripHash ( path ) ;
21+ // Remove the URL fragment, if any
22+ path = url . stripHash ( path ) ;
2423
25- // Add a new $Ref for this file, even though we don't have the value yet.
26- // This ensures that we don't simultaneously read & parse the same file multiple times
27- 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 ) ;
2827
29- // This "file object" will be passed to all resolvers and parsers.
30- let file = {
31- url : path ,
32- extension : url . getExtension ( path ) ,
33- } ;
28+ // This "file object" will be passed to all resolvers and parsers.
29+ let file = {
30+ url : path ,
31+ extension : url . getExtension ( path ) ,
32+ } ;
3433
35- // Read the file and then parse the data
34+ // Read the file and then parse the data
35+ try {
3636 const resolver = await readFile ( file , options , $refs ) ;
3737 $ref . pathType = resolver . plugin . name ;
3838 file . data = resolver . result ;
@@ -42,8 +42,14 @@ async function parse (path, $refs, options) {
4242
4343 return parser . result ;
4444 }
45- catch ( e ) {
46- return Promise . reject ( e ) ;
45+ catch ( ex ) {
46+ if ( ! ( "error" in ex ) ) {
47+ throw ex ;
48+ }
49+ else {
50+ $ref . value = ex . error ;
51+ throw ex . error ;
52+ }
4753 }
4854}
4955
@@ -113,7 +119,7 @@ function parseFile (file, options, $refs) {
113119 . then ( onParsed , onError ) ;
114120
115121 function onParsed ( parser ) {
116- if ( ! parser . plugin . allowEmpty && isEmpty ( parser . result ) ) {
122+ if ( ( ! options . failFast || ! parser . plugin . allowEmpty ) && isEmpty ( parser . result ) ) {
117123 reject ( ono . syntax ( `Error parsing "${ file . url } " as ${ parser . plugin . name } . \nParsed value is empty` ) ) ;
118124 }
119125 else {
@@ -122,12 +128,15 @@ function parseFile (file, options, $refs) {
122128 }
123129
124130 function onError ( err ) {
125- if ( err ) {
126- err = err instanceof Error ? err : new Error ( err ) ;
127- reject ( ono . syntax ( err , `Error parsing ${ file . url } ` ) ) ;
131+ if ( ! err || ! ( "error" in err ) ) {
132+ reject ( ono . syntax ( `Unable to parse ${ file . url } ` ) ) ;
133+ }
134+ else if ( err . error instanceof ParserError || err . error instanceof StoplightParserError ) {
135+ reject ( err ) ;
128136 }
129137 else {
130- reject ( ono . syntax ( `Unable to parse ${ file . url } ` ) ) ;
138+ err . error = new ParserError ( err . error . message , file . url ) ;
139+ reject ( err ) ;
131140 }
132141 }
133142 } ) ) ;
0 commit comments