@@ -29,13 +29,13 @@ import {
2929 PackageDocument ,
3030 ResolvedDocument ,
3131 VALIDATION_RULES_SEVERITY_LEVEL_ERROR ,
32- VALIDATION_RULES_SEVERITY_LEVEL_WARNING ,
3332 VersionDocument ,
3433 YAML_EXPORT_GROUP_FORMAT ,
3534} from '../types'
3635import { bundle , Resolver } from 'api-ref-bundler'
3736import { FILE_FORMAT_JSON , FILE_FORMAT_YAML , MESSAGE_SEVERITY } from '../consts'
3837import { isNotEmpty } from './arrays'
38+ import { RefErrorType , RefErrorTypes } from '@netcracker/qubership-apihub-api-unifier'
3939
4040export const EXPORT_FORMAT_TO_FILE_FORMAT = new Map < OperationsGroupExportFormat , FileFormat > ( [
4141 [ YAML_EXPORT_GROUP_FORMAT , FILE_FORMAT_YAML ] ,
@@ -121,42 +121,60 @@ export const getDocumentTitle = (fileId: string): string => {
121121 return fileId . substring ( cutDot ) . split ( '/' ) . pop ( ) ! . replace ( / \. [ ^ / . ] + $ / , '' )
122122}
123123
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- }
124+ export interface BundlingError {
125+ message : string
126+ errorType : RefErrorType
127+ }
128+
129+ export const createBundlingErrorHandler = ( ctx : BuilderContext , fileId : FileId ) => ( errors : BundlingError [ ] ) : void => {
130+ // Always push all errors to notifications
131+ for ( const error of errors ) {
132+ ctx . notifications . push ( {
133+ severity : MESSAGE_SEVERITY . Error ,
134+ message : error . message ,
135+ fileId : fileId ,
136+ } )
137+ }
138+
139+ // Only throw if severity is ERROR and there's at least one critical error
140+ if ( ctx . config . validationRulesSeverity ?. brokenRefs === VALIDATION_RULES_SEVERITY_LEVEL_ERROR ) {
141+ const criticalError = errors . find ( error =>
142+ error . errorType === RefErrorTypes . REF_NOT_FOUND ||
143+ error . errorType === RefErrorTypes . REF_NOT_VALID_FORMAT ,
144+ )
145+
146+ if ( criticalError ) {
147+ throw new Error ( criticalError . message )
148+ }
137149 }
138150}
139151
140152export const getBundledFileDataWithDependencies = async (
141153 fileId : FileId ,
142154 parsedFileResolver : _ParsedFileResolver ,
143- onError : ( messages : string [ ] ) => void ,
155+ onError : ( errors : BundlingError [ ] ) => void ,
144156) : Promise < { data : any ; dependencies : string [ ] } > => {
145157 const dependencies : string [ ] = [ ]
146- const errorMessages : string [ ] = [ ]
158+ const errors : BundlingError [ ] = [ ]
147159
148160 const resolver : Resolver = async ( filepath : string ) => {
149161 const data = await parsedFileResolver ( filepath )
150162
151163 if ( data === null ) {
152164 // 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.` )
165+ errors . push ( {
166+ message : `Unable to resolve the file "${ filepath } " because it does not exist.` ,
167+ errorType : RefErrorTypes . REF_NOT_FOUND ,
168+ } )
154169 return { }
155170 }
156171
157172 if ( data . kind !== FILE_KIND . TEXT ) {
158173 // 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.` )
174+ errors . push ( {
175+ message : `Unable to resolve the file "${ filepath } " because it is not a valid text file.` ,
176+ errorType : RefErrorTypes . REF_NOT_VALID_FORMAT ,
177+ } )
160178 return { }
161179 }
162180
@@ -169,8 +187,8 @@ export const getBundledFileDataWithDependencies = async (
169187
170188 const bundledFileData = await bundle ( fileId , resolver )
171189
172- if ( isNotEmpty ( errorMessages ) ) {
173- onError ( errorMessages )
190+ if ( isNotEmpty ( errors ) ) {
191+ onError ( errors )
174192 }
175193
176194 return { data : bundledFileData , dependencies : dependencies }
0 commit comments