Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 502 for gateway validation errors #2111

Merged
merged 7 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function moduleTransactionBuilder(): IBuilder<ModuleTransaction> {
.with('safe', getAddress(faker.finance.ethereumAddress()))
.with('to', getAddress(faker.finance.ethereumAddress()))
.with('transactionHash', faker.string.hexadecimal() as `0x${string}`)
.with('value', faker.string.hexadecimal())
.with('value', faker.string.numeric())
.with('moduleTransactionId', faker.string.sample());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export function multisigTransactionBuilder(): IBuilder<MultisigTransaction> {
.with('confirmationsRequired', faker.number.int())
.with('data', faker.string.hexadecimal() as `0x${string}`)
.with('dataDecoded', dataDecodedBuilder().build())
.with('ethGasPrice', faker.string.hexadecimal())
.with('ethGasPrice', faker.string.numeric())
.with('executor', getAddress(faker.finance.ethereumAddress()))
.with('executionDate', faker.date.recent())
.with('fee', faker.string.hexadecimal())
.with('gasPrice', faker.string.hexadecimal())
.with('fee', faker.string.numeric())
.with('gasPrice', faker.string.numeric())
.with('gasToken', getAddress(faker.finance.ethereumAddress()))
.with('gasUsed', faker.number.int())
.with('isExecuted', faker.datatype.boolean())
Expand Down
8 changes: 4 additions & 4 deletions src/routes/balances/balances.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ describe('Balances Controller (Unit)', () => {
});
});

