postio / apps /backend /src /api /routes /posts.validation.exception.ts
Leon4gr45's picture
Upload folder using huggingface_hub (part 4)
3722089 verified
Raw
History Blame Contribute Delete
1.03 kB
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,
});
}
}