Skip to content

Commit

Permalink
Export ZodSerializationException so consumers can log serialization…
Browse files Browse the repository at this point in the history
… errors (#115)
  • Loading branch information
BenLorantfy authored Nov 1, 2024
1 parent f6911cf commit 8e8d515
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ export class UserController {

In the above example, despite the `userService.findOne` method returns `password`, the `password` property will be stripped out thanks to the `@ZodSerializerDto` decorator.

### Logging serialization errors using `ZodSerializationException`

You can catch serialization errors using `ZodSerializationException` and log them using your preferred logger.

```ts
if (exception instanceof ZodSerializationException) {
const zodError = exception.getZodError();
this.logger.error(`ZodSerializationException: ${zodError.message}`);
}
```
See the example app [here](/packages/example/src/http-exception.filter.ts) for more information.

## Extended Zod

> [!CAUTION]
Expand Down
13 changes: 11 additions & 2 deletions packages/example/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { ZodValidationPipe } from 'nestjs-zod'
import { APP_PIPE } from '@nestjs/core'
import { ZodSerializerInterceptor, ZodValidationPipe } from 'nestjs-zod'
import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PostsModule } from './posts/posts.module';
import { HttpExceptionFilter } from './http-exception.filter';


@Module({
Expand All @@ -15,6 +16,14 @@ import { PostsModule } from './posts/posts.module';
provide: APP_PIPE,
useClass: ZodValidationPipe,
},
{
provide: APP_INTERCEPTOR,
useClass: ZodSerializerInterceptor,
},
{
provide: APP_FILTER,
useClass: HttpExceptionFilter,
},
]
})
export class AppModule {}
17 changes: 17 additions & 0 deletions packages/example/src/http-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Logger, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import { ZodSerializationException } from 'nestjs-zod';

@Catch(HttpException)
export class HttpExceptionFilter extends BaseExceptionFilter {
private readonly logger = new Logger(HttpExceptionFilter.name);

catch(exception: HttpException, host: ArgumentsHost) {
if (exception instanceof ZodSerializationException) {
const zodError = exception.getZodError();
this.logger.error(`ZodSerializationException: ${zodError.message}`);
}

super.catch(exception, host);
}
}
3 changes: 2 additions & 1 deletion packages/example/src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { ApiOkResponse } from '@nestjs/swagger';
import { createZodDto } from 'nestjs-zod'
import { createZodDto, ZodSerializerDto } from 'nestjs-zod'
import { z } from 'zod'

class PostDto extends createZodDto(z.object({
Expand All @@ -23,6 +23,7 @@ export class PostsController {
}

@Get(':id')
@ZodSerializerDto(PostDto)
@ApiOkResponse({ type: PostDto, description: 'Get a post by ID' })
getById(@Param('id') id: string) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/nestjs-zod/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type { ZodDto } from './dto'
export { createZodDto } from './dto'
export { ZodValidationException } from './exception'
export { ZodValidationException, ZodSerializationException } from './exception'
export { createZodGuard, UseZodGuard, ZodGuard } from './guard'
export { patchNestJsSwagger, zodToOpenAPI } from './openapi'
export { createZodValidationPipe, ZodValidationPipe } from './pipe'
Expand Down

0 comments on commit 8e8d515

Please sign in to comment.