@@ -11,6 +11,7 @@ import { defaultInterfaces } from "./stack/builtins";
1111import { format } from "../format/index" ;
1212import { ContentType } from "../types/schema" ;
1313import { cliux } from "@contentstack/cli-utilities" ;
14+ import { createValidationError , createErrorDetails } from "./shared/utils" ;
1415
1516export const generateTS = async ( {
1617 token,
@@ -28,11 +29,9 @@ export const generateTS = async ({
2829} : GenerateTS ) => {
2930 try {
3031 if ( ! token || ! tokenType || ! apiKey || ! environment || ! region ) {
31- throw {
32- type : "validation" ,
33- error_message :
34- "Please provide all the required params (token, tokenType, apiKey, environment, region)" ,
35- } ;
32+ throw createValidationError (
33+ "Please provide all the required params (token, tokenType, apiKey, environment, region)"
34+ ) ;
3635 }
3736
3837 if ( tokenType === TOKEN_TYPE . DELIVERY ) {
@@ -62,11 +61,9 @@ export const generateTS = async ({
6261 "Please create Content Models to generate type definitions" ,
6362 { color : "yellow" }
6463 ) ;
65- throw {
66- type : "validation" ,
67- error_message :
68- "There are no Content Types in the Stack, please create Content Models to generate type definitions" ,
69- } ;
64+ throw createValidationError (
65+ "There are no Content Types in the Stack, please create Content Models to generate type definitions"
66+ ) ;
7067 }
7168
7269 let schemas : ContentType [ ] = [ ] ;
@@ -95,43 +92,40 @@ export const generateTS = async ({
9592 }
9693 } catch ( error : any ) {
9794 if ( error . type === "validation" ) {
98- throw { error_message : error . error_message } ;
95+ // Handle validation errors with proper error codes
96+ throw {
97+ error_message : error . error_message ,
98+ error_code : error . error_code || "VALIDATION_ERROR" ,
99+ } ;
99100 } else {
100101 const errorObj = JSON . parse ( error . message . replace ( "Error: " , "" ) ) ;
101102 let errorMessage = "Something went wrong" ;
103+ let errorCode = "API_ERROR" ;
104+
102105 if ( errorObj . status ) {
103106 switch ( errorObj . status ) {
104107 case 401 :
105- cliux . print ( "Authentication failed" , {
106- color : "red" ,
107- bold : true ,
108- } ) ;
109- cliux . print ( "Please check your apiKey, token, and region" , {
110- color : "yellow" ,
111- } ) ;
112108 errorMessage =
113109 "Unauthorized: The apiKey, token or region is not valid." ;
110+ errorCode = "AUTHENTICATION_FAILED" ;
114111 break ;
115112 case 412 :
116- cliux . print ( "Invalid credentials" , { color : "red" , bold : true } ) ;
117- cliux . print ( "Please verify your apiKey, token, and region" , {
118- color : "yellow" ,
119- } ) ;
120113 errorMessage =
121114 "Invalid Credentials: Please check the provided apiKey, token and region." ;
115+ errorCode = "INVALID_CREDENTIALS" ;
122116 break ;
123117 default :
124- cliux . print ( `API Error (${ errorObj . status } )` , {
125- color : "red" ,
126- bold : true ,
127- } ) ;
128118 errorMessage = `${ errorMessage } , ${ errorObj . error_message } ` ;
119+ errorCode = `API_ERROR_${ errorObj . status } ` ;
129120 }
130121 }
131122 if ( errorObj . error_message && ! errorObj . status ) {
132123 errorMessage = `${ errorMessage } , ${ errorObj . error_message } ` ;
133124 }
134- throw { error_message : errorMessage } ;
125+ throw {
126+ error_message : errorMessage ,
127+ error_code : errorCode ,
128+ } ;
135129 }
136130 }
137131} ;
@@ -193,21 +187,19 @@ export const generateTSFromContentTypes = async ({
193187
194188 return output ;
195189 } catch ( err : any ) {
196- // Enhanced error logging with more context
197- const errorMessage = err . message || "Unknown error occurred" ;
198- const errorDetails = {
199- error_message : `Type generation failed: ${ errorMessage } ` ,
200- context : "generateTSFromContentTypes" ,
201- timestamp : new Date ( ) . toISOString ( ) ,
202- error_type : err . constructor . name ,
203- } ;
204-
205- // Log detailed error information for debugging
206- cliux . print ( `Type generation failed: ${ errorMessage } ` , {
207- color : "red" ,
208- bold : true ,
209- } ) ;
190+ // Handle numeric identifier errors specially to preserve their detailed format
191+ if (
192+ err . type === "validation" &&
193+ err . error_code === "VALIDATION_ERROR" &&
194+ err . error_message &&
195+ err . error_message . includes ( "numeric identifiers" )
196+ ) {
197+ // Pass through the detailed error as-is
198+ throw err ;
199+ }
210200
201+ // Use common function to create detailed error information for other errors
202+ const errorDetails = createErrorDetails ( err , "generateTSFromContentTypes" ) ;
211203 throw errorDetails ;
212204 }
213205} ;
0 commit comments