Skip to content
Snippets Groups Projects
Commit 13f71c60 authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

fix: handle error objects just containing error string arrays

Django rest framework would sometimes return objects like this

  { "email": ["Gib eine gültige E-Mail Adresse an."] }

which we didn’t properly handle.
parent 5256c765
No related branches found
No related tags found
No related merge requests found
......@@ -346,8 +346,14 @@ export function useServerErrors(error: Ref<Error | undefined>) {
if (status >= 400) {
if (_error.data !== null && typeof _error.data === 'object') {
for (const [field, _errors] of Object.entries(_error.data)) {
const errors = (Array.isArray(_errors) ? _errors : [_errors]) as ErrorDetail[]
result[field] = errors.map(({ message, code }) => ({ message, code: `server.${code}` }))
const errors = Array.isArray(_errors) ? _errors : [_errors]
result[field] = errors.map((err: string | ErrorDetail) => {
if (typeof err === 'string') {
return { message: err, code: '' }
} else {
return { message: err.message, code: `server.${err.code}` }
}
})
}
}
}
......
......@@ -5,7 +5,15 @@
<slot v-bind="controlAttributes" />
<div v-if="hasErrors" :id="errorsId" class="invalid-feedback tw-order-first">
<template v-for="(error, index) in errorList" :key="index">
<p class="last:tw-mb-0">{{ t(`error.${error.code}`) }}</p>
<p class="last:tw-mb-0">
{{
error.code
? t(`error.${error.code}`)
: error.message
? error.message
: t('error.unknown')
}}
</p>
</template>
</div>
</div>
......@@ -19,6 +27,7 @@ import { useI18n } from '@/i18n'
type Error = {
code: string
message?: string
}
const props = defineProps<{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment