@open-norantec/utilities 1.0.1-alpha.17 → 1.0.1-alpha.18

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.
@@ -1,3 +1,16 @@
1
+ declare class ResponseError extends Error {
2
+ readonly statusCode: number;
3
+ readonly status: number;
4
+ readonly code: number;
5
+ constructor(message?: string | undefined, statusCode?: number, status?: number, code?: number);
6
+ }
1
7
  export declare class ErrorUtil {
2
- static getErrorMessage(error: Error): string;
8
+ static createResponseError(error: Error, statusCode?: number): ResponseError;
9
+ static formatError(error: Error): {
10
+ id: string;
11
+ message: string;
12
+ logs: string[];
13
+ response: Response;
14
+ };
3
15
  }
16
+ export {};
@@ -1,18 +1,59 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ErrorUtil = void 0;
4
- const zod_1 = require("zod");
4
+ const uuid_util_class_1 = require("./uuid-util.class");
5
+ function isZodError(error) {
6
+ return typeof error === 'object' && error !== null && 'issues' in error && Array.isArray(error.issues);
7
+ }
8
+ class ResponseError extends Error {
9
+ constructor(message, statusCode = 500, status = 500, code = 500) {
10
+ super(message);
11
+ this.statusCode = statusCode;
12
+ this.status = status;
13
+ this.code = code;
14
+ }
15
+ }
5
16
  class ErrorUtil {
6
- static getErrorMessage(error) {
7
- if (error instanceof zod_1.ZodError) {
8
- try {
9
- return `Parameters failed to pass validator rules: ${error.errors.map((subError) => subError.path.join('.')).join(', ')}`;
10
- }
11
- catch (_a) {
12
- return 'Parameters validation error';
13
- }
17
+ static createResponseError(error, statusCode = 500) {
18
+ return new ResponseError(error.message, statusCode, statusCode, statusCode);
19
+ }
20
+ static formatError(error) {
21
+ const id = uuid_util_class_1.UUIDUtil.generateV4();
22
+ let message;
23
+ let errorLogs = [];
24
+ if (isZodError(error)) {
25
+ message = JSON.stringify({
26
+ message: 'Failed to validate your input data ({{context.trace}})',
27
+ context: {
28
+ trace: id,
29
+ },
30
+ });
31
+ errorLogs.push(JSON.stringify(error.issues));
32
+ }
33
+ else {
34
+ message = JSON.stringify({
35
+ message: `${error.message || 'An unknown error occurred'} ({{context.trace}})`,
36
+ context: {
37
+ trace: id,
38
+ },
39
+ });
14
40
  }
15
- return (error === null || error === void 0 ? void 0 : error.message) || 'An unknown error occurred';
41
+ errorLogs.push((error === null || error === void 0 ? void 0 : error.stack) || 'No stack trace available');
42
+ return {
43
+ id,
44
+ message,
45
+ logs: errorLogs.map((log) => `[AUTO_ERROR] [${id}] [${new Date().toISOString()}] ${log}`),
46
+ response: new Response(message, {
47
+ status: (() => {
48
+ if (isZodError(error))
49
+ return 400;
50
+ if (error instanceof ResponseError)
51
+ return error.statusCode;
52
+ return 500;
53
+ })(),
54
+ headers: { 'Content-Type': 'application/json' },
55
+ }),
56
+ };
16
57
  }
17
58
  }
18
59
  exports.ErrorUtil = ErrorUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/utilities",
3
- "version": "1.0.1-alpha.17",
3
+ "version": "1.0.1-alpha.18",
4
4
  "description": "NoranTec Utilities",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {