I am playing around with NestJS
and I would like to take the error thrown from TypeORM
and convert it into a shape that I can control.
Right now, I'm just trying to catch the error thrown from TypeORM
and log it out to see that my custom filter is working correctly. But unfortunately, my console.log
statement in the filter is never logging.
Here is a slimmed down version of my service and filter
user.service.ts
export class UserService {
constructor(
@InjectRepository(Users)
private readonly userRepository: Repository<Users>,
) {}
@UseFilters(new TypeOrmFilter())
async create(createUserDto: CreateUserDto) {
const user = this.userRepository.create(createUserDto);
return this.userRepository.save(user);
}
}
type-orm-filter.ts
@Catch()
export class TypeOrmFilter implements ExceptionFilter {
catch(exception: Error, host: ArgumentsHost) {
console.log('\nI have caught an error\n', exception);
throw exception;
}
}
Here is the log output from the error being thrown by TypeORM
[Nest] 61496 - 04/11/2021, 9:01:42 PM [ExceptionsHandler] invalid input syntax for type uuid: "123e4567" +2482ms
QueryFailedError: invalid input syntax for type uuid: "123e4567"
at new QueryFailedError (my-nest-project/error-project-nestjs/node_modules/typeorm/error/QueryFailedError.js:11:28)
at PostgresQueryRunner.<anonymous> (my-nest-project/error-project-nestjs/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:247:31)
at step (my-nest-project/error-project-nestjs/node_modules/typeorm/node_modules/tslib/tslib.js:141:27)
at Object.throw (my-nest-project/error-project-nestjs/node_modules/typeorm/node_modules/tslib/tslib.js:122:57)
at rejected (my-nest-project/error-project-nestjs/node_modules/typeorm/node_modules/tslib/tslib.js:113:69)
at processTicksAndRejections (internal/process/task_queues.js:93:5)