11import { AbsoluteFilePath , doesPathExist , resolve } from "@fern-api/fs-utils" ;
2+ import { CliError } from "@fern-api/task-context" ;
23import { mkdtemp , readFile , writeFile } from "fs/promises" ;
34import { tmpdir } from "os" ;
45import path from "path" ;
@@ -59,7 +60,10 @@ export class ApiSpecResolver {
5960 private async resolveLocal ( { reference } : { reference : string } ) : Promise < ApiSpecResolver . Result > {
6061 const absoluteFilePath = resolve ( this . context . cwd , reference ) ;
6162 if ( ! ( await doesPathExist ( absoluteFilePath ) ) ) {
62- throw new Error ( `API spec file does not exist: ${ reference } ` ) ;
63+ throw new CliError ( {
64+ message : `API spec file does not exist: ${ reference } ` ,
65+ code : CliError . Code . ConfigError
66+ } ) ;
6367 }
6468
6569 const content = await readFile ( absoluteFilePath , "utf-8" ) ;
@@ -74,14 +78,19 @@ export class ApiSpecResolver {
7478 private async fetchContent ( { url } : { url : string } ) : Promise < { content : string ; contentType : string } > {
7579 const response = await fetch ( url , { signal : AbortSignal . timeout ( FETCH_API_SPEC_REQUEST_TIMEOUT_MS ) } ) ;
7680 if ( ! response . ok ) {
77- throw new Error ( `Failed to fetch "${ url } ": HTTP ${ response . status } ${ response . statusText } ` ) ;
81+ throw new CliError ( {
82+ message : `Failed to fetch "${ url } ": HTTP ${ response . status } ${ response . statusText } ` ,
83+ code : CliError . Code . NetworkError
84+ } ) ;
7885 }
7986 const contentType = response . headers . get ( "content-type" ) ?? "" ;
8087 if ( contentType . includes ( "text/html" ) ) {
81- throw new Error (
82- `The URL "${ url } " returned HTML content. ` +
83- `Ensure you're pointing to a raw spec URL, not a documentation page.`
84- ) ;
88+ throw new CliError ( {
89+ message :
90+ `The URL "${ url } " returned HTML content. ` +
91+ `Ensure you're pointing to a raw spec URL, not a documentation page.` ,
92+ code : CliError . Code . ConfigError
93+ } ) ;
8594 }
8695 const content = await response . text ( ) ;
8796 return { content, contentType } ;
@@ -116,7 +125,10 @@ export class ApiSpecResolver {
116125 case "asyncapi" :
117126 return { asyncapi : absoluteFilePath , origin } ;
118127 default :
119- throw new Error ( `Unsupported spec type for flags mode: "${ specType } ". Supported: openapi, asyncapi` ) ;
128+ throw new CliError ( {
129+ message : `Unsupported spec type for flags mode: "${ specType } ". Supported: openapi, asyncapi` ,
130+ code : CliError . Code . ConfigError
131+ } ) ;
120132 }
121133 }
122134
0 commit comments