commit 51292462090789cee3577bcc2e5083d9d3b546d1
parent bfc39eeafb6bdb6c4e338074c2d52dfd1de344a5
Author: Sebastian <sebasjm@gmail.com>
Date: Mon, 17 Feb 2025 14:02:41 -0300
fix #9553
Diffstat:
2 files changed, 127 insertions(+), 20 deletions(-)
diff --git a/packages/challenger-ui/src/pages/AskChallenge.tsx b/packages/challenger-ui/src/pages/AskChallenge.tsx
@@ -17,11 +17,12 @@ import {
EmptyObject,
HttpStatusCode,
TalerError,
- TranslatedString
+ TranslatedString,
} from "@gnu-taler/taler-util";
import {
Attention,
Button,
+ ErrorLoading,
LocalNotificationBanner,
RouteDefinition,
ShowInputErrorLabel,
@@ -35,6 +36,7 @@ import { useState } from "preact/hooks";
import { useChallengeSession } from "../hooks/challenge.js";
import { SessionId, useSessionState } from "../hooks/session.js";
import { doAutoFocus } from "./AnswerChallenge.js";
+import { ErrorLoadingWithDebug } from "./ErrorLoadingWithDebug.js";
export const EMAIL_REGEX = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/;
@@ -72,24 +74,109 @@ export function AskChallenge({
if (!restrictionKey) {
return (
- <div>
- invalid server configuration, there is no restriction in /config
- </div>
+ <Attention title={i18n.str`Server configuration error`} type="danger">
+ <i18n.Translate>
+ Invalid server configuration, there is no restriction in /config
+ </i18n.Translate>
+ </Attention>
);
}
- const regexEmail = !config.restrictions
+ const restriction = !config.restrictions
? undefined
: config.restrictions[restrictionKey];
- const regexTest =
- regexEmail && regexEmail.regex ? new RegExp(regexEmail.regex) : EMAIL_REGEX;
- const regexHint =
- regexEmail && regexEmail.hint ? regexEmail.hint : i18n.str`invalid email`;
+ const regexText =
+ restriction && restriction.regex ? restriction.regex : undefined;
+
+ let restrictionRG;
+ if (regexText) {
+ try {
+ restrictionRG = new RegExp(regexText);
+ } catch (e) {
+ return (
+ <Attention title={i18n.str`Server configuration error`} type="danger">
+ <i18n.Translate>
+ Invalid server regular expression configuration. Server restriction
+ is "{regexText}" but it didn't compile: {String(e)}
+ </i18n.Translate>
+ </Attention>
+ );
+ }
+ } else {
+ restrictionRG = EMAIL_REGEX;
+ }
+
+ const restrictionHint =
+ restriction && restriction.hint
+ ? restriction.hint
+ : i18n.str`invalid email`;
+
+ if (!result) {
+ return (
+ <div>
+ <i18n.Translate>loading...</i18n.Translate>
+ </div>
+ );
+ }
+ if (result instanceof TalerError) {
+ return <ErrorLoadingWithDebug error={result} />;
+ }
+ if (result.type === "fail") {
+ switch (result.case) {
+ case HttpStatusCode.BadRequest: {
+ return (
+ <Attention
+ type="danger"
+ title={i18n.str`Couldn't get the information`}
+ >
+ <i18n.Translate>Bad request</i18n.Translate>
+ </Attention>
+ );
+ }
+ case HttpStatusCode.NotFound: {
+ return (
+ <Attention
+ type="danger"
+ title={i18n.str`Couldn't get the information`}
+ >
+ <i18n.Translate>Not found</i18n.Translate>
+ </Attention>
+ );
+ }
+ case HttpStatusCode.NotAcceptable: {
+ return (
+ <Attention
+ type="danger"
+ title={i18n.str`Couldn't get the information`}
+ >
+ <i18n.Translate>Not acceptable</i18n.Translate>
+ </Attention>
+ );
+ }
+ case HttpStatusCode.TooManyRequests: {
+ return (
+ <Attention
+ type="danger"
+ title={i18n.str`Couldn't get the information`}
+ >
+ <i18n.Translate>Too many request</i18n.Translate>
+ </Attention>
+ );
+ }
+ case HttpStatusCode.InternalServerError: {
+ return (
+ <Attention
+ type="danger"
+ title={i18n.str`Couldn't get the information`}
+ >
+ <i18n.Translate>Server error</i18n.Translate>
+ </Attention>
+ );
+ }
+ }
+ }
- const lastStatus =
- result && !(result instanceof TalerError) && result.type !== "fail"
- ? result.body
- : undefined;
+ const lastStatus = result.body;
const prevAddr = !lastStatus?.last_address
? undefined
@@ -98,8 +185,8 @@ export function AskChallenge({
const errors = undefinedIfEmpty({
address: !address
? i18n.str`required`
- : !regexTest.test(address)
- ? regexHint
+ : !restrictionRG.test(address)
+ ? restrictionHint
: prevAddr !== undefined && address === prevAddr
? i18n.str`can't use the same address`
: undefined,
@@ -118,7 +205,7 @@ export function AskChallenge({
: state.lastAddress.filter((d) => !!d.address[restrictionKey]);
const onSend =
- errors || !contact
+ errors || !contact
? undefined
: withErrorHandler(
async () => {
@@ -151,10 +238,6 @@ export function AskChallenge({
},
);
- if (!lastStatus) {
- return <div>no status loaded</div>;
- }
-
return (
<Fragment>
<LocalNotificationBanner notification={notification} showDebug={true} />
diff --git a/packages/challenger-ui/src/pages/ErrorLoadingWithDebug.tsx b/packages/challenger-ui/src/pages/ErrorLoadingWithDebug.tsx
@@ -0,0 +1,24 @@
+/*
+ This file is part of GNU Taler
+ (C) 2022-2024 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+import { TalerError } from "@gnu-taler/taler-util";
+import { ErrorLoading } from "@gnu-taler/web-util/browser";
+import { VNode, h } from "preact";
+import { usePreferences } from "../context/preferences.js";
+
+export function ErrorLoadingWithDebug({ error }: { error: TalerError }): VNode {
+ const [pref] = usePreferences();
+ return <ErrorLoading error={error} showDetail={pref.showDebugInfo} />;
+}