Skip to content

Commit

Permalink
Simplify errorInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Nov 8, 2023
1 parent c81bfe8 commit ce864bb
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/lib/seam/connect/error-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
export const errorInterceptor = async (err: unknown): Promise<void> => {
if (!isAxiosError(err)) throw err

const status = err.response?.status
const headers = err.response?.headers
const { response } = err
const status = response?.status
const headers = response?.headers
const requestId = headers?.['seam-request-id'] ?? ''

if (status == null) throw err
Expand All @@ -20,10 +21,7 @@ export const errorInterceptor = async (err: unknown): Promise<void> => {
throw new SeamHttpUnauthorizedError(requestId)
}

if (!isApiErrorResponse(err)) throw err

const { response } = err
if (response == null) throw err
if (!isApiErrorResponse(response)) throw err

const { type } = response.data.error

Expand All @@ -34,9 +32,11 @@ export const errorInterceptor = async (err: unknown): Promise<void> => {
}

const isApiErrorResponse = (
err: AxiosError,
): err is AxiosError<ApiErrorResponse> => {
const headers = err.response?.headers
response: AxiosError['response'],
): response is NonNullable<AxiosError<ApiErrorResponse>['response']> => {
if (response == null) return false
const { headers, data } = response

if (headers == null) return false

const contentType = headers['content-type']
Expand All @@ -47,7 +47,6 @@ const isApiErrorResponse = (
return false
}

const data = err.response?.data
if (typeof data === 'object' && data != null) {
return (
'error' in data &&
Expand Down

0 comments on commit ce864bb

Please sign in to comment.