File size: 1,033 Bytes
3722089 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
} from '@nestjs/common';
export type PostValidationError = {
/** Provider identifier, e.g. "x", "linkedin-page". */
provider: string;
/** Human readable provider name, e.g. "X", "LinkedIn Page". */
name: string;
/** The readable validation error. */
error: string;
};
export class PostValidationException extends HttpException {
constructor(message: PostValidationError) {
super(message, HttpStatus.BAD_REQUEST);
}
}
@Catch(PostValidationException)
export class PostValidationExceptionFilter implements ExceptionFilter {
catch(exception: PostValidationException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const status = exception.getStatus();
const { provider, name, error } =
exception.getResponse() as PostValidationError;
response.status(status).json({
statusCode: status,
provider,
name,
message: error,
});
}
}
|