-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelper.ts
More file actions
91 lines (83 loc) · 3.11 KB
/
helper.ts
File metadata and controls
91 lines (83 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { log } from "@contentstack/cli-utilities";
export const sanitizePath = (str: string) => {
return str
?.replace(/^([\/\\]){2,}/, "./") // Normalize leading slashes/backslashes to ''
.replace(/[\/\\]+/g, "/") // Replace multiple slashes/backslashes with a single '/'
.replace(/(\.\.(\/|\\|$))+/g, ""); // Remove directory traversal (../ or ..\)
};
/**
* Error object interface for consistent error handling
*/
export interface FormattedError {
error_code?: string;
error_message?: string;
message?: string;
context?: string;
timestamp?: string;
}
/**
* Prints formatted error messages with consistent styling and helpful hints
* @param error - The error object containing error details
* @param context - The context where the error occurred (e.g., "tsgen", "graphql")
*/
export const printFormattedError = (error: FormattedError, context: string) => {
const errorCode = error?.error_code || "UNKNOWN_ERROR";
// Special handling for our numeric identifier validation errors
if (
errorCode === "VALIDATION_ERROR" &&
error?.error_message &&
error.error_message.includes("numeric identifiers")
) {
// Just print our detailed message as-is, no extra formatting
log.error(error.error_message);
return;
}
let errorMessage = "An unexpected error occurred. Try again.";
let hint = "";
switch (errorCode) {
case "AUTHENTICATION_FAILED":
errorMessage = "Authentication failed. Check your credentials and try again.";
hint = "Please check your API key, token, and region.";
break;
case "INVALID_CREDENTIALS":
errorMessage = "Invalid credentials. Please verify and re-enter your login details.";
hint = "Please verify your API key, token, and region.";
break;
case "INVALID_INTERFACE_NAME":
case "INVALID_CONTENT_TYPE_UID":
errorMessage = "Generated types contain a TypeScript syntax error.";
hint =
"Use a prefix to ensure all interface names are valid TypeScript identifiers.";
break;
case "INVALID_GLOBAL_FIELD_REFERENCE":
errorMessage = "Generated types contain a TypeScript syntax error.";
hint =
"Use a prefix to ensure all interface names are valid TypeScript identifiers.";
break;
case "VALIDATION_ERROR":
errorMessage = "Type generation failed due to a validation error.";
hint =
error?.error_message ||
"Type generation failed due to a validation error.";
break;
case "TYPE_GENERATION_FAILED":
errorMessage = "Type generation failed due to a system error. Try again.";
hint =
error?.error_message ||
"Unexpected error during type generation. Try again.";
break;
default:
errorMessage =
error?.error_message ||
error?.message ||
"An unexpected error occurred. Try again.";
hint = "Check the error details and try again.";
}
// Print formatted error output
log.error(`Type generation failed: ${errorMessage}`);
if (hint) {
log.warn(`Tip: ${hint}`);
}
log.info(`Error context: ${context}`);
log.info(`Timestamp: ${error?.timestamp || new Date().toISOString()}`);
};