@@ -18,6 +18,7 @@ import createSlug from 'slug'
1818import {
1919 _ParsedFileResolver ,
2020 ApiOperation ,
21+ BuilderContext ,
2122 BuildResult ,
2223 FILE_KIND ,
2324 FileFormat ,
@@ -27,11 +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'
37+ import { FILE_FORMAT_JSON , FILE_FORMAT_YAML , MESSAGE_SEVERITY } from '../consts'
38+ import { isNotEmpty } from './arrays'
3539
3640export const EXPORT_FORMAT_TO_FILE_FORMAT = new Map < OperationsGroupExportFormat , FileFormat > ( [
3741 [ YAML_EXPORT_GROUP_FORMAT , FILE_FORMAT_YAML ] ,
@@ -117,21 +121,43 @@ export const getDocumentTitle = (fileId: string): string => {
117121 return fileId . substring ( cutDot ) . split ( '/' ) . pop ( ) ! . replace ( / \. [ ^ / . ] + $ / , '' )
118122}
119123
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+ default :
130+ for ( const message of messages ) {
131+ ctx . notifications . push ( {
132+ severity : MESSAGE_SEVERITY . Error ,
133+ message : message ,
134+ fileId : fileId ,
135+ } )
136+ }
137+ }
138+ }
139+
120140export const getBundledFileDataWithDependencies = async (
121141 fileId : FileId ,
122142 parsedFileResolver : _ParsedFileResolver ,
143+ onError : ( messages : string [ ] ) => void ,
123144) : Promise < { data : any ; dependencies : string [ ] } > => {
124145 const dependencies : string [ ] = [ ]
146+ const errorMessages : string [ ] = [ ]
125147
126148 const resolver : Resolver = async ( filepath : string ) => {
127149 const data = await parsedFileResolver ( filepath )
128150
129151 if ( data === null ) {
152+ // 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
153+ errorMessages . push ( `Unable to resolve the file "${ filepath } " because it does not exist.` )
130154 return { }
131155 }
132156
133157 if ( data . kind !== FILE_KIND . TEXT ) {
134- throw new Error ( `Dependency with path ${ filepath } is not a text file` )
158+ // 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
159+ errorMessages . push ( `Unable to resolve the file "${ filepath } " because it is not a valid text file.` )
160+ return { }
135161 }
136162
137163 if ( filepath !== fileId ) {
@@ -143,6 +169,10 @@ export const getBundledFileDataWithDependencies = async (
143169
144170 const bundledFileData = await bundle ( fileId , resolver )
145171
172+ if ( isNotEmpty ( errorMessages ) ) {
173+ onError ( errorMessages )
174+ }
175+
146176 return { data : bundledFileData , dependencies : dependencies }
147177}
148178
0 commit comments