Skip to content

Commit 3e215c2

Browse files
fix: add gateway force error response status
1 parent b31f5a8 commit 3e215c2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

__tests__/services/gateway-test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe('services/gateway', () => {
4444
json: sinon.stub(),
4545
cookie: sinon.stub(),
4646
clearCookie: sinon.stub(),
47+
status: sinon.stub(),
4748
} as unknown as Response & { json: SinonStub; cookie: SinonStub; clearCookie: SinonStub });
4849

4950
const msName = 'ms1';
@@ -117,6 +118,7 @@ describe('services/gateway', () => {
117118
it('should return express error response', () => {
118119
const service = 'example';
119120
const req = { service } as unknown as IExpressRequest;
121+
const req2 = { service, forceStatus: true } as unknown as IExpressRequest;
120122
const res = createResponse();
121123
const name = 'error-name';
122124
const message = 'error-message';
@@ -125,9 +127,11 @@ describe('services/gateway', () => {
125127

126128
const case1 = { status: 1, code: 2, service: 'hi' };
127129
const case2 = { statusCode: 10 };
130+
const case3 = { status: 501 };
128131

129132
handleException({ ...case1, message, name }, req, res, next);
130133
handleException({ ...case2, message, name }, req, res, next);
134+
handleException({ ...case3, message, name }, req2, res, next);
131135
handleException({ message, name }, req, res, next);
132136

133137
const result1 = res.json.getCall(0).firstArg;
@@ -139,6 +143,8 @@ describe('services/gateway', () => {
139143
expect(result1.getError().toJSON().service).to.equal(case1.service);
140144
expect(result2.getError().toJSON().status).to.equal(case2.statusCode);
141145
expect(result3.getError().toJSON().service).to.equal(service);
146+
expect(res['status']).to.calledOnceWith(501);
147+
expect(res['json']).to.callCount(4);
142148
});
143149

144150
it('should correct start gateway microservice', async () => {

src/services/gateway.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,24 @@ class Gateway extends AbstractMicroservice {
127127
*/
128128
private static expressError(
129129
err: IHttpException,
130-
req: Request & { service?: string },
130+
req: Request & { service?: string; forceStatus?: boolean },
131131
res: Response,
132132
// not works without next function parameter
133133
// eslint-disable-next-line @typescript-eslint/no-unused-vars
134134
__: NextFunction,
135135
) {
136+
const status = err.status || err.statusCode || 500;
136137
const error = new BaseException({
137138
status: err.status || err.statusCode || 500,
138139
code: err.code || EXCEPTION_CODE.PARSE_ERROR,
139140
message: err.message,
140141
service: err.service ?? req.service,
141142
});
142143

144+
if (req.forceStatus) {
145+
res.status(status);
146+
}
147+
143148
res.json(new MicroserviceResponse({ error }));
144149
}
145150

0 commit comments

Comments
 (0)