From bc69c6e76a0f2f28ef46a82a91c6b73ae01ad122 Mon Sep 17 00:00:00 2001 From: paribaker <58012003+paribaker@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:38:32 +0000 Subject: [PATCH 1/4] add axios error handler that handles drf messages --- .../web/react/src/utils/server-errors.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 {{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts new file mode 100644 index 000000000..ebc15ca17 --- /dev/null +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts @@ -0,0 +1,21 @@ +import { isAxiosError, AxiosError } from 'axios' + +export function handleDRFAPIErrorsOrReThrow(e: AxiosError | Error): string[]{ + if (isAxiosError(e as AxiosError)){ + const { data } = e?.response ?? {} + if (data) { + const isArrayOfStrings = + Array.isArray(data) && data.length && typeof data[0] === 'string' + const isObjectOfErrors = Object.keys(data).every((key) => Array.isArray(data[key])) + return (isArrayOfStrings + ? data + : isObjectOfErrors + ? Object.keys(data).map((key) => data[key]) + : ['Something went wrong']) as string[] + + } + return [] + } + throw e as Error +} + From 659007fe1a115790653348490629efe1d7dc33b7 Mon Sep 17 00:00:00 2001 From: paribaker <58012003+paribaker@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:13:48 +0000 Subject: [PATCH 2/4] updated type --- .../clients/web/react/src/utils/server-errors.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts index ebc15ca17..e8c259579 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts @@ -1,7 +1,9 @@ import { isAxiosError, AxiosError } from 'axios' -export function handleDRFAPIErrorsOrReThrow(e: AxiosError | Error): string[]{ - if (isAxiosError(e as AxiosError)){ +export function handleDRFAPIErrorsOrReThrow(error: AxiosError | Error): string[]{ + if (isAxiosError(error)){ + const e: AxiosError = error + const { data } = e?.response ?? {} if (data) { const isArrayOfStrings = @@ -16,6 +18,6 @@ export function handleDRFAPIErrorsOrReThrow(e: AxiosError | Error): string[]{ } return [] } - throw e as Error + throw error as Error } From 97cdbf39327ddc9fac6dbb4451e45d5a748b34b5 Mon Sep 17 00:00:00 2001 From: paribaker <58012003+paribaker@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:48:55 +0000 Subject: [PATCH 3/4] or null --- .../clients/web/react/src/utils/server-errors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts index e8c259579..fa852a549 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts @@ -4,7 +4,7 @@ export function handleDRFAPIErrorsOrReThrow(error: AxiosError | Error): string[] if (isAxiosError(error)){ const e: AxiosError = error - const { data } = e?.response ?? {} + const { data } = e?.response ?? null if (data) { const isArrayOfStrings = Array.isArray(data) && data.length && typeof data[0] === 'string' From 4b2ba393e51ed7c533c5c0fe8f64862ee7cc75a6 Mon Sep 17 00:00:00 2001 From: paribaker <58012003+paribaker@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:59:12 +0000 Subject: [PATCH 4/4] try typing error --- .../clients/web/react/src/utils/server-errors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts index fa852a549..d88b6056f 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/server-errors.ts @@ -4,7 +4,7 @@ export function handleDRFAPIErrorsOrReThrow(error: AxiosError | Error): string[] if (isAxiosError(error)){ const e: AxiosError = error - const { data } = e?.response ?? null + const { data } = (e as AxiosError)?.response ?? null if (data) { const isArrayOfStrings = Array.isArray(data) && data.length && typeof data[0] === 'string'