Replies: 2 comments 2 replies
-
|
Hi @spietras! I'm Dosu and I’m helping the orpc team. You can handle unexpected HTTP responses (like a 401 from your gateway) in the oRPC client by using interceptors—specifically, the For example, you can map a 401 Unauthorized response to an import { RPCLink, onError, ORPCError } from 'orpc';
const link = new RPCLink({
interceptors: [
onError((error) => {
// If the error is an HTTP 401, map it to an ORPCError with code 'UNAUTHORIZED'
if (error?.status === 401) {
throw new ORPCError('UNAUTHORIZED', {
status: 401,
message: 'Session expired or unauthorized',
});
}
// Optionally, rethrow other errors unchanged
throw error;
}),
],
});This pattern is recommended and aligns with how oRPC suggests handling and transforming errors globally in the client. You can adapt the logic inside To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
You can intercept responses in the client and map custom HTTP codes (like 401) to ORPCError with the appropriate code, using a middleware or error hook is the cleanest approach. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I am using
RPCHandlerandRPCLink. When performing any operation, the underlying HTTP request and response with the underlying data will be based on the protocol designed by oRPC.Everything works fine when the client can communicate directly with the server. But what if you introduce a middleman who can change something and break the expectation?
What I have in mind specifically for my case is that all my requests from the client to the server will go through a gateway that, for example, will check if the session is still valid and, if not, will respond with 401 Unauthorized directly.
So I am thinking: can I change something in the client, so that I could map these custom unexpected responses to something understandable by oRPC? In this case, mapping that 401 Unauthorized from the gateway to
ORPCErrorwithUNAUTHORIZEDcode.This library looks so well thought out that no doubt this is possible, so rather the question is: what is the best way to do it?
Beta Was this translation helpful? Give feedback.
All reactions