From 9922192b0dba2e479b5af3e29c1d44b98e4d29d7 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Tue, 28 Feb 2023 19:03:43 -0300
Subject: fix #7729

---
 .../demobank-ui/src/pages/RegistrationPage.tsx     | 65 ++++++----------------
 1 file changed, 17 insertions(+), 48 deletions(-)

(limited to 'packages/demobank-ui/src/pages/RegistrationPage.tsx')

diff --git a/packages/demobank-ui/src/pages/RegistrationPage.tsx b/packages/demobank-ui/src/pages/RegistrationPage.tsx
index c6bc3c327..f22475e10 100644
--- a/packages/demobank-ui/src/pages/RegistrationPage.tsx
+++ b/packages/demobank-ui/src/pages/RegistrationPage.tsx
@@ -15,7 +15,6 @@
  */
 import { HttpStatusCode, Logger } from "@gnu-taler/taler-util";
 import {
-  ErrorType,
   RequestError,
   useTranslationContext,
 } from "@gnu-taler/web-util/lib/index.browser";
@@ -25,7 +24,7 @@ import { useBackendContext } from "../context/backend.js";
 import { PageStateType } from "../context/pageState.js";
 import { useTestingAPI } from "../hooks/access.js";
 import { bankUiSettings } from "../settings.js";
-import { undefinedIfEmpty } from "../utils.js";
+import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
 import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
 
 const logger = new Logger("RegistrationPage");
@@ -177,52 +176,22 @@ function RegistrationForm({
                     onComplete();
                   } catch (error) {
                     if (error instanceof RequestError) {
-                      const e =
-                        error as RequestError<SandboxBackend.SandboxError>;
-                      switch (e.cause.type) {
-                        case ErrorType.TIMEOUT: {
-                          onError({
-                            title: i18n.str`Request timeout, try again later.`,
-                          });
-                          break;
-                        }
-                        case ErrorType.CLIENT: {
-                          const errorData = e.cause.error;
-                          if (e.cause.status === HttpStatusCode.Conflict) {
-                            onError({
-                              title: i18n.str`That username is already taken`,
-                              description: errorData.error.description,
-                              debug: JSON.stringify(error.cause),
-                            });
-                          } else {
-                            onError({
-                              title: i18n.str`New registration gave response error`,
-                              description: errorData.error.description,
-                              debug: JSON.stringify(error.cause),
-                            });
-                          }
-                          break;
-                        }
-                        case ErrorType.SERVER: {
-                          const errorData = e.cause.error;
-                          onError({
-                            title: i18n.str`New registration gave response error`,
-                            description: errorData?.error?.description,
-                            debug: JSON.stringify(error.cause),
-                          });
-                          break;
-                        }
-                        case ErrorType.UNEXPECTED: {
-                          onError({
-                            title: i18n.str`Unexpected error doing the registration.`,
-                            debug: JSON.stringify(error.cause),
-                          });
-                          break;
-                        }
-                        default: {
-                          assertUnreachable(e.cause);
-                        }
-                      }
+                      onError(
+                        buildRequestErrorMessage(i18n, error.cause, {
+                          onClientError: (status) =>
+                            status === HttpStatusCode.Conflict
+                              ? i18n.str`That username is already taken`
+                              : undefined,
+                        }),
+                      );
+                    } else {
+                      onError({
+                        title: i18n.str`Operation failed, please report`,
+                        description:
+                          error instanceof Error
+                            ? error.message
+                            : JSON.stringify(error),
+                      });
                     }
                   }
                 }}
-- 
cgit v1.2.3

