@carecard/common-util 3.1.2 → 3.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +39 -39
- package/package.json +1 -1
- package/src/lib/appErrorHandlers.js +1 -1
- package/src/lib/errorUtils.js +30 -16
package/index.d.ts
CHANGED
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
* @returns A new object with the extracted properties.
|
|
6
6
|
*/
|
|
7
7
|
export function extractObjectWithProperties(
|
|
8
|
-
obj:
|
|
8
|
+
obj: unknown,
|
|
9
9
|
arrayOfProperties: string[]
|
|
10
|
-
): Record<string,
|
|
10
|
+
): Record<string, unknown>;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Utility functions for object manipulation.
|
|
@@ -18,69 +18,69 @@ export const util: {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
/** Throws an Account_Suspended error. */
|
|
21
|
-
export function throwAccountSuspendedError(params?: { userMessage?: string; details?:
|
|
21
|
+
export function throwAccountSuspendedError(params?: { userMessage?: string; details?: unknown }): never;
|
|
22
22
|
/** Throws an Account_Blocked error. */
|
|
23
|
-
export function throwAccountBlockedError(params?: { userMessage?: string; details?:
|
|
23
|
+
export function throwAccountBlockedError(params?: { userMessage?: string; details?: unknown }): never;
|
|
24
24
|
/** Throws an Account_Inactive error. */
|
|
25
|
-
export function throwAccountInactiveError(params?: { userMessage?: string; details?:
|
|
25
|
+
export function throwAccountInactiveError(params?: { userMessage?: string; details?: unknown }): never;
|
|
26
26
|
/** Middleware to handle 404 Not Found. */
|
|
27
|
-
export function notFound404(req:
|
|
27
|
+
export function notFound404(req: unknown, res: unknown, next: unknown): void;
|
|
28
28
|
/** Central application error handler middleware. */
|
|
29
|
-
export function appErrorHandler(err:
|
|
29
|
+
export function appErrorHandler(err: unknown, req: unknown, res: unknown, next: unknown): void;
|
|
30
30
|
/** Throws a Validation_Failure error. */
|
|
31
31
|
export function throwValidationFailureError(params?: {
|
|
32
32
|
userMessage?: string;
|
|
33
|
-
details?:
|
|
33
|
+
details?: unknown;
|
|
34
34
|
}): never;
|
|
35
35
|
/** Throws a Record_Exist error. */
|
|
36
|
-
export function throwRecordExistError(params?: { userMessage?: string; details?:
|
|
36
|
+
export function throwRecordExistError(params?: { userMessage?: string; details?: unknown }): never;
|
|
37
37
|
/** Throws a Wrong_Credentials error. */
|
|
38
|
-
export function throwWrongCredentialsError(params?: { userMessage?: string; details?:
|
|
38
|
+
export function throwWrongCredentialsError(params?: { userMessage?: string; details?: unknown }): never;
|
|
39
39
|
/** Throws a Login_Required error. */
|
|
40
|
-
export function throwLoginRequiredError(params?: { userMessage?: string; details?:
|
|
40
|
+
export function throwLoginRequiredError(params?: { userMessage?: string; details?: unknown }): never;
|
|
41
41
|
/** Throws a Record_NotFound error. */
|
|
42
|
-
export function throwRecordNotFoundError(params?: { userMessage?: string; details?:
|
|
42
|
+
export function throwRecordNotFoundError(params?: { userMessage?: string; details?: unknown }): never;
|
|
43
43
|
/** Throws a Record_NotSaved error. */
|
|
44
|
-
export function throwRecordNotSavedError(params?: { userMessage?: string; details?:
|
|
44
|
+
export function throwRecordNotSavedError(params?: { userMessage?: string; details?: unknown }): never;
|
|
45
45
|
/** Throws an Update_Failed error. */
|
|
46
|
-
export function throwUpdateFailedError(params?: { userMessage?: string; details?:
|
|
46
|
+
export function throwUpdateFailedError(params?: { userMessage?: string; details?: unknown }): never;
|
|
47
47
|
/** Throws a Transaction_Failed error. */
|
|
48
48
|
export function throwTransactionFailedError(params?: {
|
|
49
49
|
userMessage?: string;
|
|
50
|
-
details?:
|
|
50
|
+
details?: unknown;
|
|
51
51
|
}): never;
|
|
52
52
|
/** Throws a Used_Token error. */
|
|
53
|
-
export function throwUsedTokenError(params?: { userMessage?: string; details?:
|
|
53
|
+
export function throwUsedTokenError(params?: { userMessage?: string; details?: unknown }): never;
|
|
54
54
|
/** Throws a Bad_Visitor_Token error. */
|
|
55
|
-
export function throwBadVisitorTokenError(params?: { userMessage?: string; details?:
|
|
55
|
+
export function throwBadVisitorTokenError(params?: { userMessage?: string; details?: unknown }): never;
|
|
56
56
|
/** Throws a File_Format_Not_Supported error. */
|
|
57
57
|
export function throwFileFormatNotSupportedError(params?: {
|
|
58
58
|
userMessage?: string;
|
|
59
|
-
details?:
|
|
59
|
+
details?: unknown;
|
|
60
60
|
}): never;
|
|
61
61
|
/** Throws a Not_Authorized error. */
|
|
62
|
-
export function throwNotAuthorizedError(params?: { userMessage?: string; details?:
|
|
62
|
+
export function throwNotAuthorizedError(params?: { userMessage?: string; details?: unknown }): never;
|
|
63
63
|
/** Throws a Bad_Input error. */
|
|
64
|
-
export function throwBadInputError(params?: { userMessage?: string; details?:
|
|
64
|
+
export function throwBadInputError(params?: { userMessage?: string; details?: unknown }): never;
|
|
65
65
|
/** Throws an Input_Not_Uuid error. */
|
|
66
|
-
export function throwInputNotUuidError(params?: { userMessage?: string; details?:
|
|
66
|
+
export function throwInputNotUuidError(params?: { userMessage?: string; details?: unknown }): never;
|
|
67
67
|
/** Throws a File_Too_Large error. */
|
|
68
|
-
export function throwFileTooLargeError(params?: { userMessage?: string; details?:
|
|
68
|
+
export function throwFileTooLargeError(params?: { userMessage?: string; details?: unknown }): never;
|
|
69
69
|
/** Throws an Invalid_Time_Value error. */
|
|
70
|
-
export function throwInvalidTimeValueError(params?: { userMessage?: string; details?:
|
|
70
|
+
export function throwInvalidTimeValueError(params?: { userMessage?: string; details?: unknown }): never;
|
|
71
71
|
/** Throws a Not_Found error. */
|
|
72
|
-
export function throwNotFoundError(params?: { userMessage?: string; details?:
|
|
72
|
+
export function throwNotFoundError(params?: { userMessage?: string; details?: unknown }): never;
|
|
73
73
|
/** Throws a Record_Save_Failure error. */
|
|
74
74
|
export function throwRecordSaveFailureError(params?: {
|
|
75
75
|
userMessage?: string;
|
|
76
|
-
details?:
|
|
76
|
+
details?: unknown;
|
|
77
77
|
}): never;
|
|
78
78
|
/** Throws an Application_Error error. */
|
|
79
|
-
export function throwApplicationError(params?: { userMessage?: string; details?:
|
|
79
|
+
export function throwApplicationError(params?: { userMessage?: string; details?: unknown }): never;
|
|
80
80
|
/** Throws a Network_Error error. */
|
|
81
|
-
export function throwNetworkError(params?: { userMessage?: string; details?:
|
|
81
|
+
export function throwNetworkError(params?: { userMessage?: string; details?: unknown }): never;
|
|
82
82
|
/** Throws an Unexpected_Error error. */
|
|
83
|
-
export function throwUnexpectedError(params?: { userMessage?: string; details?:
|
|
83
|
+
export function throwUnexpectedError(params?: { userMessage?: string; details?: unknown }): never;
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* Application-level error handlers and throwers.
|
|
@@ -116,11 +116,11 @@ export const error: {
|
|
|
116
116
|
};
|
|
117
117
|
|
|
118
118
|
/** Sets 200 OK status and optionally an ETag header. */
|
|
119
|
-
export function setOk200(res:
|
|
119
|
+
export function setOk200(res: unknown, ETag?: string): unknown;
|
|
120
120
|
/** Sets 201 Created status. */
|
|
121
|
-
export function setCreated201(res:
|
|
121
|
+
export function setCreated201(res: unknown): unknown;
|
|
122
122
|
/** Sets 400 Bad Request status. */
|
|
123
|
-
export function setBadRequest400ClientError(res:
|
|
123
|
+
export function setBadRequest400ClientError(res: unknown): unknown;
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
126
|
* Utility functions for setting HTTP response status codes and headers.
|
|
@@ -153,7 +153,7 @@ export interface ApiResponseMeta {
|
|
|
153
153
|
limit: number;
|
|
154
154
|
total: number;
|
|
155
155
|
};
|
|
156
|
-
[key: string]:
|
|
156
|
+
[key: string]: unknown;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
/**
|
|
@@ -169,7 +169,7 @@ export interface ApiError {
|
|
|
169
169
|
/**
|
|
170
170
|
* Standard API response body.
|
|
171
171
|
*/
|
|
172
|
-
export interface ApiResponse<T =
|
|
172
|
+
export interface ApiResponse<T = unknown> {
|
|
173
173
|
success: boolean;
|
|
174
174
|
statusCode: number;
|
|
175
175
|
message: string;
|
|
@@ -181,21 +181,21 @@ export interface ApiResponse<T = any> {
|
|
|
181
181
|
/**
|
|
182
182
|
* Express middleware to generate and attach request context.
|
|
183
183
|
*/
|
|
184
|
-
export function requestContext(req:
|
|
184
|
+
export function requestContext(req: unknown, res: unknown, next: unknown): void;
|
|
185
185
|
|
|
186
186
|
/**
|
|
187
187
|
* Standardized API response utility.
|
|
188
188
|
*/
|
|
189
|
-
export function sendResponse<T =
|
|
190
|
-
req:
|
|
191
|
-
res:
|
|
189
|
+
export function sendResponse<T = unknown>(params: {
|
|
190
|
+
req: unknown;
|
|
191
|
+
res: unknown;
|
|
192
192
|
statusCode?: number;
|
|
193
193
|
success?: boolean;
|
|
194
194
|
message?: string;
|
|
195
195
|
data?: T | null;
|
|
196
196
|
error?: ApiError | null;
|
|
197
197
|
meta?: Partial<ApiResponseMeta>;
|
|
198
|
-
}):
|
|
198
|
+
}): unknown;
|
|
199
199
|
|
|
200
200
|
/**
|
|
201
201
|
* Helper for standardized errors.
|
|
@@ -240,4 +240,4 @@ export enum ApiErrorType {
|
|
|
240
240
|
/**
|
|
241
241
|
* Extracts error message from standardized error data.
|
|
242
242
|
*/
|
|
243
|
-
export function getApiErrorMessage(errorData:
|
|
243
|
+
export function getApiErrorMessage(errorData: unknown, t: (key: string) => string): string;
|
package/package.json
CHANGED
|
@@ -53,9 +53,9 @@ function notFound404(req, res, next) {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
function appErrorHandler(err, req, res, next) {
|
|
56
|
-
let statusCode = 500;
|
|
57
56
|
const errorMessage = err?.message || 'Internal Server Error';
|
|
58
57
|
const errorCode = err?.code;
|
|
58
|
+
let statusCode;
|
|
59
59
|
|
|
60
60
|
switch (errorCode || err?.message) {
|
|
61
61
|
case 'Account_Suspended':
|
package/src/lib/errorUtils.js
CHANGED
|
@@ -18,12 +18,7 @@ function getApiErrorMessage(errorData, t) {
|
|
|
18
18
|
let message;
|
|
19
19
|
|
|
20
20
|
// Prefer nested error message if available
|
|
21
|
-
if (
|
|
22
|
-
errorData.error &&
|
|
23
|
-
typeof errorData.error === 'object' &&
|
|
24
|
-
errorData.error !== null &&
|
|
25
|
-
errorData.error.message
|
|
26
|
-
) {
|
|
21
|
+
if (errorData.error && typeof errorData.error === 'object' && errorData.error.message) {
|
|
27
22
|
message = errorData.error.message;
|
|
28
23
|
}
|
|
29
24
|
|
|
@@ -32,28 +27,47 @@ function getApiErrorMessage(errorData, t) {
|
|
|
32
27
|
message = errorData.message;
|
|
33
28
|
}
|
|
34
29
|
|
|
35
|
-
if (message) {
|
|
36
|
-
return message;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
30
|
// Try to get error code
|
|
40
31
|
let errorCode;
|
|
41
32
|
if (errorData.error) {
|
|
42
33
|
if (typeof errorData.error === 'string') {
|
|
43
34
|
errorCode = errorData.error;
|
|
44
|
-
} else if (
|
|
45
|
-
typeof errorData.error === 'object' &&
|
|
46
|
-
errorData.error !== null &&
|
|
47
|
-
errorData.error.code
|
|
48
|
-
) {
|
|
35
|
+
} else if (typeof errorData.error === 'object' && errorData.error.code) {
|
|
49
36
|
errorCode = errorData.error.code;
|
|
50
37
|
}
|
|
51
38
|
}
|
|
52
39
|
|
|
40
|
+
// If we have a recognized error code, try to translate it first
|
|
41
|
+
// Unless we have a message that doesn't look like an error code
|
|
42
|
+
if (errorCode) {
|
|
43
|
+
const translated = translateCode(errorCode, t);
|
|
44
|
+
if (translated && translated !== errorCode && translated !== t('errors.unexpected_error')) {
|
|
45
|
+
// If we have a translation, and it's not the code itself, return it
|
|
46
|
+
// But wait, if the message is specific, we might still want it.
|
|
47
|
+
// For now, let's just return the translated message if it's found.
|
|
48
|
+
return translated;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (message) {
|
|
53
|
+
return message;
|
|
54
|
+
}
|
|
55
|
+
|
|
53
56
|
if (!errorCode) {
|
|
54
57
|
return t('errors.unexpected_error');
|
|
55
58
|
}
|
|
56
59
|
|
|
60
|
+
return translateCode(errorCode, t) || t('errors.unexpected_error');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Helper to translate error code.
|
|
65
|
+
* @param {string} errorCode
|
|
66
|
+
* @param {Function} t
|
|
67
|
+
* @returns {string|null}
|
|
68
|
+
*/
|
|
69
|
+
function translateCode(errorCode, t) {
|
|
70
|
+
if (!errorCode) return null;
|
|
57
71
|
const code = errorCode.toLowerCase();
|
|
58
72
|
|
|
59
73
|
switch (code) {
|
|
@@ -128,7 +142,7 @@ function getApiErrorMessage(errorData, t) {
|
|
|
128
142
|
|
|
129
143
|
case ApiErrorType.UNEXPECTED_ERROR.toLowerCase():
|
|
130
144
|
default:
|
|
131
|
-
return
|
|
145
|
+
return null;
|
|
132
146
|
}
|
|
133
147
|
}
|
|
134
148
|
|