Error Handling in NestJS
- NestJS provides set of built-in exceptions (such as the
NotFoundException)
import { Body, Controller, Get, NotFoundException, Param, Put } from '@nestjs/common';
import { TodoDto, UpdateTodoDto } from './dtos/todo.dto';
import { TodoService } from './todo.service';
@Controller('todos')
export class TodoController {
constructor(private readonly todoService: TodoService) {}
@Get(':id')
findOne(@Param('id') id: string): TodoDto | null {
const todo = this.todoService.findOne(id);
if (!todo) {
throw new NotFoundException('Todo not found');
}
return todo;
}
@Put(':id')
updateOne(@Param('id') id: string, @Body() todo: UpdateTodoDto): TodoDto | null {
const updatedTodo = this.todoService.updateOne(id, todo);
if (!updatedTodo) {
throw new NotFoundException('Todo not found');
}
return updatedTodo;
}
@Put(':id/complete')
completeOne(@Param('id') id: string): TodoDto | null {
const completedTodo = this.todoService.markComplete(id);
if (!completedTodo) {
throw new NotFoundException('Todo not found');
}
return completedTodo;
}
}