response-standardizer 1.2.7 → 1.2.9
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/dist/index.d.ts +1 -0
- package/dist/index.js +31 -1
- package/package.json +1 -1
- package/src/index.ts +32 -8
package/dist/index.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export declare const info: (message: string, meta?: any) => void;
|
|
|
25
25
|
export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
|
|
26
26
|
export declare const handleRestException: (req: Request, res: Response, err: Error) => void;
|
|
27
27
|
export declare const handleRestResponse: (req: Request, res: Response, response: ServiceResponse) => void;
|
|
28
|
+
export declare const handleServiceException: (message: string, err: any) => ServiceResponse;
|
|
28
29
|
export declare const handleValidationException: (error: any) => never;
|
|
29
30
|
export declare class ServiceException extends Error {
|
|
30
31
|
status: number;
|
package/dist/index.js
CHANGED
|
@@ -3,11 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ServiceResponse = exports.ServiceException = exports.handleValidationException = exports.handleRestResponse = exports.handleRestException = exports.log = exports.info = exports.warn = exports.error = exports.RestMiddleware = exports.RestResponse = exports.protect = exports.initKeycloak = void 0;
|
|
6
|
+
exports.ServiceResponse = exports.ServiceException = exports.handleValidationException = exports.handleServiceException = exports.handleRestResponse = exports.handleRestException = exports.log = exports.info = exports.warn = exports.error = exports.RestMiddleware = exports.RestResponse = exports.protect = exports.initKeycloak = void 0;
|
|
7
7
|
const utils_1 = require("./utils");
|
|
8
8
|
const axios_1 = __importDefault(require("axios"));
|
|
9
9
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
10
10
|
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const zod_1 = require("zod");
|
|
11
12
|
let KEYCLOAK_PUBLIC_KEY = null;
|
|
12
13
|
const initKeycloak = async (config) => {
|
|
13
14
|
const KEYCLOAK_SERVICE = config.service ?? "localhost";
|
|
@@ -222,6 +223,35 @@ const handleRestResponse = (req, res, response) => {
|
|
|
222
223
|
return exports.RestResponse.exceptionError(req, res, response.message);
|
|
223
224
|
};
|
|
224
225
|
exports.handleRestResponse = handleRestResponse;
|
|
226
|
+
const handleServiceException = (message, err) => {
|
|
227
|
+
if (err instanceof zod_1.ZodError) {
|
|
228
|
+
const zodError = err;
|
|
229
|
+
if (zodError.issues) {
|
|
230
|
+
const validationErrors = {};
|
|
231
|
+
zodError.issues.forEach((i) => {
|
|
232
|
+
const key = i.path[0];
|
|
233
|
+
if (!validationErrors[key])
|
|
234
|
+
validationErrors[key] = [];
|
|
235
|
+
validationErrors[key].push(i.message);
|
|
236
|
+
});
|
|
237
|
+
return new ServiceResponse(400, "Validation error", validationErrors);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else if (err?.isAxiosError && err?.response && err.response.status) {
|
|
241
|
+
(0, exports.error)(message, {
|
|
242
|
+
status: err.response.status,
|
|
243
|
+
statusText: err.response.statusText,
|
|
244
|
+
error: err.response.data
|
|
245
|
+
});
|
|
246
|
+
return new ServiceResponse(err.response.status, err.response.data?.error_description);
|
|
247
|
+
}
|
|
248
|
+
else if (err instanceof Error) {
|
|
249
|
+
(0, exports.error)(message, { error: err });
|
|
250
|
+
return new ServiceResponse(500, err?.message);
|
|
251
|
+
}
|
|
252
|
+
return new ServiceResponse(500, "Internal server error");
|
|
253
|
+
};
|
|
254
|
+
exports.handleServiceException = handleServiceException;
|
|
225
255
|
const handleValidationException = (error) => {
|
|
226
256
|
const zodError = error;
|
|
227
257
|
if (zodError.issues) {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -262,17 +262,41 @@ export const handleRestResponse = (req: Request, res: Response, response: Servic
|
|
|
262
262
|
}
|
|
263
263
|
return RestResponse.exceptionError(req, res, response.message)
|
|
264
264
|
}
|
|
265
|
-
|
|
265
|
+
export const handleServiceException = (message: string, err: any) : ServiceResponse => {
|
|
266
|
+
if(err instanceof ZodError){
|
|
267
|
+
const zodError = err as ZodError;
|
|
268
|
+
if (zodError.issues) {
|
|
269
|
+
const validationErrors: any = {};
|
|
270
|
+
zodError.issues.forEach((i) => {
|
|
271
|
+
const key = i.path[0];
|
|
272
|
+
if (!validationErrors[key]) validationErrors[key] = [];
|
|
273
|
+
validationErrors[key].push(i.message);
|
|
274
|
+
});
|
|
275
|
+
return new ServiceResponse(400, "Validation error", validationErrors)
|
|
276
|
+
}
|
|
277
|
+
}else if((err as any)?.isAxiosError && (err as any)?.response && (err as any).response.status){
|
|
278
|
+
error(message, {
|
|
279
|
+
status: (err as any).response.status,
|
|
280
|
+
statusText: (err as any).response.statusText,
|
|
281
|
+
error: (err as any).response.data
|
|
282
|
+
})
|
|
283
|
+
return new ServiceResponse((err as any).response.status, (err as any).response.data?.error_description);
|
|
284
|
+
}else if(err instanceof Error){
|
|
285
|
+
error(message, { error: err })
|
|
286
|
+
return new ServiceResponse(500, err?.message);
|
|
287
|
+
}
|
|
288
|
+
return new ServiceResponse(500, "Internal server error");
|
|
289
|
+
}
|
|
266
290
|
export const handleValidationException = (error: any) => {
|
|
267
291
|
const zodError = error as ZodError;
|
|
268
292
|
if (zodError.issues) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
293
|
+
const validationErrors: any = {};
|
|
294
|
+
zodError.issues.forEach((i) => {
|
|
295
|
+
const key = i.path[0];
|
|
296
|
+
if (!validationErrors[key]) validationErrors[key] = [];
|
|
297
|
+
validationErrors[key].push(i.message);
|
|
298
|
+
});
|
|
299
|
+
throw new ServiceException("Validation error", 400, validationErrors);
|
|
276
300
|
}
|
|
277
301
|
throw new ServiceException("Internal server error", 500, error);
|
|
278
302
|
}
|