@@ -18,6 +18,7 @@ import createSlug from 'slug'
1818import {
1919 _ParsedFileResolver ,
2020 ApiOperation ,
21+ BuilderContext ,
2122 BuildResult ,
2223 FILE_KIND ,
2324 FileFormat ,
@@ -27,12 +28,14 @@ import {
2728 OperationsGroupExportFormat ,
2829 PackageDocument ,
2930 ResolvedDocument ,
31+ VALIDATION_RULES_SEVERITY_LEVEL_ERROR ,
32+ VALIDATION_RULES_SEVERITY_LEVEL_WARNING ,
3033 VersionDocument ,
3134 YAML_EXPORT_GROUP_FORMAT ,
3235} from '../types'
3336import { bundle , Resolver } from 'api-ref-bundler'
34- import { FILE_FORMAT_JSON , FILE_FORMAT_YAML } from '../consts'
35- import { takeIf } from './objects '
37+ import { FILE_FORMAT_JSON , FILE_FORMAT_YAML , MESSAGE_SEVERITY } from '../consts'
38+ import { isNotEmpty } from './arrays '
3639
3740export const EXPORT_FORMAT_TO_FILE_FORMAT = new Map < OperationsGroupExportFormat , FileFormat > ( [
3841 [ YAML_EXPORT_GROUP_FORMAT , FILE_FORMAT_YAML ] ,
@@ -118,24 +121,42 @@ export const getDocumentTitle = (fileId: string): string => {
118121 return fileId . substring ( cutDot ) . split ( '/' ) . pop ( ) ! . replace ( / \. [ ^ / . ] + $ / , '' )
119122}
120123
124+ export const createBundlingErrorHandler = ( ctx : BuilderContext , fileId : FileId ) => ( messages : string [ ] ) : void => {
125+ switch ( ctx . config . validationRulesSeverity ?. brokenRefs ) {
126+ case VALIDATION_RULES_SEVERITY_LEVEL_ERROR :
127+ throw new Error ( messages [ 0 ] )
128+ case VALIDATION_RULES_SEVERITY_LEVEL_WARNING :
129+ for ( const message of messages ) {
130+ ctx . notifications . push ( {
131+ severity : MESSAGE_SEVERITY . Error ,
132+ message : message ,
133+ fileId : fileId ,
134+ } )
135+ }
136+ }
137+ }
138+
121139export const getBundledFileDataWithDependencies = async (
122140 fileId : FileId ,
123141 parsedFileResolver : _ParsedFileResolver ,
124- throwOnBadRef : boolean ,
142+ onError : ( messages : string [ ] ) => void ,
125143) : Promise < { data : any ; dependencies : string [ ] } > => {
126144 const dependencies : string [ ] = [ ]
145+ const errorMessages : string [ ] = [ ]
127146
128147 const resolver : Resolver = async ( filepath : string ) => {
129148 const data = await parsedFileResolver ( filepath )
130149
131150 if ( data === null ) {
151+ // can't throw the error here because it will be suppressed: https://github.com/udamir/api-ref-bundler/blob/0.4.0/src/resolver.ts#L33
152+ errorMessages . push ( `Unable to resolve the file "${ filepath } " because it does not exist.` )
132153 return { }
133154 }
134155
135- // todo: this error has no effect because it is suppressed here: https://github.com/udamir/api-ref-bundler/blob/master/src/resolver.ts#L33
136- // move this throw to bundler hooks or you'll see just "Cannot resolve: filename" message in case of wrong file kind
137156 if ( data . kind !== FILE_KIND . TEXT ) {
138- throw new Error ( `Dependency with path ${ filepath } is not a text file` )
157+ // can't throw the error here because it will be suppressed: https://github.com/udamir/api-ref-bundler/blob/0.4.0/src/resolver.ts#L33
158+ errorMessages . push ( `Unable to resolve the file "${ filepath } " because it is not a valid text file.` )
159+ return { }
139160 }
140161
141162 if ( filepath !== fileId ) {
@@ -145,14 +166,11 @@ export const getBundledFileDataWithDependencies = async (
145166 return data . data
146167 }
147168
148- const bundledFileData = await bundle ( fileId , resolver , {
149- hooks : {
150- ...takeIf (
151- { onError : ( message : string ) => { throw new Error ( message ) } } ,
152- throwOnBadRef ,
153- ) ,
154- } ,
155- } )
169+ const bundledFileData = await bundle ( fileId , resolver )
170+
171+ if ( isNotEmpty ( errorMessages ) ) {
172+ onError ( errorMessages )
173+ }
156174
157175 return { data : bundledFileData , dependencies : dependencies }
158176}
0 commit comments