Hi all, i can't rewrite controllers exceptions.
When TypeORM generate some exceptions, i want display this for end user in error message.
Exception Filters and Interceptors not work in this case.
It would better to catch TypeORM exceptions in service level and transform those business exceptions which are then handled in controller level. Then you can catch those using filters and send maybe Bad Request back to clients.
@Matonen Thank for yout help. How i can tranfrom this exception? For example i have user.service with create method:
create = (user: UserDto): Observable<User> => Observable
.fromPromise(this.userRepository.save({ ...new User(), ...user }))
and controller method
@Post('create')
async create( @Body() user: UserDto) {
return this.userService.create(user);
}
I haven't used Observable a much. Maybe something like this could work:
create = (user: UserDto): Observable<User> => Observable
.fromPromise(this.userRepository.save({ ...new User(), ...user }))
.catch((err) => Observable.throw(mapBusinessError(err));
@Matonen thank you, after mapping work is nice, my example:
create = (user: UserDto): Observable<User> => Observable
.fromPromise(this.userRepository.save({ ...new User(), ...user }))
.catch(err => Observable.throw(new HttpException(err.sqlMessage, 500)))
Great! Also think about extends HttpException with your custom ones to have better readability.
I dont't know what kind app you are writing but it not usually good idea to send sql error message back to client.
Hi @xvs32x,
Yeah, @Matonen solution is great! Glad you found an answer 馃檪
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I haven't used Observable a much. Maybe something like this could work: