better error handling
This commit is contained in:
parent
0bf92a44df
commit
ae4d4647e9
@ -141,8 +141,8 @@ export function LoginModal({ onConfirm, withMessage }: Props): VNode {
|
|||||||
class="button is-info"
|
class="button is-info"
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
const secretToken = normalizeToken(token);
|
const secretToken = normalizeToken(token);
|
||||||
const isOk = await testLogin(url, secretToken);
|
const { valid, cause } = await testLogin(url, secretToken);
|
||||||
if (isOk) {
|
if (valid) {
|
||||||
onConfirm(url, secretToken);
|
onConfirm(url, secretToken);
|
||||||
} else {
|
} else {
|
||||||
onConfirm(url);
|
onConfirm(url);
|
||||||
|
@ -25,8 +25,10 @@ import { useBackendContext } from "../context/backend.js";
|
|||||||
import { useCallback, useEffect, useState } from "preact/hooks";
|
import { useCallback, useEffect, useState } from "preact/hooks";
|
||||||
import { useInstanceContext } from "../context/instance.js";
|
import { useInstanceContext } from "../context/instance.js";
|
||||||
import {
|
import {
|
||||||
|
ErrorType,
|
||||||
HttpResponse,
|
HttpResponse,
|
||||||
HttpResponseOk,
|
HttpResponseOk,
|
||||||
|
RequestError,
|
||||||
RequestOptions,
|
RequestOptions,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { useApiContext } 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(
|
return async function testLogin(
|
||||||
instance: string,
|
instance: string,
|
||||||
token: string,
|
token: string,
|
||||||
): Promise<boolean> {
|
): Promise<{
|
||||||
|
valid: boolean;
|
||||||
|
cause?: ErrorType;
|
||||||
|
}> {
|
||||||
try {
|
try {
|
||||||
const response = await request(instance, `/private/`, {
|
const response = await request(instance, `/private/`, {
|
||||||
token,
|
token,
|
||||||
});
|
});
|
||||||
return true;
|
return { valid: true };
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
return false;
|
if (error instanceof RequestError) {
|
||||||
|
return { valid: false, cause: error.cause.type };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { valid: false, cause: ErrorType.UNEXPECTED };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user