I'm trying to use a "bad input exception filter" to catch errors and return them to the client. I have both websockets and http endpoints throughout my monorepo, on one application I have both.
The issue I'm having, I don't want to use two separate exception filters for WS & HTTP and until now I was relying on nestjs defining either the request or the socket context to tap into that and either return the error back to the websocket or returning the error via http res.send.
But it turns out they're both defined even when there's no websockets within the application at all, no adapter registered, nothing.
Exception handler:
const req: IRequest = host.switchToHttp().getRequest();
const res: Response = host.switchToHttp().getResponse();
const socket: ISocket = host.switchToWs().getClient();
if (req && res) {
// returning req res
}
if (socket) {
// emitting error on socket
Is there a way to either:
- prevent this
- tap into another value from the context to know where to return the error to
Thank you.
Something to take note of is that the
ExecutionContext
has anargs
property that is an array of values related to the request. TheswitchTo*().get*()
methods are really just syntactic sugar for things likegetArgsByIndex()
. What you can do, instead of checking for truthiness ofreq
,res
, andsocket
is use thegetType()
property on theArgumentHost
/ExecutionContext
which will returnhttp
,ws
,rpc
, orgraphql
depending on the request, and then you can separate out your error handling logic from there. I like to use a switch case for it, but if statements would work tooI have something similar in an interceptor for logging, using
ExecutionContext
instead ofArgumentHost
, but the idea should be the same. ExecutionContext just has agetClass
andgetHandler
method