Skip to content

Commit 75174e2

Browse files
authored
Merge pull request #95 from permitio/gidi/per-10648-issue-with-the-error-handling-of-node-sdk
aligned the log message error anf the permit-api-error
2 parents 7c66cc5 + 794dbc3 commit 75174e2

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/*
22
.nyc_output
3+
.env
34
build
45
node_modules
56
test
@@ -13,3 +14,4 @@ src/openapi/.gitignore
1314
src/openapi/.npmignore
1415
src/openapi/.openapi-generator-ignore
1516
src/openapi/git_push.sh
17+

src/api/base.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,26 @@ import { BASE_PATH } from '../openapi/base';
77

88
import { API_ACCESS_LEVELS, ApiContextLevel, ApiKeyLevel, PermitContextError } from './context';
99

10+
interface FormattedAxiosError {
11+
code: string | undefined;
12+
message: string;
13+
error: any;
14+
status: number | undefined;
15+
}
1016
export class PermitApiError<T> extends Error {
1117
constructor(message: string, public originalError: AxiosError<T>) {
1218
super(message);
1319
}
1420

21+
public get formattedAxiosError(): FormattedAxiosError {
22+
return {
23+
code: this.originalError.code,
24+
message: this.message,
25+
error: this.originalError.response?.data,
26+
status: this.originalError.status,
27+
};
28+
}
29+
1530
public get request(): any {
1631
return this.originalError.request;
1732
}
@@ -165,11 +180,14 @@ export abstract class BasePermitApi {
165180
protected handleApiError(err: unknown): never {
166181
if (axios.isAxiosError(err)) {
167182
// this is an http response with an error status code
168-
const message = `Got error status code: ${err.response?.status}`;
183+
const logMessage = `Got error status code: ${err.response?.status}, err: ${JSON.stringify(
184+
err?.response?.data,
185+
)}`;
186+
const apiMessage = err.response?.data.message;
169187
// log this to the SDK logger
170-
this.logger.error(`${message}, err: ${JSON.stringify(err?.response?.data)}`);
188+
this.logger.error(logMessage);
171189
// and throw a permit error exception
172-
throw new PermitApiError(message, err);
190+
throw new PermitApiError(apiMessage, err);
173191
} else {
174192
// unexpected error, just throw
175193
throw err;

0 commit comments

Comments
 (0)