From f03fe3eb24a2ea4d91abf03fdb7ac9e27708c691 Mon Sep 17 00:00:00 2001 From: vgeffer Date: Sat, 14 Dec 2024 13:06:51 +0100 Subject: [PATCH] RA extracts error messages from AxiosError --- src/components/Admin/dataProvider.ts | 112 ++++++++++++++++++--------- 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/src/components/Admin/dataProvider.ts b/src/components/Admin/dataProvider.ts index ffb16583..652b6231 100644 --- a/src/components/Admin/dataProvider.ts +++ b/src/components/Admin/dataProvider.ts @@ -1,4 +1,4 @@ -import axios from 'axios' +import axios, {AxiosError, AxiosResponse} from 'axios' import {stringify} from 'querystring' import {DataProvider, FilterPayload, /* PaginationPayload, */ RaRecord, SortPayload} from 'react-admin' @@ -13,6 +13,14 @@ const getOrderingQuery = ({order, field}: SortPayload) => ({ordering: `${order = // limit: perPage, // }) +/* field errors by mali byt zachytene uz FE validaciou, dumpujem do 'Nastala neznáma chyba' */ +const parseError = (response: any) => { + if (typeof response === 'undefined') return 'Nastala neznáma chyba' + if (typeof response.detail !== 'undefined') return response.detail + if (response.non_field_errors?.constructor.name === 'Array') return response.non_field_errors.join('\n') + return 'Nastala neznáma chyba' +} + const apiUrl = '/api' // skopirovane a dost upravene z https://github.com/bmihelac/ra-data-django-rest-framework/blob/master/src/index.ts @@ -25,40 +33,55 @@ export const dataProvider: DataProvider = { // ...getPaginationQuery(pagination), } const stringifiedQuery = stringify(query) - const {data} = await axios.get(`${apiUrl}/${resource}${stringifiedQuery ? `/?${stringifiedQuery}` : ''}`) - // client-side pagination - let pagedData = data - if (pagination) { - const {page, perPage} = pagination - pagedData = data.slice((page - 1) * perPage, page * perPage) - } - - return { - data: pagedData, - total: data.length, + try { + const {data} = await axios.get(`${apiUrl}/${resource}${stringifiedQuery ? `/?${stringifiedQuery}` : ''}`) + + // client-side pagination + let pagedData = data + if (pagination) { + const {page, perPage} = pagination + pagedData = data.slice((page - 1) * perPage, page * perPage) + } + + return { + data: pagedData, + total: data.length, + } + } catch (e: any) { + throw new Error(parseError(e.response.data)) } }, getOne: async (resource, params) => { - const {data} = await axios.get(`${apiUrl}/${resource}/${params.id}`) - - return {data} + try { + const {data} = await axios.get(`${apiUrl}/${resource}/${params.id}`) + return {data} + } catch (e: any) { + throw new Error(parseError(e.response.data)) + } }, getMany: async (resource, params) => { - const data = await Promise.all(params.ids.map((id) => axios.get(`${apiUrl}/${resource}/${id}`))) - - return {data: data.map(({data}) => data)} + try { + const data = await Promise.all(params.ids.map((id) => axios.get(`${apiUrl}/${resource}/${id}`))) + return {data: data.map(({data}) => data)} + } catch (e: any) { + throw new Error(parseError(e.response.data)) + } }, // TODO: ak budeme pouzivat tuto funkciu, upravime podla getList (pagination, sort, filter). uprimne este neviem, pri com sa pouziva getManyReference: async (resource, params) => { const query = { [params.target]: params.id, } - const {data} = await axios.get(`${apiUrl}/${resource}/?${stringify(query)}`) - return { - data: data, - total: data.length, + try { + const {data} = await axios.get(`${apiUrl}/${resource}/?${stringify(query)}`) + return { + data: data, + total: data.length, + } + } catch (e: any) { + throw new Error(parseError(e.response.data)) } }, update: async (resource, params) => { @@ -68,32 +91,47 @@ export const dataProvider: DataProvider = { // ked existuju formData, ktore sme do recordu pridali v `transform` v `MyEdit`, pouzijeme tie const body = formData ?? input - const {data} = await axios.patch(`${apiUrl}/${resource}/${id}`, body) - - return {data} + try { + const {data} = await axios.patch(`${apiUrl}/${resource}/${id}`, body) + return {data} + } catch (e: any) { + throw new Error(parseError(e.response.data)) + } }, updateMany: async (resource, params) => { - const data = await Promise.all(params.ids.map((id) => axios.patch(`${apiUrl}/${resource}/${id}`, params.data))) - - return {data: data.map(({data}) => data)} + try { + const data = await Promise.all(params.ids.map((id) => axios.patch(`${apiUrl}/${resource}/${id}`, params.data))) + return {data: data.map(({data}) => data)} + } catch (e: any) { + throw new Error(parseError(e.response.data)) + } }, create: async (resource, params) => { const {formData, ...input} = params.data const body = formData ?? input - const {data} = await axios.post(`${apiUrl}/${resource}`, body) - - return {data} + try { + const {data} = await axios.post(`${apiUrl}/${resource}`, body) + return {data} + } catch (e: any) { + throw new Error(parseError(e.response.data)) + } }, delete: async (resource, params) => { - const {data} = await axios.delete(`${apiUrl}/${resource}/${params.id}`) - - return {data} + try { + const {data} = await axios.delete(`${apiUrl}/${resource}/${params.id}`) + return {data} + } catch (e: any) { + throw new Error(parseError(e.response.data)) + } }, deleteMany: async (resource, params) => { - const data = await Promise.all(params.ids.map((id) => axios.delete(`${apiUrl}/${resource}/${id}`))) - - return {data: data.map(({data}) => data.id)} + try { + const data = await Promise.all(params.ids.map((id) => axios.delete(`${apiUrl}/${resource}/${id}`))) + return {data: data.map(({data}) => data.id)} + } catch (e: any) { + throw new Error(parseError(e.response.data)) + } }, }