From 760c5478505d69d7d4ffb0dfbc76f19958aef865 Mon Sep 17 00:00:00 2001 From: Ramon Candel Date: Thu, 16 May 2024 09:11:40 +0200 Subject: [PATCH] Added code field to normalize error middleware --- package.json | 2 +- src/shared/http/client.ts | 24 +++++++++++------------- src/shared/types/errors.ts | 7 ++++--- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index a6c565c9..653594e9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/shared/http/client.ts b/src/shared/http/client.ts index 58311e2f..808ccb38 100644 --- a/src/shared/http/client.ts +++ b/src/shared/http/client.ts @@ -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'; @@ -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; @@ -198,7 +196,7 @@ export class HttpClient { errorStatus = 400; } - throw new AppError(errorMessage, errorStatus); + throw new AppError(errorMessage, errorStatus, errorCode); } } diff --git a/src/shared/types/errors.ts b/src/shared/types/errors.ts index 6e385ce7..a67e1bba 100644 --- a/src/shared/types/errors.ts +++ b/src/shared/types/errors.ts @@ -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; } -} \ No newline at end of file +}