返回 AiToEarn
zod-validation.exception.ts
根目录 / project / aitoearn-backend / libs / common / src / exceptions / zod-validation.exception.ts
1 import type { ZodError } from 'zod'
2 import {
3 BadRequestException,
4 HttpStatus,
5 InternalServerErrorException,
6 } from '@nestjs/common'
7 import { z } from 'zod'
8
9 export class ZodValidationException extends BadRequestException {
10 constructor(private error: ZodError) {
11 super({
12 statusCode: HttpStatus.BAD_REQUEST,
13 message: 'Validation failed',
14 errors: z.treeifyError(error),
15 })
16 }
17
18 public getZodError() {
19 return this.error
20 }
21 }
22
23 export class ZodSerializationException extends InternalServerErrorException {
24 // eslint-disable-next-line node/handle-callback-err
25 constructor(private error: ZodError) {
26 super()
27 }
28
29 public getZodError() {
30 return this.error
31 }
32 }
33
34 export type ZodExceptionCreator = (error: ZodError) => Error
35
36 export const createZodValidationException: ZodExceptionCreator = (error) => {
37 return new ZodValidationException(error)
38 }
39
40 export const createZodSerializationException: ZodExceptionCreator = (error) => {
41 return new ZodSerializationException(error)
42 }
43
43 lines TYPESCRIPT