@@ -4,7 +4,7 @@ import * as path from "path";
44import * as fs from "fs" ;
55import { cliux } from "@contentstack/cli-utilities" ;
66import { generateTS , graphqlTS } from "@contentstack/types-generator" ;
7- import { sanitizePath } from "../lib/helper" ;
7+ import { sanitizePath , printFormattedError } from "../lib/helper" ;
88import { StackConnectionConfig } from "../types" ;
99
1010function createOutputPath ( outputFile : string ) {
@@ -185,54 +185,7 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
185185 `Successfully added the GraphQL schema type definitions to '${ outputPath } '.` ,
186186 ) ;
187187 } catch ( error : any ) {
188- const errorCode = error ?. error_code || "UNKNOWN_ERROR" ;
189- let errorMessage = "An error occurred while generating GraphQL types" ;
190- let hint = "" ;
191-
192- // If we have a new error format with error_code, use it for better messaging
193- if ( error ?. error_code ) {
194- switch ( errorCode ) {
195- case "AUTHENTICATION_FAILED" :
196- errorMessage = `Authentication failed` ;
197- hint = "Please check your API key, token, and region." ;
198- break ;
199- case "INVALID_CREDENTIALS" :
200- errorMessage = `Invalid credentials provided` ;
201- hint = "Please verify your API key, token, and region." ;
202- break ;
203- default :
204- errorMessage =
205- error ?. error_message || "An unexpected error occurred" ;
206- hint = "Please check the error details and try again." ;
207- }
208- } else {
209- // Fallback to old error format
210- errorMessage =
211- error ?. error_message ||
212- error ?. message ||
213- "An error occurred while generating GraphQL types" ;
214- hint = "Please check your API credentials and try again." ;
215- }
216- cliux . print ( `Type generation failed: ${ errorMessage } ` , {
217- color : "red" ,
218- bold : true ,
219- } ) ;
220- if ( hint ) {
221- cliux . print ( `Tip: ${ hint } ` , { color : "yellow" } ) ;
222- }
223- cliux . print ( `Error context: ${ error ?. context || "graphql" } ` , {
224- color : "cyan" ,
225- } ) ;
226- cliux . print (
227- `Timestamp: ${ error ?. timestamp || new Date ( ) . toISOString ( ) } ` ,
228- { color : "gray" } ,
229- ) ;
230-
231- if ( error ?. error_message && error . error_message !== errorMessage ) {
232- cliux . print ( "" , { } ) ;
233- cliux . print ( "Raw error details:" , { color : "magenta" , bold : true } ) ;
234- cliux . print ( error . error_message , { color : "red" } ) ;
235- }
188+ printFormattedError ( error , error ?. context || "graphql" ) ;
236189 process . exit ( 1 ) ;
237190 }
238191 } else {
@@ -279,135 +232,12 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
279232
280233 // this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
281234 } catch ( error : any ) {
282- const errorCode = error ?. error_code || "UNKNOWN_ERROR" ;
283- let errorMessage =
284- "An error occurred while generating TypeScript types" ;
285- let hint = "" ;
286-
287- // If we have a new error format with error_code, use it for better messaging
288- if ( error ?. error_code ) {
289- switch ( errorCode ) {
290- case "INVALID_INTERFACE_NAME" :
291- case "INVALID_CONTENT_TYPE_UID" :
292- errorMessage = `TypeScript syntax error detected in the generated types.` ;
293- hint = `Use a prefix to ensure all interface names are valid TypeScript identifiers.` ;
294- break ;
295- case "INVALID_GLOBAL_FIELD_REFERENCE" :
296- errorMessage = `TypeScript syntax error detected in the generated types.` ;
297- hint = `Use a prefix to ensure all interface names are valid TypeScript identifiers.` ;
298- break ;
299- case "VALIDATION_ERROR" :
300- errorMessage = `TypeScript syntax error detected in generated types` ;
301- hint =
302- error ?. error_message ||
303- "Validation error occurred during type generation" ;
304- break ;
305- case "TYPE_GENERATION_FAILED" :
306- errorMessage = `Type generation failed due to an internal error` ;
307- hint =
308- error ?. error_message ||
309- "An unexpected error occurred during type generation" ;
310- break ;
311- case "AUTHENTICATION_FAILED" :
312- errorMessage = `Authentication failed` ;
313- hint = "Please check your API key, token, and region." ;
314- break ;
315- case "INVALID_CREDENTIALS" :
316- errorMessage = `Invalid credentials provided` ;
317- hint = "Please verify your API key, token, and region." ;
318- break ;
319- default :
320- errorMessage =
321- error ?. error_message || "An unexpected error occurred" ;
322- hint = "Please check the error details and try again." ;
323- }
324- } else {
325- // Fallback to old error format
326- errorMessage =
327- error ?. error_message ||
328- error ?. message ||
329- "An error occurred while generating TypeScript types" ;
330- hint =
331- "Use a prefix to ensure all interface names are valid TypeScript identifiers." ;
332- }
333-
334- // Display our formatted error first
335- cliux . print ( `Type generation failed: ${ errorMessage } ` , {
336- color : "red" ,
337- bold : true ,
338- } ) ;
339- if ( hint ) {
340- cliux . print ( `Tip: ${ hint } ` , { color : "yellow" } ) ;
341- }
342- cliux . print ( `Error context: ${ error ?. context || "tsgen" } ` , {
343- color : "cyan" ,
344- } ) ;
345- cliux . print (
346- `Timestamp: ${ error ?. timestamp || new Date ( ) . toISOString ( ) } ` ,
347- { color : "gray" } ,
348- ) ;
235+ printFormattedError ( error , error ?. context || "tsgen" ) ;
349236 process . exit ( 1 ) ;
350237 }
351238 }
352239 } catch ( error : any ) {
353- // Handle both old and new error formats
354- const errorCode = error ?. error_code || "UNKNOWN_ERROR" ;
355- let errorMessage = "An unexpected error occurred" ;
356- let hint = "" ;
357-
358- // If we have a new error format with error_code, use it for better messaging
359- if ( error ?. error_code ) {
360- switch ( errorCode ) {
361- case "AUTHENTICATION_FAILED" :
362- errorMessage = `Authentication failed` ;
363- hint = "Please check your API key, token, and region." ;
364- break ;
365- case "INVALID_CREDENTIALS" :
366- errorMessage = `Invalid credentials provided` ;
367- hint = "Please verify your API key, token, and region." ;
368- break ;
369- case "INVALID_INTERFACE_NAME" :
370- case "INVALID_CONTENT_TYPE_UID" :
371- case "INVALID_GLOBAL_FIELD_REFERENCE" :
372- errorMessage = `TypeScript syntax error detected in the generated types.` ;
373- hint = `Use a prefix to ensure all interface names are valid TypeScript identifiers.` ;
374- break ;
375- default :
376- errorMessage =
377- error ?. error_message || "An unexpected error occurred" ;
378- hint = "Please check the error details and try again." ;
379- }
380- } else {
381- // Fallback to old error format
382- errorMessage =
383- error ?. error_message ||
384- error ?. message ||
385- "An unexpected error occurred" ;
386- hint = "Please check your configuration and try again." ;
387- }
388-
389- // Display our formatted error first
390- cliux . print ( `Type generation failed: ${ errorMessage } ` , {
391- color : "red" ,
392- bold : true ,
393- } ) ;
394- if ( hint ) {
395- cliux . print ( `Tip: ${ hint } ` , { color : "yellow" } ) ;
396- }
397- cliux . print ( `Error context: ${ error ?. context || "tsgen" } ` , {
398- color : "cyan" ,
399- } ) ;
400- cliux . print (
401- `Timestamp: ${ error ?. timestamp || new Date ( ) . toISOString ( ) } ` ,
402- { color : "gray" } ,
403- ) ;
404-
405- // Show the raw error details from types-generator
406- if ( error ?. error_message && error . error_message !== errorMessage ) {
407- cliux . print ( "" , { } ) ;
408- cliux . print ( "Raw error details:" , { color : "magenta" , bold : true } ) ;
409- cliux . print ( error . error_message , { color : "red" } ) ;
410- }
240+ printFormattedError ( error , error ?. context || "tsgen" ) ;
411241 process . exit ( 1 ) ;
412242 }
413243 }
0 commit comments