Skip to content

Commit

Permalink
Merge pull request #95 from permitio/gidi/per-10648-issue-with-the-er…
Browse files Browse the repository at this point in the history
…ror-handling-of-node-sdk

aligned the log message error anf the permit-api-error
  • Loading branch information
gideonsmila authored Sep 19, 2024
2 parents 7c66cc5 + 794dbc3 commit 75174e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/*
.nyc_output
.env
build
node_modules
test
Expand All @@ -13,3 +14,4 @@ src/openapi/.gitignore
src/openapi/.npmignore
src/openapi/.openapi-generator-ignore
src/openapi/git_push.sh

24 changes: 21 additions & 3 deletions src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ 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<T> extends Error {
constructor(message: string, public originalError: AxiosError<T>) {
super(message);
}

public get formattedAxiosError(): FormattedAxiosError {
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;
}
Expand Down Expand Up @@ -165,11 +180,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}`;
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}, err: ${JSON.stringify(err?.response?.data)}`);
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;
Expand Down

0 comments on commit 75174e2

Please sign in to comment.