@cryptexlabs/codex-nodejs-common 0.16.7 → 0.16.8
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/lib/package.json +1 -1
- package/lib/src/exception/friendly-http-exception.d.ts +1 -2
- package/lib/src/exception/friendly-http-exception.js +7 -2
- package/lib/src/exception/friendly-http-exception.js.map +1 -1
- package/package.json +1 -1
- package/src/exception/friendly-http-exception.spec.ts +99 -0
- package/src/exception/friendly-http-exception.ts +6 -1
package/lib/package.json
CHANGED
|
@@ -3,6 +3,5 @@ import { Context } from "../context";
|
|
|
3
3
|
export declare class FriendlyHttpException extends HttpException {
|
|
4
4
|
readonly context: Context;
|
|
5
5
|
readonly userMessage: string;
|
|
6
|
-
|
|
7
|
-
constructor(response: string | Record<string, any>, context: Context, userMessage: string, status: HttpStatus, stack: string);
|
|
6
|
+
constructor(response: string | Record<string, any>, context: Context, userMessage: string, status: HttpStatus, customStack?: string);
|
|
8
7
|
}
|
|
@@ -3,11 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FriendlyHttpException = void 0;
|
|
4
4
|
const common_1 = require("@nestjs/common");
|
|
5
5
|
class FriendlyHttpException extends common_1.HttpException {
|
|
6
|
-
constructor(response, context, userMessage, status,
|
|
6
|
+
constructor(response, context, userMessage, status, customStack) {
|
|
7
7
|
super(response, status);
|
|
8
8
|
this.context = context;
|
|
9
9
|
this.userMessage = userMessage;
|
|
10
|
-
|
|
10
|
+
if (customStack) {
|
|
11
|
+
this.stack = customStack;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
Error.captureStackTrace(this, this.constructor);
|
|
15
|
+
}
|
|
11
16
|
}
|
|
12
17
|
}
|
|
13
18
|
exports.FriendlyHttpException = FriendlyHttpException;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"friendly-http-exception.js","sourceRoot":"","sources":["../../../src/exception/friendly-http-exception.ts"],"names":[],"mappings":";;;AAAA,2CAA2D;AAG3D,MAAa,qBAAsB,SAAQ,sBAAa;IACtD,YACE,QAAsC,EACtB,OAAgB,EAChB,WAAmB,EACnC,MAAkB,
|
|
1
|
+
{"version":3,"file":"friendly-http-exception.js","sourceRoot":"","sources":["../../../src/exception/friendly-http-exception.ts"],"names":[],"mappings":";;;AAAA,2CAA2D;AAG3D,MAAa,qBAAsB,SAAQ,sBAAa;IACtD,YACE,QAAsC,EACtB,OAAgB,EAChB,WAAmB,EACnC,MAAkB,EAElB,WAAoB;QAEpB,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QANR,YAAO,GAAP,OAAO,CAAS;QAChB,gBAAW,GAAX,WAAW,CAAQ;QAMnC,IAAI,WAAW,EAAE,CAAC;YAChB,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC;QAC3B,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;CACF;AAhBD,sDAgBC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { FriendlyHttpException } from "./friendly-http-exception";
|
|
2
|
+
import { HttpStatus, LoggerService } from "@nestjs/common";
|
|
3
|
+
import { Context } from "../context";
|
|
4
|
+
import { Locale, MessageContextInterface } from "@cryptexlabs/codex-data-model";
|
|
5
|
+
import { DefaultConfig } from "../config";
|
|
6
|
+
import { instance, mock, when } from "ts-mockito";
|
|
7
|
+
|
|
8
|
+
describe("FriendlyHttpException", () => {
|
|
9
|
+
let mockContext: Context;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
const mockConfig = mock(DefaultConfig);
|
|
13
|
+
const config = instance(mockConfig);
|
|
14
|
+
when(mockConfig.logLevels).thenReturn(["info"]);
|
|
15
|
+
when(mockConfig.appName).thenReturn("test-app");
|
|
16
|
+
when(mockConfig.appVersion).thenReturn("1.0.0");
|
|
17
|
+
when(mockConfig.environmentName).thenReturn("test");
|
|
18
|
+
when(mockConfig.clientId).thenReturn("test-client");
|
|
19
|
+
|
|
20
|
+
const mockLogger = mock<LoggerService>();
|
|
21
|
+
const logger = instance(mockLogger);
|
|
22
|
+
|
|
23
|
+
const mockMessageContext = mock<MessageContextInterface>();
|
|
24
|
+
const messageContext = instance(mockMessageContext);
|
|
25
|
+
|
|
26
|
+
mockContext = new Context(
|
|
27
|
+
"test-correlation-id",
|
|
28
|
+
logger,
|
|
29
|
+
config,
|
|
30
|
+
{
|
|
31
|
+
id: "client-id",
|
|
32
|
+
name: "client-name",
|
|
33
|
+
version: "1.0.0",
|
|
34
|
+
variant: "test",
|
|
35
|
+
},
|
|
36
|
+
new Locale("en", "US"),
|
|
37
|
+
messageContext
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("stack trace handling", () => {
|
|
42
|
+
it("should capture stack trace automatically when no custom stack is provided", () => {
|
|
43
|
+
const exception = new FriendlyHttpException(
|
|
44
|
+
"Test error message",
|
|
45
|
+
mockContext,
|
|
46
|
+
"User friendly message",
|
|
47
|
+
HttpStatus.BAD_REQUEST
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
expect(exception.stack).toBeDefined();
|
|
51
|
+
expect(typeof exception.stack).toBe("string");
|
|
52
|
+
expect(exception.stack).toContain("FriendlyHttpException");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("should use custom stack when provided", () => {
|
|
56
|
+
const customStack = "CustomStack\nLine1\nLine2";
|
|
57
|
+
|
|
58
|
+
const exception = new FriendlyHttpException(
|
|
59
|
+
"Test error message",
|
|
60
|
+
mockContext,
|
|
61
|
+
"User friendly message",
|
|
62
|
+
HttpStatus.BAD_REQUEST,
|
|
63
|
+
customStack
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(exception.stack).toBe(customStack);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should have stack property accessible for logging", () => {
|
|
70
|
+
const exception = new FriendlyHttpException(
|
|
71
|
+
"Test error message",
|
|
72
|
+
mockContext,
|
|
73
|
+
"User friendly message",
|
|
74
|
+
HttpStatus.INTERNAL_SERVER_ERROR
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
expect(exception.stack).toBeDefined();
|
|
78
|
+
expect(exception.name).toBe("FriendlyHttpException");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should preserve other exception properties", () => {
|
|
82
|
+
const userMessage = "Something went wrong";
|
|
83
|
+
const response = "Developer error message";
|
|
84
|
+
const status = HttpStatus.NOT_FOUND;
|
|
85
|
+
|
|
86
|
+
const exception = new FriendlyHttpException(
|
|
87
|
+
response,
|
|
88
|
+
mockContext,
|
|
89
|
+
userMessage,
|
|
90
|
+
status
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(exception.userMessage).toBe(userMessage);
|
|
94
|
+
expect(exception.context).toBe(mockContext);
|
|
95
|
+
expect(exception.getStatus()).toBe(status);
|
|
96
|
+
expect(exception.message).toBe(response);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -8,8 +8,13 @@ export class FriendlyHttpException extends HttpException {
|
|
|
8
8
|
public readonly userMessage: string,
|
|
9
9
|
status: HttpStatus,
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
customStack?: string
|
|
12
12
|
) {
|
|
13
13
|
super(response, status);
|
|
14
|
+
if (customStack) {
|
|
15
|
+
this.stack = customStack;
|
|
16
|
+
} else {
|
|
17
|
+
Error.captureStackTrace(this, this.constructor);
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
20
|
}
|