Skip to content

Commit 6efc6e7

Browse files
Copilotpanz3r
andauthored
fix: surface errors from useAsyncCallback in examples/base
Agent-Logs-Url: https://github.com/forwardsoftware/react-auth/sessions/848c6a14-640a-4fb5-8f04-f3f09fbbedf8 Co-authored-by: panz3r <1754457+panz3r@users.noreply.github.com>
1 parent d0c3af4 commit 6efc6e7

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

examples/base/src/Content.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import { useAuthClient } from './auth';
66
export const Content: React.FC = () => {
77
const authClient = useAuthClient();
88

9-
const [doLogin, isLoginLoading] = useAsyncCallback(() => authClient.login(), [
9+
const [doLogin, isLoginLoading, loginError] = useAsyncCallback(() => authClient.login(), [
1010
authClient,
1111
]);
12-
const [doRefresh, isRefreshLoading] = useAsyncCallback(
12+
const [doRefresh, isRefreshLoading, refreshError] = useAsyncCallback(
1313
() => authClient.refresh(),
1414
[authClient]
1515
);
16-
const [doLogout, isLogoutLoading] = useAsyncCallback(
16+
const [doLogout, isLogoutLoading, logoutError] = useAsyncCallback(
1717
() => authClient.logout(),
1818
[authClient]
1919
);
@@ -49,6 +49,10 @@ export const Content: React.FC = () => {
4949
{isLoginLoading ? <p>Login in progress..</p> : null}
5050
{isRefreshLoading ? <p>Refresh in progress..</p> : null}
5151

52+
{loginError != null ? <p>Login error: {loginError instanceof Error ? loginError.message : 'An error occurred'}</p> : null}
53+
{refreshError != null ? <p>Refresh error: {refreshError instanceof Error ? refreshError.message : 'An error occurred'}</p> : null}
54+
{logoutError != null ? <p>Logout error: {logoutError instanceof Error ? logoutError.message : 'An error occurred'}</p> : null}
55+
5256
<p>Tokens:</p>
5357
<pre>{JSON.stringify(authClient.tokens, null, 2)}</pre>
5458
</div>
@@ -58,16 +62,20 @@ export const Content: React.FC = () => {
5862
function useAsyncCallback<T extends (...args: never[]) => Promise<unknown>>(
5963
callback: T,
6064
deps: DependencyList
61-
): [T, boolean] {
65+
): [T, boolean, unknown] {
6266
const [isLoading, setLoading] = useState(false);
67+
const [error, setError] = useState<unknown>(null);
6368
const cb = useCallback(async (...argsx: never[]) => {
6469
setLoading(true);
70+
setError(null);
6571
try {
6672
return await callback(...argsx);
73+
} catch (err) {
74+
setError(err);
6775
} finally {
6876
setLoading(false);
6977
}
7078
}, deps) as T;
7179

72-
return [cb, isLoading];
80+
return [cb, isLoading, error];
7381
}

0 commit comments

Comments
 (0)