From ea920d2f53e7a020f845cdb3b2a2b9e26f8c9d8a Mon Sep 17 00:00:00 2001 From: gideonsmila Date: Tue, 17 Sep 2024 10:46:59 +0300 Subject: [PATCH 1/3] aligned the log message error anf the permit-api-error --- .gitignore | 2 ++ src/api/base.ts | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e5888da..4688981 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea/* .nyc_output +.env build node_modules test @@ -13,3 +14,4 @@ src/openapi/.gitignore src/openapi/.npmignore src/openapi/.openapi-generator-ignore src/openapi/git_push.sh + diff --git a/src/api/base.ts b/src/api/base.ts index 4266192..6475f25 100644 --- a/src/api/base.ts +++ b/src/api/base.ts @@ -165,9 +165,11 @@ export abstract class BasePermitApi { protected handleApiError(err: unknown): never { if (axios.isAxiosError(err)) { // this is an http response with an error status code - const message = `Got error status code: ${err.response?.status}`; + const message = `Got error status code: ${err.response?.status}, err: ${JSON.stringify( + err?.response?.data, + )}`; // log this to the SDK logger - this.logger.error(`${message}, err: ${JSON.stringify(err?.response?.data)}`); + this.logger.error(message); // and throw a permit error exception throw new PermitApiError(message, err); } else { From a0a833484927bd361b84594e8bb9ac70792a87c8 Mon Sep 17 00:00:00 2001 From: gideonsmila Date: Thu, 19 Sep 2024 07:48:53 +0300 Subject: [PATCH 2/3] add formatted axios error --- src/api/base.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/api/base.ts b/src/api/base.ts index 6475f25..e905b8e 100644 --- a/src/api/base.ts +++ b/src/api/base.ts @@ -12,6 +12,15 @@ export class PermitApiError extends Error { super(message); } + public get formattedAxiosError(): any { + return { + code: this.originalError.code, + message: this.message, + error: this.originalError.response?.data, + status: this.originalError.status, + }; + } + public get request(): any { return this.originalError.request; } @@ -165,13 +174,14 @@ export abstract class BasePermitApi { protected handleApiError(err: unknown): never { if (axios.isAxiosError(err)) { // this is an http response with an error status code - const message = `Got error status code: ${err.response?.status}, err: ${JSON.stringify( + const logMessage = `Got error status code: ${err.response?.status}, err: ${JSON.stringify( err?.response?.data, )}`; + const apiMessage = err.response?.data.message; // log this to the SDK logger - this.logger.error(message); + this.logger.error(logMessage); // and throw a permit error exception - throw new PermitApiError(message, err); + throw new PermitApiError(apiMessage, err); } else { // unexpected error, just throw throw err; From 794dbc3e9781e3ae4473fe4b475451e25e6f57dd Mon Sep 17 00:00:00 2001 From: gideonsmila Date: Thu, 19 Sep 2024 09:59:49 +0300 Subject: [PATCH 3/3] Add return interface to formattedAxiosError getterr --- src/api/base.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/api/base.ts b/src/api/base.ts index e905b8e..5e94cdd 100644 --- a/src/api/base.ts +++ b/src/api/base.ts @@ -7,12 +7,18 @@ import { BASE_PATH } from '../openapi/base'; import { API_ACCESS_LEVELS, ApiContextLevel, ApiKeyLevel, PermitContextError } from './context'; +interface FormattedAxiosError { + code: string | undefined; + message: string; + error: any; + status: number | undefined; +} export class PermitApiError extends Error { constructor(message: string, public originalError: AxiosError) { super(message); } - public get formattedAxiosError(): any { + public get formattedAxiosError(): FormattedAxiosError { return { code: this.originalError.code, message: this.message,