Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
fix: axios error
Browse files Browse the repository at this point in the history
fixes #231
  • Loading branch information
everhardt committed Jun 3, 2022
1 parent 4e90f58 commit ea500c2
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions src/comms/index.ts
Expand Up @@ -5,6 +5,7 @@ import Axios, {
CancelTokenSource,
AxiosRequestConfig,
Method,
AxiosError,
} from 'axios';

import CommsRequestError from '../errors/comms-request';
Expand Down Expand Up @@ -96,37 +97,48 @@ class Comms {
// is a wtg error already, eg. from axios interceptors
throw e;
}
if (Axios.isCancel(e)) {
throw new CommsCanceledError();
}
if (typeof e.response === 'object' && e.response !== null) {
if (e.response.status === 401) {
throw new AuthenticationError(e.response.data.key, e.response.data.message);
if (Axios.isAxiosError(e)) {
if (Axios.isCancel(e)) {
throw new CommsCanceledError();
}
if (e.response.status === 403 && e.response.data.key === 'outdated_client_error') {
throw new OutdatedClientError();
if (e.response !== undefined) {
const responseData = e.response.data;
let rawKey: unknown;
let rawMessage: unknown;
if (typeof responseData === 'object' && responseData !== null) {
rawKey = (responseData as Record<string, unknown>)?.key;
rawMessage = (responseData as Record<string, unknown>)?.message;
}
const key = typeof rawKey === 'string' ? rawKey : 'unknown';
const message = typeof rawMessage === 'string' ? rawMessage : 'unknown';
if (e.response.status === 401) {
throw new AuthenticationError(key, message);
}
if (e.response.status === 403 && key === 'outdated_client_error') {
throw new OutdatedClientError();
}
if (e.response.status === 402) {
throw new BillingError(key, message);
}

throw new CommsResponseError(
e.response.data,
e.response.status,
e.response.headers,
method,
path,
params,
data,
);
}
if (e.response.status === 402) {
throw new BillingError(e.response.data.key, e.response.data.message);
if (e.code === AxiosError.ERR_NETWORK && e.request !== undefined) {
throw new CommsRequestError(
method,
path,
params,
data,
);
}

throw new CommsResponseError(
e.response.data,
e.response.status,
e.response.headers,
method,
path,
params,
data,
);
}
if (e.request !== undefined) {
throw new CommsRequestError(
method,
path,
params,
data,
);
}
throw new CommsError(e.message);
}
Expand Down

0 comments on commit ea500c2

Please sign in to comment.