Skip to content

Commit

Permalink
Fix Error Handling in Client Hook
Browse files Browse the repository at this point in the history
  • Loading branch information
timoclsn committed Oct 6, 2023
1 parent b62d01b commit 536c1d7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions apps/resources/lib/serverActions/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,23 @@ export const useAction = <
options.onSuccess?.(result.data);
}
} catch (error) {
const errorMessage = 'Something went wrong. Please try again.';
const errorMessage = getErrorMessage(error);
// next/navigation functions work by throwing an error that will be
// processed internally by Next.js. So, in this case we need to rethrow it.
if (
errorMessage === 'NEXT_REDIRECT' ||
errorMessage == 'NEXT_NOT_FOUND'
) {
throw error;
}

const userErrorMessage = 'Something went wrong. Please try again.';
dispatch({
type: 'IS_ERROR',
error: errorMessage,
error: userErrorMessage,
});
options.onError?.(errorMessage);
console.log(getErrorMessage(error));
options.onError?.(userErrorMessage);
console.log(errorMessage);
}

options.onSettled?.();
Expand Down

0 comments on commit 536c1d7

Please sign in to comment.