@saihu/common 1.1.93 → 1.1.95

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.
@@ -6,6 +6,11 @@ export declare class UnifiedExceptionFilter implements ExceptionFilter {
6
6
  catch(exception: any, host: ArgumentsHost): void;
7
7
  private mapAppErrorCodeToHttpStatus;
8
8
  private logError;
9
+ /**
10
+ * Determine if an error is "expected" (business logic, validation)
11
+ * vs "unexpected" (infrastructure failures, bugs)
12
+ */
13
+ private isExpectedError;
9
14
  /**
10
15
  * Recursively sanitize sensitive information from details object
11
16
  * Handles nested objects like { headers: { authorization: "..." } }
@@ -1 +1 @@
1
- {"version":3,"file":"unified-exception.filter.d.ts","sourceRoot":"","sources":["../../src/errors/unified-exception.filter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAEf,aAAa,EAGd,MAAM,gBAAgB,CAAC;AAOxB,qBACa,sBAAuB,YAAW,eAAe;IAC5D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2C;IAElE,OAAO,CAAC,QAAQ,CAAC,cAAc,CAM7B;IACF,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAQ;IAE3C,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa;IA+CzC,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,QAAQ;IA2BhB;;;OAGG;IACH,OAAO,CAAC,eAAe;IAUvB;;OAEG;IACH,OAAO,CAAC,cAAc;IAmCtB;;;OAGG;IACH,OAAO,CAAC,cAAc;CAQvB"}
1
+ {"version":3,"file":"unified-exception.filter.d.ts","sourceRoot":"","sources":["../../src/errors/unified-exception.filter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAEf,aAAa,EAGd,MAAM,gBAAgB,CAAC;AAQxB,qBACa,sBAAuB,YAAW,eAAe;IAC5D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2C;IAElE,OAAO,CAAC,QAAQ,CAAC,cAAc,CAM7B;IACF,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAQ;IAE3C,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa;IA+CzC,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,QAAQ;IAiChB;;;OAGG;IACH,OAAO,CAAC,eAAe;IAoBvB;;;OAGG;IACH,OAAO,CAAC,eAAe;IAUvB;;OAEG;IACH,OAAO,CAAC,cAAc;IAmCtB;;;OAGG;IACH,OAAO,CAAC,cAAc;CAQvB"}
@@ -13,6 +13,7 @@ const domain_error_1 = require("./domain.error");
13
13
  const application_error_1 = require("./application.error");
14
14
  const infrastructure_error_1 = require("./infrastructure.error");
15
15
  const dto_1 = require("../dto");
16
+ const times_1 = require("../util/times");
16
17
  let UnifiedExceptionFilter = UnifiedExceptionFilter_1 = class UnifiedExceptionFilter {
17
18
  constructor() {
18
19
  this.logger = new common_1.Logger(UnifiedExceptionFilter_1.name);
@@ -66,7 +67,7 @@ let UnifiedExceptionFilter = UnifiedExceptionFilter_1 = class UnifiedExceptionFi
66
67
  errorResponse.errorCode = errorCode;
67
68
  if (details)
68
69
  errorResponse.details = this.sanitizeDetails(details);
69
- errorResponse.timestamp = new Date().toISOString();
70
+ errorResponse.timestamp = (0, times_1.getCurrentChinaTime)().toISOString();
70
71
  response.status(statusCode).json(errorResponse);
71
72
  }
72
73
  mapAppErrorCodeToHttpStatus(code) {
@@ -91,9 +92,36 @@ let UnifiedExceptionFilter = UnifiedExceptionFilter_1 = class UnifiedExceptionFi
91
92
  ]
92
93
  .filter(Boolean)
93
94
  .join(' | ');
94
- this.logger.error(logMessage, exception.stack);
95
- if (cause === null || cause === void 0 ? void 0 : cause.stack)
96
- this.logger.error(`Caused by: ${cause.stack}`);
95
+ // Expected errors (business/validation) → warn, no stack
96
+ // Unexpected errors (infra/unknown) error with stack
97
+ if (this.isExpectedError(exception, code)) {
98
+ this.logger.warn(logMessage);
99
+ }
100
+ else {
101
+ this.logger.error(logMessage, exception.stack);
102
+ if (cause === null || cause === void 0 ? void 0 : cause.stack)
103
+ this.logger.error(`Caused by: ${cause.stack}`);
104
+ }
105
+ }
106
+ /**
107
+ * Determine if an error is "expected" (business logic, validation)
108
+ * vs "unexpected" (infrastructure failures, bugs)
109
+ */
110
+ isExpectedError(exception, code) {
111
+ // Domain errors are always expected (business rule violations)
112
+ if (exception instanceof domain_error_1.DomainError)
113
+ return true;
114
+ // Application errors with these codes are expected
115
+ const expectedCodes = [
116
+ application_error_1.ErrorCodes.RESOURCE_NOT_FOUND,
117
+ application_error_1.ErrorCodes.INVALID_INPUT,
118
+ ];
119
+ if (exception instanceof application_error_1.ApplicationError &&
120
+ expectedCodes.includes(code)) {
121
+ return true;
122
+ }
123
+ // Infrastructure errors and unknown errors are unexpected
124
+ return false;
97
125
  }
98
126
  /**
99
127
  * Recursively sanitize sensitive information from details object
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saihu/common",
3
- "version": "1.1.93",
3
+ "version": "1.1.95",
4
4
  "description": "Common utilities for NestJS applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",