it(`500 error if validation fails`, async () => {
it(`502 error if validation fails`, async () => {
const chainId = '1';
const safeAddress = getAddress(faker.finance.ethereumAddress());
const chainResponse = chainBuilder().with('chainId', chainId).build();
Expand Down Expand Up @@ -830,10 +830,10 @@ describe('Balances Controller (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/chains/${chainId}/safes/${safeAddress}/balances/usd`)
.expect(500)
.expect(502)
.expect({
statusCode: 500,
message: 'Internal server error',
statusCode: 502,
message: 'Bad gateway',
});

expect(networkService.get.mock.calls.length).toBe(4);
Expand Down
29 changes: 10 additions & 19 deletions src/routes/chains/chains.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ describe('Chains Controller (Unit)', () => {
status: 200,
});

await request(app.getHttpServer()).get('/v1/chains').expect(500).expect({
statusCode: 500,
message: 'Internal server error',
});
await request(app.getHttpServer())
.get('/v1/chains')
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
expect(networkService.get).toHaveBeenCalledTimes(1);
expect(networkService.get).toHaveBeenCalledWith({
url: `${safeConfigUrl}/api/v1/chains`,
Expand Down Expand Up @@ -439,11 +439,8 @@ describe('Chains Controller (Unit)', () => {

await request(app.getHttpServer())
.get('/v1/chains/1/about/backbone')
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });

expect(networkService.get).toHaveBeenCalledTimes(2);
expect(networkService.get.mock.calls[0][0].url).toBe(
Expand Down Expand Up @@ -628,11 +625,8 @@ describe('Chains Controller (Unit)', () => {

await request(app.getHttpServer())
.get('/v1/chains/1/about/master-copies')
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});
});

Expand Down Expand Up @@ -761,11 +755,8 @@ describe('Chains Controller (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/chains/${chainResponse.chainId}/about/indexing`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});
});

Expand Down
23 changes: 19 additions & 4 deletions src/routes/common/filters/zod-error.filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class TestController {
return body;
}

@Post('zod-standard')
zodStandardError(): number {
return z.number().parse('string');
}

@Get('non-zod-exception')
nonZodException(): void {
throw new Error('Some random error');
Expand Down Expand Up @@ -84,7 +89,7 @@ describe('ZodErrorFilter tests', () => {
await app.close();
});

it('ZodError exception returns first issue', async () => {
it('ZodErrorWithCode exception returns first issue', async () => {
await request(app.getHttpServer())
.post('/zod-exception')
.send({ value: faker.number.int() })
Expand All @@ -99,7 +104,7 @@ describe('ZodErrorFilter tests', () => {
});
});

it('ZodError union exception returns first issue of first union issue', async () => {
it('ZodErrorWithCode union exception returns first issue of first union issue', async () => {
await request(app.getHttpServer())
.post('/zod-union-exception')
.send({ value: faker.datatype.boolean() })
Expand All @@ -114,7 +119,7 @@ describe('ZodErrorFilter tests', () => {
});
});

it('ZodError nested union exception returns first issue of nested union issue', async () => {
it('ZodErrorWithCode nested union exception returns first issue of nested union issue', async () => {
await request(app.getHttpServer())
.post('/zod-union-exception')
.send({
Expand All @@ -133,7 +138,17 @@ describe('ZodErrorFilter tests', () => {
});
});

it('non-ZodError exception returns correct error code and message', async () => {
it('ZodError returns 502', async () => {
await request(app.getHttpServer())
.post('/zod-standard')
.expect(502)
.expect({
statusCode: 502,
message: 'Bad gateway',
});
});

it('non-ZodError/ZodErrorWithCode exception returns correct error code and message', async () => {
await request(app.getHttpServer())
.get('/non-zod-exception')
.expect(500)
Expand Down
8 changes: 4 additions & 4 deletions src/routes/common/filters/zod-error.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ZodErrorWithCode } from '@/validation/pipes/validation.pipe';
*
* It builds a JSON payload which contains a code and a message.
* The message is read from the initial {@link ZodIssue} and the code
* from {@link ZodErrorWithCode.code} or 500 if {@link ZodError}.
* from {@link ZodErrorWithCode.code} or 502 if {@link ZodError}.
*/
@Catch(ZodError, ZodErrorWithCode)
export class ZodErrorFilter implements ExceptionFilter {
Expand All @@ -32,9 +32,9 @@ export class ZodErrorFilter implements ExceptionFilter {
});
} else {
// Don't expose validation as it may contain sensitive data
response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message: 'Internal server error',
response.status(HttpStatus.BAD_GATEWAY).json({
statusCode: HttpStatus.BAD_GATEWAY,
message: 'Bad gateway',
});
}
}
Expand Down
56 changes: 16 additions & 40 deletions src/routes/community/community.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,8 @@ describe('Community (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/community/campaigns`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward the pagination parameters', async () => {
Expand Down Expand Up @@ -246,11 +243,8 @@ describe('Community (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/community/campaigns/${invalidCampaign.resourceId}`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward an error from the service', async () => {
Expand Down Expand Up @@ -448,11 +442,8 @@ describe('Community (Unit)', () => {
.get(
`/v1/community/campaigns/${campaign.resourceId}/activities?holder=${holder}`,
)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward an error from the service', () => {
Expand Down Expand Up @@ -545,11 +536,8 @@ describe('Community (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/community/campaigns/${campaign.resourceId}/leaderboard`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward the pagination parameters', async () => {
Expand Down Expand Up @@ -680,11 +668,8 @@ describe('Community (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/community/campaigns/${resourceId}/leaderboard/${safeAddress}`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward an error from the service', async () => {
Expand Down Expand Up @@ -815,11 +800,8 @@ describe('Community (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/community/locking/leaderboard`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward an error from the service', async () => {
Expand Down Expand Up @@ -900,11 +882,8 @@ describe('Community (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/community/locking/${safeAddress}/rank`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward an error from the service', async () => {
Expand Down Expand Up @@ -1068,11 +1047,8 @@ describe('Community (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/community/locking/${safeAddress}/history`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should forward an error from the service', async () => {
Expand Down
7 changes: 2 additions & 5 deletions src/routes/contracts/contracts.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,8 @@ describe('Contracts controller', () => {

await request(app.getHttpServer())
.get(`/v1/chains/${chain.chainId}/contracts/${contract.address}`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});
});
});
7 changes: 2 additions & 5 deletions src/routes/delegates/delegates.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ describe('Delegates controller', () => {

await request(app.getHttpServer())
.get(`/v1/chains/${chain.chainId}/delegates?safe=${safe}`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('Should return empty result', async () => {
Expand Down
7 changes: 2 additions & 5 deletions src/routes/delegates/v2/delegates.v2.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,8 @@ describe('Delegates controller', () => {

await request(app.getHttpServer())
.get(`/v2/chains/${chain.chainId}/delegates?safe=${safe}`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should return empty result', async () => {
Expand Down
7 changes: 2 additions & 5 deletions src/routes/estimations/estimations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,8 @@ describe('Estimations Controller (Unit)', () => {
data: faker.string.hexadecimal({ length: 32 }),
operation: 0,
})
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('Should get a validation error', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/messages/messages.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ describe('Messages controller', () => {

await request(app.getHttpServer())
.get(`/v1/chains/${chain.chainId}/safes/${safe.address}/messages`)
.expect(500)
.expect({ statusCode: 500, message: 'Internal server error' });
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});

it('should get a message with a date label', async () => {
Expand Down
14 changes: 4 additions & 10 deletions src/routes/owners/owners.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,8 @@ describe('Owners Controller (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/chains/${chainId}/owners/${ownerAddress}/safes`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});
});

Expand Down Expand Up @@ -445,11 +442,8 @@ describe('Owners Controller (Unit)', () => {

await request(app.getHttpServer())
.get(`/v1/owners/${ownerAddress}/safes`)
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});
.expect(502)
.expect({ statusCode: 502, message: 'Bad gateway' });
});
});
});
Loading
Loading