Error Handling
Errors are handled very gracefully in Kaito. You can throw anywhere in any route, and it will be caught and handled. This allows you to focus on the happy-paths of your application, and know that errors are handled gracefully. The other advantage to this pattern is that you don’t have to worry about error messages that could contain sensitive information, as everything passes through your onError handler.
Kaito has a built in error called KaitoError
. You can throw this error in your routes, and it will be caught and sent back to the client.
Throwing a KaitoError
will not call your .onError
handler defined in your server. KaitoErrors are handled
internally and are always sent back to the client.
export const users = router().get('/:id', async ({ctx, params}) => {
const user = await ctx.db.users.findOne({
where: {id: params.id},
});
if (!user) {
// Client will receive this status code and error message always. This bypasses your .onError() handler
throw new KaitoError(404, 'User not found');
}
return user;
});
All other errors will be forwarded to your .onError
handler, which dictates what status and message should be sent to the client.