Skip to content

Commit

Permalink
Merge pull request #204 from internxt/feature/PB-2100-handle-402-errors
Browse files Browse the repository at this point in the history
Added code field to normalize error middleware
  • Loading branch information
CandelR authored May 16, 2024
2 parents c04fa62 + 760c547 commit c3ade93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internxt/sdk",
"version": "1.4.81",
"version": "1.4.82",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
24 changes: 11 additions & 13 deletions src/shared/http/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { Axios, AxiosError, AxiosResponse, CancelToken } from 'axios';
import { Headers, URL, RequestCanceler, Parameters, UnauthorizedCallback } from './types';
import AppError from '../types/errors';
import { Headers, Parameters, RequestCanceler, URL, UnauthorizedCallback } from './types';

export { RequestCanceler } from './types';

Expand Down Expand Up @@ -173,23 +173,21 @@ export class HttpClient {
* @private
*/
private normalizeError(error: AxiosError) {
let errorMessage: string, errorStatus: number;
let errorMessage: string, errorStatus: number, errorCode: string | undefined;

if (error.response) {
const response = error.response as AxiosResponse<{ error: string, message: string, statusCode: number }>;
const response = error.response as AxiosResponse<{
error: string;
message: string;
statusCode: number;
code?: string;
}>;
if (response.status === 401) {
this.unauthorizedCallback();
}
if (response.data.message !== undefined) {
errorMessage = response.data.message;
} else if (response.data.error !== undefined) {
errorMessage = response.data.error;
} else {
// TODO : remove when endpoints of updateMetadata(file/folder) are updated
// after all clients use th SDK
errorMessage = JSON.stringify(response.data);
}
errorMessage = response.data.message || response.data.error || JSON.stringify(response.data);
errorStatus = response.status;
errorCode = response.data.code;
} else if (error.request) {
errorMessage = 'Server unavailable';
errorStatus = 500;
Expand All @@ -198,7 +196,7 @@ export class HttpClient {
errorStatus = 400;
}

throw new AppError(errorMessage, errorStatus);
throw new AppError(errorMessage, errorStatus, errorCode);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/shared/types/errors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

export default class AppError extends Error {
public readonly status?: number;
public readonly code?: string;

constructor(message: string, status?: number) {
constructor(message: string, status?: number, code?: string) {
super(message);

this.status = status;
this.code = code;
}
}
}

0 comments on commit c3ade93

Please sign in to comment.