sm-utility 2.4.13 → 2.4.15
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.
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AxiosError } from '../request';
|
|
2
|
+
export declare const REQUEST_ERRORS: {
|
|
3
|
+
TIMEOUT: string;
|
|
4
|
+
NOT_FOUND: string;
|
|
5
|
+
UNAUTHORIZED: string;
|
|
6
|
+
UNEXPECTED: string;
|
|
7
|
+
BAD_REQUEST: string;
|
|
8
|
+
API_TIMEOUT: string;
|
|
9
|
+
HUB_TIMEOUT: string;
|
|
10
|
+
};
|
|
11
|
+
export interface AxiosErrorHolder {
|
|
12
|
+
error: Error;
|
|
13
|
+
message: string | undefined;
|
|
14
|
+
type: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
export declare class AxiosErrHandler {
|
|
17
|
+
static extract(error: AxiosError): AxiosErrorHolder;
|
|
18
|
+
}
|
|
19
|
+
export declare class InternalTimeoutError implements AxiosErrorHolder {
|
|
20
|
+
error: Error;
|
|
21
|
+
message: string | undefined;
|
|
22
|
+
type: string;
|
|
23
|
+
constructor(error: Error, message: string);
|
|
24
|
+
}
|
|
25
|
+
export declare class ExternalTimeoutError implements AxiosErrorHolder {
|
|
26
|
+
error: Error;
|
|
27
|
+
message: string | undefined;
|
|
28
|
+
type: string;
|
|
29
|
+
constructor(error: Error, message: string | undefined);
|
|
30
|
+
}
|
|
31
|
+
export declare class UnknownError implements AxiosErrorHolder {
|
|
32
|
+
error: Error;
|
|
33
|
+
message: string | undefined;
|
|
34
|
+
type: string | undefined;
|
|
35
|
+
constructor(error: Error, message: string | undefined, type: string | undefined);
|
|
36
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnknownError = exports.ExternalTimeoutError = exports.InternalTimeoutError = exports.AxiosErrHandler = void 0;
|
|
4
|
+
class AxiosErrorWrapper {
|
|
5
|
+
constructor(error) {
|
|
6
|
+
this.error = error;
|
|
7
|
+
}
|
|
8
|
+
isInternalTimeout() {
|
|
9
|
+
if (!this.error.response) {
|
|
10
|
+
return (this.error.code === 'ETIMEDOUT' || this.error.code === 'ECONNABORTED');
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
isExternalTimeout() {
|
|
15
|
+
if (this.error.response) {
|
|
16
|
+
return (this.error.response.status === 504 || this.error.response.status === 408);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
getResponseData() {
|
|
21
|
+
if (this.error.response) {
|
|
22
|
+
return this.error.response.data;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class AxiosErrHandler {
|
|
28
|
+
static extract(error) {
|
|
29
|
+
const err = new AxiosErrorWrapper(error);
|
|
30
|
+
const httpResponse = err.getResponseData();
|
|
31
|
+
if (!httpResponse) {
|
|
32
|
+
if (err.isInternalTimeout()) {
|
|
33
|
+
return new InternalTimeoutError(error, error.message);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (err.isExternalTimeout()) {
|
|
37
|
+
return new ExternalTimeoutError(error, httpResponse === null || httpResponse === void 0 ? void 0 : httpResponse.message);
|
|
38
|
+
}
|
|
39
|
+
return new UnknownError(error, httpResponse === null || httpResponse === void 0 ? void 0 : httpResponse.message, httpResponse === null || httpResponse === void 0 ? void 0 : httpResponse.status);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.AxiosErrHandler = AxiosErrHandler;
|
|
43
|
+
class InternalTimeoutError {
|
|
44
|
+
constructor(error, message) {
|
|
45
|
+
this.error = error;
|
|
46
|
+
this.message = message || error.message;
|
|
47
|
+
this.type = exports.REQUEST_ERRORS.API_TIMEOUT;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.InternalTimeoutError = InternalTimeoutError;
|
|
51
|
+
class ExternalTimeoutError {
|
|
52
|
+
constructor(error, message) {
|
|
53
|
+
this.error = error;
|
|
54
|
+
this.message = message || error.message;
|
|
55
|
+
this.type = exports.REQUEST_ERRORS.HUB_TIMEOUT;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.ExternalTimeoutError = ExternalTimeoutError;
|
|
59
|
+
class UnknownError {
|
|
60
|
+
constructor(error, message, type) {
|
|
61
|
+
this.error = error;
|
|
62
|
+
this.message = message || error.message;
|
|
63
|
+
this.type = type || 'UNKNOWN';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.UnknownError = UnknownError;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LogMethod = void 0;
|
|
4
|
-
const logger_1 = require("
|
|
4
|
+
const logger_1 = require("../../logger");
|
|
5
|
+
const axios_1 = require("axios");
|
|
6
|
+
const axiosErrors_1 = require("../../errors/axiosErrors");
|
|
5
7
|
/**
|
|
6
8
|
* Decorator that logs the execution of a method.
|
|
7
9
|
*
|
|
@@ -22,7 +24,7 @@ function LogMethod(options) {
|
|
|
22
24
|
return function (target, propertyKey, descriptor) {
|
|
23
25
|
const originalMethod = descriptor.value;
|
|
24
26
|
descriptor.value = async function (...args) {
|
|
25
|
-
var _a
|
|
27
|
+
var _a;
|
|
26
28
|
const className = target.constructor.name;
|
|
27
29
|
const params = (options === null || options === void 0 ? void 0 : options.logParams) ? args : undefined;
|
|
28
30
|
const metadata = (_a = this.metadata) === null || _a === void 0 ? void 0 : _a.getData();
|
|
@@ -34,6 +36,10 @@ function LogMethod(options) {
|
|
|
34
36
|
},
|
|
35
37
|
});
|
|
36
38
|
const result = await originalMethod.apply(this, args);
|
|
39
|
+
if (result.error) {
|
|
40
|
+
handleError(result.error, className, propertyKey, metadata, params);
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
37
43
|
logger_1.logger.info(`${className}.${propertyKey} Finished`, {
|
|
38
44
|
metadata,
|
|
39
45
|
context: {
|
|
@@ -44,33 +50,53 @@ function LogMethod(options) {
|
|
|
44
50
|
return result;
|
|
45
51
|
}
|
|
46
52
|
catch (error) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
!line.includes('logMethod.decorator.ts') &&
|
|
51
|
-
!line.includes('node:internal/process/task_queues')).join('\n');
|
|
52
|
-
logger_1.logger.error(`${className}.${propertyKey} Failed`, {
|
|
53
|
-
err: { message: error.message, stack: cleanStack },
|
|
54
|
-
metadata,
|
|
55
|
-
context: {
|
|
56
|
-
params,
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
// Caso o erro não seja uma instância de Error, logamos como um objeto desconhecido
|
|
62
|
-
logger_1.logger.error(`${className}.${propertyKey} Failed`, {
|
|
63
|
-
err: { message: 'Unknown error', details: error },
|
|
64
|
-
metadata,
|
|
65
|
-
context: {
|
|
66
|
-
params,
|
|
67
|
-
},
|
|
68
|
-
});
|
|
53
|
+
const errorToThrow = handleError(error, className, propertyKey, metadata, params);
|
|
54
|
+
if ((options === null || options === void 0 ? void 0 : options.throwError) === undefined || (options === null || options === void 0 ? void 0 : options.throwError) === true) {
|
|
55
|
+
throw errorToThrow;
|
|
69
56
|
}
|
|
70
|
-
throw error;
|
|
71
57
|
}
|
|
72
58
|
};
|
|
73
59
|
return descriptor;
|
|
74
60
|
};
|
|
75
61
|
}
|
|
76
62
|
exports.LogMethod = LogMethod;
|
|
63
|
+
function handleError(error, className, propertyKey, metadata, params) {
|
|
64
|
+
if ((0, axios_1.isAxiosError)(error)) {
|
|
65
|
+
const fullErr = axiosErrors_1.AxiosErrHandler.extract(error);
|
|
66
|
+
const cleanStack = cleanErrorStack(fullErr.error.stack);
|
|
67
|
+
logger_1.logger.error(`${className}.${propertyKey} Failed`, {
|
|
68
|
+
err: { message: error.message, stack: cleanStack },
|
|
69
|
+
metadata,
|
|
70
|
+
context: {
|
|
71
|
+
params,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
return fullErr;
|
|
75
|
+
}
|
|
76
|
+
if (error instanceof Error) {
|
|
77
|
+
const cleanStack = cleanErrorStack(error.stack);
|
|
78
|
+
logger_1.logger.error(`${className}.${propertyKey} Failed`, {
|
|
79
|
+
err: { message: error.message, stack: cleanStack },
|
|
80
|
+
metadata,
|
|
81
|
+
context: {
|
|
82
|
+
params,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
return error;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
logger_1.logger.error(`${className}.${propertyKey} Failed`, {
|
|
89
|
+
err: { message: 'Unknown error', details: error },
|
|
90
|
+
metadata,
|
|
91
|
+
context: {
|
|
92
|
+
params,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
return error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function cleanErrorStack(stack) {
|
|
99
|
+
return stack === null || stack === void 0 ? void 0 : stack.split('\n').filter((line) => !line.includes('descriptor.value') &&
|
|
100
|
+
!line.includes('logMethod.decorator.ts') &&
|
|
101
|
+
!line.includes('node:internal/process/task_queues')).join('\n');
|
|
102
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sm-utility",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.15",
|
|
4
4
|
"description": "reusable utility codes for sm projects",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"@opentelemetry/sdk-node": "0.57.1",
|
|
37
37
|
"@opentelemetry/sdk-trace-base": "1.30.1",
|
|
38
38
|
"aws-sdk": "^2.1134.0",
|
|
39
|
-
"axios": "
|
|
39
|
+
"axios": "1.6.8",
|
|
40
40
|
"express-winston": "^4.1.0",
|
|
41
41
|
"nanoid": "^3.3.6",
|
|
42
42
|
"node-xlsx": "^0.24.0",
|