Repository navigation
Expand file tree
/
Copy patherrorCodes.ts
More file actions
153 lines (145 loc) · 4.91 KB
/
Copy patherrorCodes.ts
File metadata and controls
153 lines (145 loc) · 4.91 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Standardized error codes for consistent error handling across the API.
*
* All error codes are organized by category (validation, authentication, etc.)
* and automatically mapped to appropriate HTTP status codes.
*
* Usage:
* ```typescript
* import { ERROR_CODES } from './constants/errorCodes';
*
* throw createError(ERROR_CODES.INVALID_INPUT);
* ```
*
* @see getHttpStatus - For HTTP status code mapping
* @see getLocalizedMessage - For localized error messages
*/
export const ERROR_CODES = {
// Validation errors (4000-4099) - HTTP 400
INVALID_INPUT: "INVALID_INPUT",
MISSING_FIELD: "MISSING_FIELD",
INVALID_PHONE_FORMAT: "INVALID_PHONE_FORMAT",
INVALID_AMOUNT: "INVALID_AMOUNT",
// Authentication errors (4010-4019) - HTTP 401
UNAUTHORIZED: "UNAUTHORIZED",
INVALID_CREDENTIALS: "INVALID_CREDENTIALS",
TOKEN_EXPIRED: "TOKEN_EXPIRED",
INVALID_TOKEN: "INVALID_TOKEN",
// Authorization errors (4030-4039) - HTTP 403
FORBIDDEN: "FORBIDDEN",
INSUFFICIENT_PERMISSIONS: "INSUFFICIENT_PERMISSIONS",
// Resource errors (4040-4049) - HTTP 404
NOT_FOUND: "NOT_FOUND",
RESOURCE_NOT_FOUND: "RESOURCE_NOT_FOUND",
TRANSACTION_NOT_FOUND: "TRANSACTION_NOT_FOUND",
// Conflict errors (4090-4099) - HTTP 409
CONFLICT: "CONFLICT",
DUPLICATE_REQUEST: "DUPLICATE_REQUEST",
TRANSACTION_EXISTS: "TRANSACTION_EXISTS",
// Security / abuse-prevention errors (4290-4299) - HTTP 429
ACCOUNT_LOCKED: "ACCOUNT_LOCKED",
// Business logic errors (4200-4299) - Various
LIMIT_EXCEEDED: "LIMIT_EXCEEDED",
INSUFFICIENT_BALANCE: "INSUFFICIENT_BALANCE",
INSUFFICIENT_FUNDS: "INSUFFICIENT_FUNDS",
TRANSACTION_FAILED: "TRANSACTION_FAILED",
PROVIDER_ERROR: "PROVIDER_ERROR",
RATE_LIMIT: "RATE_LIMIT",
/** Destination Stellar account has not established a trustline for the payment asset. */
TRUSTLINE_MISSING: "TRUSTLINE_MISSING",
// Server errors (5000+) - HTTP 500+
INTERNAL_ERROR: "INTERNAL_ERROR",
SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE",
DATABASE_ERROR: "DATABASE_ERROR",
};
/**
* Maps error codes to appropriate HTTP status codes.
*
* This function determines the correct HTTP status code based on the error type.
* Used by the error handler middleware to set the response status.
*
* **Status Code Mapping:**
* - 400 Bad Request: Validation/input errors
* - 401 Unauthorized: Authentication errors
* - 403 Forbidden: Authorization/permission errors
* - 404 Not Found: Resource not found errors
* - 409 Conflict: State/conflict errors
* - 429 Too Many Requests: Rate limit/quota exceeded
* - 500 Internal Server Error: Server/database errors
* - 502 Bad Gateway: External provider errors
*
* @param {string} code - Error code to map (e.g., INVALID_INPUT, UNAUTHORIZED)
* @returns {number} HTTP status code (defaults to 500 if code not recognized)
*
* @example
* getHttpStatus(ERROR_CODES.INVALID_INPUT); // Returns 400
* getHttpStatus(ERROR_CODES.UNAUTHORIZED); // Returns 401
* getHttpStatus(ERROR_CODES.LIMIT_EXCEEDED); // Returns 429
* getHttpStatus('UNKNOWN'); // Returns 500
*/
export const getHttpStatus = (code: string): number => {
if (
code.startsWith("400") ||
code === ERROR_CODES.INVALID_INPUT ||
code === ERROR_CODES.MISSING_FIELD ||
code === ERROR_CODES.INVALID_PHONE_FORMAT ||
code === ERROR_CODES.INVALID_AMOUNT
) {
return 400;
}
if (
code === ERROR_CODES.UNAUTHORIZED ||
code === ERROR_CODES.INVALID_CREDENTIALS ||
code === ERROR_CODES.TOKEN_EXPIRED ||
code === ERROR_CODES.INVALID_TOKEN
) {
return 401;
}
if (
code === ERROR_CODES.FORBIDDEN ||
code === ERROR_CODES.INSUFFICIENT_PERMISSIONS
) {
return 403;
}
if (
code === ERROR_CODES.NOT_FOUND ||
code === ERROR_CODES.RESOURCE_NOT_FOUND ||
code === ERROR_CODES.TRANSACTION_NOT_FOUND
) {
return 404;
}
if (
code === ERROR_CODES.CONFLICT ||
code === ERROR_CODES.DUPLICATE_REQUEST ||
code === ERROR_CODES.TRANSACTION_EXISTS
) {
return 409;
}
if (
code === ERROR_CODES.LIMIT_EXCEEDED ||
code === ERROR_CODES.RATE_LIMIT ||
code === ERROR_CODES.ACCOUNT_LOCKED
) {
return 429;
}
if (
code === ERROR_CODES.INSUFFICIENT_BALANCE ||
code === ERROR_CODES.INSUFFICIENT_FUNDS ||
code === ERROR_CODES.TRANSACTION_FAILED ||
code === ERROR_CODES.TRUSTLINE_MISSING
) {
return 400;
}
if (code === ERROR_CODES.PROVIDER_ERROR) {
return 502;
}
if (
code.startsWith("500") ||
code === ERROR_CODES.INTERNAL_ERROR ||
code === ERROR_CODES.SERVICE_UNAVAILABLE ||
code === ERROR_CODES.DATABASE_ERROR
) {
return 500;
}
return 500;
};