I am seeking a way to capture both the result and any potential errors from an asynchronous method within a controller using an interceptor. When an error is thrown, the interceptor can respond accordingly. However, I am struggling to figure out how to trigger logic in the interceptor when everything goes smoothly.
Below is the code for my controller method:
@UseInterceptors(GatewayInterceptor)
@Get([ ':id?', ':id/relationships/:type', 'relationships/:type' ])
public async handleGet(
@Req() req : Request,
@Res() res : Response,
) {
await this.gatewayService.redirect_to_database_service_GET()
.then( data => res.send(data))
.catch(err => { throw( new HttpException( err.message, HttpStatus.BAD_REQUEST) ) })
}
And here is the interceptor :
export class GatewayInterceptor implements NestInterceptor {
public intercept(context: ExecutionContext, next: CallHandler) {
const request = context.switchToHttp().getRequest();
return next
.handle()
.pipe(
tap( () => {
console.log('VALUE')
this.decrement_user_acces_on_service(request)
}),
catchError(error => {
console.log('ERROR')
throw error
})
);
}
}
The issue I'm facing is that 'console.log('ERROR')' is triggered with errors, but I cannot seem to get 'console.log('VALUE')' to execute when there are no errors. Can anyone spot where my mistake might be?
Any help would be greatly appreciated!