-
I'm trying something like this in my tests: try {
const data = await call(appRouter.auth.checkUsername, {
username,
});
} catch (error) {
if (isDefinedError(error)) {
// error is unknown here
}
} But the error doesn't have a type. What else can I do? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
To get type-safe errors from the import { InferContractRouterErrorMap, ErrorFromErrorMap } from '@orpc/contract';
try {
const data = await call(appRouter.sellers.auth.checkUsername, { username });
} catch (e) {
const error = e as ErrorFromErrorMap<InferContractRouterErrorMap<typeof appRouter>>;
if (isDefinedError(error)) {
// error is now properly typed here
// you can access error.code, error.message, etc.
}
} This approach works as long as your router does not use lazy routers. The error will be either a contract-defined error or a generic throwable error, matching the contract's error map structure. More details and examples are available in the official discussion. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
Because of TypeScript’s limitations with try/catch, it's impossible to achieve type safety. You must use the |
Beta Was this translation helpful? Give feedback.
Because of TypeScript’s limitations with try/catch, it's impossible to achieve type safety. You must use the
safe
utility instead.