Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add axios error handler that handles drf messages #324

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isAxiosError, AxiosError } from 'axios'

export function handleDRFAPIErrorsOrReThrow(error: AxiosError | Error): string[]{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say error is unknown

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AxiosError type is inferred by the isAxiosError assert function on it's own, no need to declare it at all

if (isAxiosError(error)){
const e: AxiosError = error

const { data } = (e as AxiosError)?.response ?? null
Comment on lines +5 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to cast here. In neither of the two lines

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 error as Error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether the cast does anything at all here 🤔

}

Loading