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: update to filter to allow custom errors to manipulate filter output #13

Open
wants to merge 1 commit into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@nestjs/common": "^11.0.4",
"@nestjs/core": "^11.0.4",
"@nestjs/platform-express": "^11.0.4",
"nest-problem-details-filter": "file:../../libs/nest-problem-details-filter/nest-problem-details-filter-1.0.0.tgz",
"nest-problem-details-filter": "file:../../libs/nest-problem-details-filter/nest-problem-details-filter-1.0.1.tgz",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@nestjs/common": "^11.0.4",
"@nestjs/core": "^11.0.4",
"@nestjs/platform-fastify": "^11.0.4",
"nest-problem-details-filter": "file:../../libs/nest-problem-details-filter/nest-problem-details-filter-1.0.0.tgz",
"nest-problem-details-filter": "file:../../libs/nest-problem-details-filter/nest-problem-details-filter-1.0.1.tgz",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
},
Expand Down
7 changes: 4 additions & 3 deletions libs/nest-problem-details-filter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nest-problem-details-filter",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -18,8 +18,8 @@
"homepage": "https://github.com/Fcmam5/nest-http-problem-details#readme",
"scripts": {
"build": "nest build",
"prepack": "rm -rf dist && npm run build",
"prepublish": "rm -rf dist && npm run build",
"prepack": "shx rm -rf dist && npm run build",
"prepublish": "shx rm -rf dist && npm run build",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint:fix": "npm run lint -- --fix",
Expand Down Expand Up @@ -48,6 +48,7 @@
"prettier": "^3.4.2",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"shx": "^0.3.4",
"source-map-support": "^0.5.21",
"ts-jest": "^29.2.5",
"ts-loader": "^9.5.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ const mockHttpAdatperHost = {
},
} as unknown as Pick<HttpAdapterHost, 'httpAdapter'>;

// Example business error
class BusinessErrorException extends HttpException {
constructor(message: string, detail?: string, instance?: string) {
super(
{
message,
error: {
type: 'business-error',
detail,
instance,
},
},
HttpStatus.BAD_REQUEST,
);
}
}

describe('HttpExceptionFilter', () => {
let filter: HttpExceptionFilter;

Expand Down Expand Up @@ -199,6 +216,65 @@ describe('HttpExceptionFilter', () => {
});
});

describe('BusinessErrorException', () => {
it('should map BusinessErrorException with custom type', () => {
const status = HttpStatus.BAD_REQUEST;
const message = 'A business error occurred';

const expectation: IProblemDetail = {
title: message,
status,
type: 'http://fcmam5.me/problems/business-error', // Include base URI
};

filter.catch(new BusinessErrorException(message), mockArgumentsHost);

assertResponse(status, expectation);
});

it('should map BusinessErrorException with custom type and additional details', () => {
const status = HttpStatus.BAD_REQUEST;
const message = 'A business error occurred with details';
const detail = 'Additional error details';

const expectation: IProblemDetail = {
title: message,
status,
type: 'http://fcmam5.me/problems/business-error', // Include base URI
detail,
};

filter.catch(
new BusinessErrorException(message, detail),
mockArgumentsHost,
);

assertResponse(status, expectation);
});

it('should map BusinessErrorException with custom type, details, and instance', () => {
const status = HttpStatus.BAD_REQUEST;
const message = 'A business error occurred with details and instance';
const detail = 'Additional error details';
const instance = 'instance-123';

const expectation: IProblemDetail = {
title: message,
status,
type: 'http://fcmam5.me/problems/business-error', // Include base URI
detail,
instance,
};

filter.catch(
new BusinessErrorException(message, detail, instance),
mockArgumentsHost,
);

assertResponse(status, expectation);
});
});

describe.each([
['HttpAdapterHost', mockHttpAdatperHost as HttpAdapterHost],
['HttpAdapter', mockHttpAdapter as HttpAdapterHost['httpAdapter']],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const status = exception.getStatus();
const errorResponse = exception.getResponse() as
| string
| IExceptionResponse;
const errorResponse = exception.getResponse() as string | IErrorDetail;

let title: string;
let detail;
Expand All @@ -57,7 +55,8 @@ export class HttpExceptionFilter implements ExceptionFilter {
detail = errorResponse.error;
} else {
if (errorResponse.error) {
type = errorResponse.error.error?.type;
type = errorResponse.error.type;
detail = errorResponse.error.detail;
objectExtras = {
...errorResponse.error,
};
Expand All @@ -83,11 +82,3 @@ export class HttpExceptionFilter implements ExceptionFilter {
return this.defaultHttpErrors[status];
}
}

interface IExceptionResponse {
error?: string | IErrorDetail;
message: string;
type?: string;
instance?: string;
statusCode: number;
}