@saihu/common 1.1.65 → 1.1.66
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/dist/interceptors/index.d.ts +1 -0
- package/dist/interceptors/index.d.ts.map +1 -1
- package/dist/interceptors/index.js +1 -0
- package/dist/interceptors/rpc-error.interceptor.d.ts +7 -0
- package/dist/interceptors/rpc-error.interceptor.d.ts.map +1 -0
- package/dist/interceptors/rpc-error.interceptor.js +59 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interceptors/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interceptors/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC"}
|
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./transform.interceptor"), exports);
|
|
18
18
|
__exportStar(require("./operlog.interceptor"), exports);
|
|
19
|
+
__exportStar(require("./rpc-error.interceptor"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
export declare class RpcErrorInterceptor implements NestInterceptor {
|
|
4
|
+
private readonly logger;
|
|
5
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=rpc-error.interceptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-error.interceptor.d.ts","sourceRoot":"","sources":["../../src/interceptors/rpc-error.interceptor.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,eAAe,EACf,gBAAgB,EAChB,WAAW,EAEZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,UAAU,EAAc,MAAM,MAAM,CAAC;AAK9C,qBACa,mBAAoB,YAAW,eAAe;IACzD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwC;IAE/D,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC;CA4CzE"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
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
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var RpcErrorInterceptor_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.RpcErrorInterceptor = void 0;
|
|
11
|
+
// @saihu/common/src/interceptors/rpc-error.interceptor.ts
|
|
12
|
+
const common_1 = require("@nestjs/common");
|
|
13
|
+
const rxjs_1 = require("rxjs");
|
|
14
|
+
const operators_1 = require("rxjs/operators");
|
|
15
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
16
|
+
const base_res_dto_1 = require("../dto/base-res.dto");
|
|
17
|
+
let RpcErrorInterceptor = RpcErrorInterceptor_1 = class RpcErrorInterceptor {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.logger = new common_1.Logger(RpcErrorInterceptor_1.name);
|
|
20
|
+
}
|
|
21
|
+
intercept(context, next) {
|
|
22
|
+
// Only intercept RPC context
|
|
23
|
+
if (context.getType() !== 'rpc') {
|
|
24
|
+
return next.handle();
|
|
25
|
+
}
|
|
26
|
+
return next.handle().pipe((0, operators_1.catchError)((error) => {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
this.logger.debug('RPC error caught by interceptor:', {
|
|
29
|
+
type: (_a = error === null || error === void 0 ? void 0 : error.constructor) === null || _a === void 0 ? void 0 : _a.name,
|
|
30
|
+
message: error === null || error === void 0 ? void 0 : error.message,
|
|
31
|
+
});
|
|
32
|
+
// Transform BaseError to RpcException
|
|
33
|
+
if (error instanceof base_res_dto_1.BaseError) {
|
|
34
|
+
const payload = {
|
|
35
|
+
code: error.code,
|
|
36
|
+
msg: error.message,
|
|
37
|
+
data: (_b = error.data) !== null && _b !== void 0 ? _b : null,
|
|
38
|
+
};
|
|
39
|
+
this.logger.warn(`BaseError transformed: [${payload.code}] ${payload.msg}`);
|
|
40
|
+
return (0, rxjs_1.throwError)(() => new microservices_1.RpcException(payload));
|
|
41
|
+
}
|
|
42
|
+
// Already an RpcException, pass through
|
|
43
|
+
if (error instanceof microservices_1.RpcException) {
|
|
44
|
+
return (0, rxjs_1.throwError)(() => error);
|
|
45
|
+
}
|
|
46
|
+
// Transform other errors to generic RpcException
|
|
47
|
+
this.logger.error('Unhandled RPC error:', error);
|
|
48
|
+
return (0, rxjs_1.throwError)(() => new microservices_1.RpcException({
|
|
49
|
+
code: 500,
|
|
50
|
+
msg: (error === null || error === void 0 ? void 0 : error.message) || 'Internal server error',
|
|
51
|
+
data: null,
|
|
52
|
+
}));
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.RpcErrorInterceptor = RpcErrorInterceptor;
|
|
57
|
+
exports.RpcErrorInterceptor = RpcErrorInterceptor = RpcErrorInterceptor_1 = __decorate([
|
|
58
|
+
(0, common_1.Injectable)()
|
|
59
|
+
], RpcErrorInterceptor);
|