From c84c9834158a136e59b52315b35e7367341d44f0 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Mon, 21 Feb 2022 17:06:35 +0100 Subject: [PATCH] changed how errors are displayed (#2066) --- .../add/OpenIdConnectSettings.tsx | 52 +++++++++++-------- .../add/SamlConnectSettings.tsx | 20 +++++-- .../component/DiscoveryEndpointField.tsx | 9 +++- src/identity-providers/messages.ts | 3 +- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/identity-providers/add/OpenIdConnectSettings.tsx b/src/identity-providers/add/OpenIdConnectSettings.tsx index 6530a05e60..74bd8a57ac 100644 --- a/src/identity-providers/add/OpenIdConnectSettings.tsx +++ b/src/identity-providers/add/OpenIdConnectSettings.tsx @@ -17,39 +17,49 @@ export const OpenIdConnectSettings = () => { const adminClient = useAdminClient(); const { realm } = useRealm(); - const { setValue, errors, setError } = useFormContext(); + const { setValue, errors, setError, clearErrors } = useFormContext(); const setupForm = (result: any) => { Object.keys(result).map((k) => setValue(`config.${k}`, result[k])); }; const fileUpload = async (obj?: object) => { - if (obj) { - const formData = new FormData(); - formData.append("providerId", id); - formData.append("file", new Blob([JSON.stringify(obj)])); + clearErrors("discoveryError"); + if (!obj) { + return; + } - try { - const response = await fetch( - `${getBaseUrl( - adminClient - )}admin/realms/${realm}/identity-provider/import-config`, - { - method: "POST", - body: formData, - headers: { - Authorization: `bearer ${await adminClient.getAccessToken()}`, - }, - } - ); + const formData = new FormData(); + formData.append("providerId", id); + formData.append("file", new Blob([JSON.stringify(obj)])); + + try { + const response = await fetch( + `${getBaseUrl( + adminClient + )}admin/realms/${realm}/identity-provider/import-config`, + { + method: "POST", + body: formData, + headers: { + Authorization: `Bearer ${await adminClient.getAccessToken()}`, + }, + } + ); + if (response.ok) { const result = await response.json(); setupForm(result); - } catch (error) { + } else { setError("discoveryError", { type: "manual", - message: (error as Error).message, + message: response.statusText, }); } + } catch (error) { + setError("discoveryError", { + type: "manual", + message: (error as Error).message, + }); } }; @@ -72,7 +82,7 @@ export const OpenIdConnectSettings = () => { /> } validated={errors.discoveryError ? "error" : "default"} - helperTextInvalid={errors.discoveryError} + helperTextInvalid={errors.discoveryError?.message} > { const adminClient = useAdminClient(); const { realm } = useRealm(); - const { setValue, register, errors, setError } = useFormContext(); + const { setValue, register, errors, setError, clearErrors } = + useFormContext(); const setupForm = (result: IdentityProviderRepresentation) => { Object.entries(result).map(([key, value]) => @@ -29,6 +30,10 @@ export const SamlConnectSettings = () => { }; const fileUpload = async (xml: string) => { + clearErrors("discoveryError"); + if (!xml) { + return; + } const formData = new FormData(); formData.append("providerId", id); formData.append("file", new Blob([xml])); @@ -46,8 +51,15 @@ export const SamlConnectSettings = () => { }, } ); - const result = await response.json(); - setupForm(result); + if (response.ok) { + const result = await response.json(); + setupForm(result); + } else { + setError("discoveryError", { + type: "manual", + message: response.statusText, + }); + } } catch (error) { setError("discoveryError", { type: "manual", @@ -99,7 +111,7 @@ export const SamlConnectSettings = () => { /> } validated={errors.discoveryError ? "error" : "default"} - helperTextInvalid={errors.discoveryError} + helperTextInvalid={errors.discoveryError?.message} > { + clearErrors("discoveryError"); + setDiscovery(checked); + }} /> {discovery && ( @@ -109,7 +112,9 @@ export const DiscoveryEndpointField = ({ helperTextInvalid={ errors.discoveryEndpoint ? t("common:required") - : t("noValidMetaDataFound") + : t("noValidMetaDataFound", { + error: errors.discoveryError?.message, + }) } isRequired > diff --git a/src/identity-providers/messages.ts b/src/identity-providers/messages.ts index 29f11b112e..bfcb53502e 100644 --- a/src/identity-providers/messages.ts +++ b/src/identity-providers/messages.ts @@ -98,7 +98,8 @@ export default { importConfig: "Import config from file", showMetaData: "Show metadata", hideMetaData: "Hide metadata", - noValidMetaDataFound: "No valid metadata was found at this URL", + noValidMetaDataFound: + "No valid metadata was found at this URL: '{{error}}'", advanced: "Advanced", metadataOfDiscoveryEndpoint: "Metadata of the discovery endpoint", authorizationUrl: "Authorization URL",