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

fix(core): make isHttpError check stricter #14753

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 20 additions & 2 deletions packages/core/exceptions/base-exception-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,28 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
}

/**
* Checks if the thrown error comes from the "http-errors" library.
* Checks if the thrown error is a FastifyError or comes from the "http-errors" library.
* @param err error object
*/
public isHttpError(err: any): err is { statusCode: number; message: string } {
return err?.statusCode && err?.message;
if (!err || typeof err !== 'object') {
return false;
}

if (
err.constructor.name === 'FastifyError' &&
typeof err.code === 'string' &&
typeof err.statusCode === 'number'
) {
return true;
}

// "http-errors" error signature
return (
typeof err.expose === 'boolean' &&
typeof err.statusCode === 'number' &&
err.status === err.statusCode &&
err instanceof Error
);
}
}
33 changes: 33 additions & 0 deletions packages/core/test/exceptions/exceptions-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isNil, isObject } from '@nestjs/common/utils/shared.utils';
import { expect } from 'chai';
import * as createHttpError from 'http-errors';
import * as sinon from 'sinon';
import fastifyErrors from '@fastify/error';
import { AbstractHttpAdapter } from '../../adapters';
import { InvalidExceptionFilterException } from '../../errors/exceptions/invalid-exception-filter.exception';
import { ExceptionsHandler } from '../../exceptions/exceptions-handler';
Expand Down Expand Up @@ -57,6 +58,38 @@ describe('ExceptionsHandler', () => {
}),
).to.be.true;
});
it('should treat fastify errors as http errors', () => {
const fastifyError = fastifyErrors.createError(
'FST_ERR_CTP_EMPTY_JSON_BODY',
"Body cannot be empty when content-type is set to 'application/json'",
400,
)();
handler.next(fastifyError, new ExecutionContextHost([0, response]));

expect(statusStub.calledWith(400)).to.be.true;
expect(
jsonStub.calledWith({
statusCode: 400,
message:
"Body cannot be empty when content-type is set to 'application/json'",
}),
).to.be.true;
});
it('should not treat errors from external API calls as errors from "http-errors" library', () => {
const apiCallError = Object.assign(
new Error('Some external API call failed'),
{ status: 400 },
);
handler.next(apiCallError, new ExecutionContextHost([0, response]));

expect(statusStub.calledWith(500)).to.be.true;
expect(
jsonStub.calledWith({
statusCode: 500,
message: 'Internal server error',
}),
).to.be.true;
});
describe('when exception is instantiated by "http-errors" library', () => {
it('should send expected response status code and message', () => {
const error = new createHttpError.NotFound('User does not exist');
Expand Down