better error handling

This commit is contained in:
Sebastian 2023-03-15 09:54:31 -03:00
parent 0bf92a44df
commit ae4d4647e9
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069
2 changed files with 15 additions and 6 deletions

View File

@ -141,8 +141,8 @@ export function LoginModal({ onConfirm, withMessage }: Props): VNode {
class="button is-info"
onClick={async () => {
const secretToken = normalizeToken(token);
const isOk = await testLogin(url, secretToken);
if (isOk) {
const { valid, cause } = await testLogin(url, secretToken);
if (valid) {
onConfirm(url, secretToken);
} else {
onConfirm(url);

View File

@ -25,8 +25,10 @@ import { useBackendContext } from "../context/backend.js";
import { useCallback, useEffect, useState } from "preact/hooks";
import { useInstanceContext } from "../context/instance.js";
import {
ErrorType,
HttpResponse,
HttpResponseOk,
RequestError,
RequestOptions,
} from "@gnu-taler/web-util/lib/index.browser";
import { useApiContext } from "@gnu-taler/web-util/lib/index.browser";
@ -146,14 +148,21 @@ export function useCredentialsChecker() {
return async function testLogin(
instance: string,
token: string,
): Promise<boolean> {
): Promise<{
valid: boolean;
cause?: ErrorType;
}> {
try {
const response = await request(instance, `/private/`, {
token,
});
return true;
} catch (e) {
return false;
return { valid: true };
} catch (error) {
if (error instanceof RequestError) {
return { valid: false, cause: error.cause.type };
}
return { valid: false, cause: ErrorType.UNEXPECTED };
}
};
}