From e8bb66b7403f610a2dfa2bdcf13cc96d15f7128d Mon Sep 17 00:00:00 2001 From: Leander Furumo Date: Fri, 26 Jul 2024 03:04:08 +0200 Subject: [PATCH] display message on javascript error Specifically this allows us to display an error message when the API is unavailable --- src/components/FormErrors.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/components/FormErrors.tsx b/src/components/FormErrors.tsx index 9564043..0b0dca7 100644 --- a/src/components/FormErrors.tsx +++ b/src/components/FormErrors.tsx @@ -4,13 +4,24 @@ import { Text, StyleSheet, View } from 'react-native'; // eslint-disable-next-line @typescript-eslint/no-explicit-any const FormErrors = ({ errors }: { errors: Record }) => { + if (!errors) { + return; + } + let errorFormatted; - if ('non_field_errors' in errors) { + if (errors instanceof Error) { + // This check might backfire on us as it's too general. We do not wish to display the error message to the user + // Hence we display this generic one + errorFormatted = 'Kunne ikke sende inn skjemaet! Prøv igjen senere...'; + } else if ('non_field_errors' in errors) { errorFormatted = errors.non_field_errors?.[0]; } else if ('detail' in errors) { errorFormatted = errors.detail; } - if (!errorFormatted) errorFormatted = 'Kunne ikke sende inn skjemaet! Prøv igjen senere...'; + + if (!errorFormatted) { + return; + } return (