Skip to content

Commit e23a3b0

Browse files
author
naman-contentstack
committed
added common function for error logging
1 parent 00af0b6 commit e23a3b0

File tree

3 files changed

+95
-174
lines changed

3 files changed

+95
-174
lines changed

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ fileignoreconfig:
88
checksum: 5817a6320d368fb82354709962cdb9d357c6dcba870ba53bf21eade9708505ba
99
- filename: package-lock.json
1010
checksum: 881e3bb4855315648df324c3f01fd610ee6e1997ea7af8afe6cc7b3ed517d1e5
11+
- filename: src/lib/helper.ts
12+
checksum: b271817a4a666edea15429e1a5b79c074ee7e525e26dc1e68904f003853ed7e2
1113
version: "1.0"

src/commands/tsgen.ts

Lines changed: 4 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from "path";
44
import * as fs from "fs";
55
import { cliux } from "@contentstack/cli-utilities";
66
import { generateTS, graphqlTS } from "@contentstack/types-generator";
7-
import { sanitizePath } from "../lib/helper";
7+
import { sanitizePath, printFormattedError } from "../lib/helper";
88
import { StackConnectionConfig } from "../types";
99

1010
function 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
}

src/lib/helper.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,95 @@
1+
import { cliux } from "@contentstack/cli-utilities";
2+
13
export const sanitizePath = (str: string) => {
24
return str
35
?.replace(/^([\/\\]){2,}/, "./") // Normalize leading slashes/backslashes to ''
46
.replace(/[\/\\]+/g, "/") // Replace multiple slashes/backslashes with a single '/'
57
.replace(/(\.\.(\/|\\|$))+/g, ""); // Remove directory traversal (../ or ..\)
68
};
9+
10+
/**
11+
* Error object interface for consistent error handling
12+
*/
13+
export interface FormattedError {
14+
error_code?: string;
15+
error_message?: string;
16+
message?: string;
17+
context?: string;
18+
timestamp?: string;
19+
}
20+
21+
/**
22+
* Prints formatted error messages with consistent styling and helpful hints
23+
* @param error - The error object containing error details
24+
* @param context - The context where the error occurred (e.g., "tsgen", "graphql")
25+
*/
26+
export const printFormattedError = (error: FormattedError, context: string) => {
27+
const errorCode = error?.error_code || "UNKNOWN_ERROR";
28+
let errorMessage = "An unexpected error occurred";
29+
let hint = "";
30+
31+
switch (errorCode) {
32+
case "AUTHENTICATION_FAILED":
33+
errorMessage = "Authentication failed";
34+
hint = "Please check your API key, token, and region.";
35+
break;
36+
case "INVALID_CREDENTIALS":
37+
errorMessage = "Invalid credentials provided";
38+
hint = "Please verify your API key, token, and region.";
39+
break;
40+
case "INVALID_INTERFACE_NAME":
41+
case "INVALID_CONTENT_TYPE_UID":
42+
errorMessage = "TypeScript syntax error detected in the generated types.";
43+
hint =
44+
"Use a prefix to ensure all interface names are valid TypeScript identifiers.";
45+
break;
46+
case "INVALID_GLOBAL_FIELD_REFERENCE":
47+
errorMessage = "TypeScript syntax error detected in the generated types.";
48+
hint =
49+
"Use a prefix to ensure all interface names are valid TypeScript identifiers.";
50+
break;
51+
case "VALIDATION_ERROR":
52+
errorMessage = "TypeScript syntax error detected in generated types";
53+
hint =
54+
error?.error_message ||
55+
"Validation error occurred during type generation";
56+
break;
57+
case "TYPE_GENERATION_FAILED":
58+
errorMessage = "Type generation failed due to an internal error";
59+
hint =
60+
error?.error_message ||
61+
"An unexpected error occurred during type generation";
62+
break;
63+
default:
64+
errorMessage =
65+
error?.error_message ||
66+
error?.message ||
67+
"An unexpected error occurred";
68+
hint = "Please check the error details and try again.";
69+
}
70+
71+
// Print formatted error output
72+
cliux.print(`Type generation failed: ${errorMessage}`, {
73+
color: "red",
74+
bold: true,
75+
});
76+
77+
if (hint) {
78+
cliux.print(`Tip: ${hint}`, { color: "yellow" });
79+
}
80+
81+
cliux.print(`Error context: ${context}`, {
82+
color: "cyan",
83+
});
84+
85+
cliux.print(`Timestamp: ${error?.timestamp || new Date().toISOString()}`, {
86+
color: "gray",
87+
});
88+
89+
// Show the raw error details from types-generator if different from our formatted message
90+
if (error?.error_message && error.error_message !== errorMessage) {
91+
cliux.print("", {});
92+
cliux.print("Raw error details:", { color: "magenta", bold: true });
93+
cliux.print(error.error_message, { color: "red" });
94+
}
95+
};

0 commit comments

Comments
 (0)