HonoH
Hono7mo ago
Josh

Does anyone have any recommendations for handling RPC response errors?

I want to do something like the parseResult function below so I can have consistent error handling across my app, but I've been struggling with getting type generics to work on the parseResult function such that the result type is correct.

I'm kind of curious if anyone has recommendations on how to best handle using the rpc client throughout the app, or maybe I'm just over complicating things.

export async function fetchCentering(id: number) {
    const client = getClient();

    const result = await client.api.app.centering[':id'].$get({
        param: { id }
    });

    const parsed = await parseResult(result);

    return parsed;
}

async function parseResult(result) {
    if (!result.ok) {
        const errorData = await result.text();
        throw new Error(errorData || 'Failed');
    }

    return await result.json();
}
Was this page helpful?