@@ -18,17 +18,27 @@ const RULE_NAME = 'xgen-IPA-114-error-responses-refer-to-api-error';
1818 * @param  {object } context - The context object containing path and document information 
1919 */ 
2020export  default  ( input ,  _ ,  {  path,  documentInventory } )  =>  { 
21-   const  oas  =  documentInventory . unresolved ; 
22-   const  apiResponseObject  =  resolveObject ( oas ,  path ) ; 
23-   const  errorCode  =  path [ path . length  -  1 ] ;  // e.g., "400", "404", "500" 
21+   try  { 
22+     const  oas  =  documentInventory . unresolved ; 
23+     const  apiResponseObject  =  resolveObject ( oas ,  path ) ; 
24+     const  errorCode  =  path [ path . length  -  1 ] ; 
25+ 
26+     // Check for exception at response level 
27+     if  ( hasException ( apiResponseObject ,  RULE_NAME ) )  { 
28+       collectException ( apiResponseObject ,  RULE_NAME ,  path ) ; 
29+       return ; 
30+     } 
2431
25-   const  errors  =  checkViolationsAndReturnErrors ( apiResponseObject ,  oas ,  path ,  errorCode ) ; 
32+     const  errors  =  checkViolationsAndReturnErrors ( apiResponseObject ,  oas ,  path ,  errorCode ) ; 
33+     if  ( errors . length  !==  0 )  { 
34+       return  collectAndReturnViolation ( path ,  RULE_NAME ,  errors ) ; 
35+     } 
2636
27-   if  ( errors . length  !==  0 )  { 
28-     return  collectAndReturnViolation ( path ,  RULE_NAME ,  errors ) ; 
37+     collectAdoption ( path ,  RULE_NAME ) ; 
38+   }  catch ( e )  { 
39+     handleInternalError ( RULE_NAME ,  path ,  e ) ; 
2940  } 
3041
31-   collectAdoption ( path ,  RULE_NAME ) ; 
3242} ; 
3343
3444function  checkViolationsAndReturnErrors ( apiResponseObject ,  oas ,  path ,  errorCode )  { 
@@ -42,6 +52,8 @@ function checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode)
4252      const  schemaName  =  getSchemaNameFromRef ( apiResponseObject . $ref ) ; 
4353      const  responseSchema  =  resolveObject ( oas ,  [ 'components' ,  'responses' ,  schemaName ] ) ; 
4454      content  =  responseSchema . content ; 
55+     }  else  { 
56+       return  [ {  path,  message : `${ errorCode }   response must define content with ApiError schema reference.`  } ] ; 
4557    } 
4658
4759    for  ( const  [ mediaType ,  mediaTypeObj ]  of  Object . entries ( content ) )  { 
@@ -57,20 +69,21 @@ function checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode)
5769      const  contentPath  =  [ ...path ,  'content' ,  mediaType ] ; 
5870
5971      // Check if schema exists 
60-       if  ( ! mediaTypeObj   ||   ! mediaTypeObj . schema )  { 
72+       if  ( ! mediaTypeObj . schema )  { 
6173        errors . push ( { 
6274          path : contentPath , 
63-           message : `${ errorCode }   response must define a schema referencing ApiError` , 
75+           message : `${ errorCode }   response must define a schema referencing ApiError. ` , 
6476        } ) ; 
6577        continue ; 
6678      } 
6779
6880      // Check if schema references ApiError 
6981      const  schema  =  mediaTypeObj . schema ; 
82+ 
7083      if  ( ! schema . $ref  ||  ! schema . $ref . endsWith ( '/ApiError' ) )  { 
7184        errors . push ( { 
7285          path : contentPath , 
73-           message : `${ errorCode }   response must reference ApiError schema` , 
86+           message : `${ errorCode }   response must reference ApiError schema. ` , 
7487        } ) ; 
7588      } 
7689    } 
0 commit comments