@saihu/common 1.1.62 → 1.1.63

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,6 +1,7 @@
1
1
  import { ArgumentsHost } from '@nestjs/common';
2
2
  import { BaseRpcExceptionFilter } from '@nestjs/microservices';
3
3
  export declare class BaseErrorRpcFilter extends BaseRpcExceptionFilter {
4
+ private readonly logger;
4
5
  catch(exception: any, host: ArgumentsHost): import("rxjs").Observable<any>;
5
6
  }
6
7
  //# sourceMappingURL=base-error-rpc.filter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"base-error-rpc.filter.d.ts","sourceRoot":"","sources":["../../src/filters/base-error-rpc.filter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAS,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,sBAAsB,EAAgB,MAAM,uBAAuB,CAAC;AAE7E,qBACa,kBAAmB,SAAQ,sBAAsB;IAC5D,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa;CA+B1C"}
1
+ {"version":3,"file":"base-error-rpc.filter.d.ts","sourceRoot":"","sources":["../../src/filters/base-error-rpc.filter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAiB,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,sBAAsB,EAAgB,MAAM,uBAAuB,CAAC;AAG7E,qBACa,kBAAmB,SAAQ,sBAAsB;IAC5D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;IAE9D,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa;CAwC1C"}
@@ -5,41 +5,56 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
7
  };
8
+ var BaseErrorRpcFilter_1;
8
9
  Object.defineProperty(exports, "__esModule", { value: true });
9
10
  exports.BaseErrorRpcFilter = void 0;
10
11
  // @saihu/common/src/filters/base-error-rpc.filter.ts
11
12
  const common_1 = require("@nestjs/common");
12
13
  const microservices_1 = require("@nestjs/microservices");
13
- let BaseErrorRpcFilter = class BaseErrorRpcFilter extends microservices_1.BaseRpcExceptionFilter {
14
+ const base_res_dto_1 = require("../dto/base-res.dto");
15
+ let BaseErrorRpcFilter = BaseErrorRpcFilter_1 = class BaseErrorRpcFilter extends microservices_1.BaseRpcExceptionFilter {
16
+ constructor() {
17
+ super(...arguments);
18
+ this.logger = new common_1.Logger(BaseErrorRpcFilter_1.name);
19
+ }
14
20
  catch(exception, host) {
15
21
  var _a;
16
- // Only transform for RPC
17
- if (host.getType() !== 'rpc') {
18
- return super.catch(exception, host);
19
- }
20
- // If it's already RpcException, pass through
22
+ // Skip if already an RpcException
21
23
  if (exception instanceof microservices_1.RpcException) {
22
24
  return super.catch(exception, host);
23
25
  }
24
- // Duck-type check for your BaseError shape
25
- const isBaseErrorLike = ((exception === null || exception === void 0 ? void 0 : exception.name) === 'BaseError' ||
26
- typeof (exception === null || exception === void 0 ? void 0 : exception.code) === 'number') &&
27
- typeof (exception === null || exception === void 0 ? void 0 : exception.message) === 'string';
28
- const payload = isBaseErrorLike
29
- ? {
30
- code: Number(exception.code) || 500,
26
+ // Check if it's a BaseError using duck-typing
27
+ // This is more reliable than instanceof for serialized errors
28
+ const isBaseErrorLike = exception &&
29
+ typeof exception === 'object' &&
30
+ exception.name === 'BaseError' &&
31
+ typeof exception.code === 'number' &&
32
+ typeof exception.message === 'string';
33
+ if (isBaseErrorLike || exception instanceof base_res_dto_1.BaseError) {
34
+ const payload = {
35
+ code: exception.code,
31
36
  msg: exception.message,
32
37
  data: (_a = exception.data) !== null && _a !== void 0 ? _a : null,
33
- }
34
- : {
35
- code: 500,
36
- msg: 'Internal server error',
37
- data: null,
38
38
  };
39
+ this.logger.log(`BaseError caught: [${payload.code}] ${payload.msg}`);
40
+ return super.catch(new microservices_1.RpcException(payload), host);
41
+ }
42
+ // Handle all other errors
43
+ this.logger.error('Unhandled RPC error:', {
44
+ name: exception === null || exception === void 0 ? void 0 : exception.name,
45
+ message: exception === null || exception === void 0 ? void 0 : exception.message,
46
+ code: exception === null || exception === void 0 ? void 0 : exception.code,
47
+ stack: exception === null || exception === void 0 ? void 0 : exception.stack,
48
+ });
49
+ const payload = {
50
+ code: 500,
51
+ msg: (exception === null || exception === void 0 ? void 0 : exception.message) || 'Internal server error',
52
+ data: null,
53
+ };
39
54
  return super.catch(new microservices_1.RpcException(payload), host);
40
55
  }
41
56
  };
42
57
  exports.BaseErrorRpcFilter = BaseErrorRpcFilter;
43
- exports.BaseErrorRpcFilter = BaseErrorRpcFilter = __decorate([
44
- (0, common_1.Catch)() // <-- catch everything (not just BaseError)
58
+ exports.BaseErrorRpcFilter = BaseErrorRpcFilter = BaseErrorRpcFilter_1 = __decorate([
59
+ (0, common_1.Catch)() // Catch everything
45
60
  ], BaseErrorRpcFilter);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saihu/common",
3
- "version": "1.1.62",
3
+ "version": "1.1.63",
4
4
  "description": "Common utilities for NestJS applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",