Skip to content

Commit

Permalink
Fixed error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Nov 2, 2021
1 parent 452be73 commit 16db0cb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.3.1

* fixed error handling

## 1.3.0

* added endpoint to extend sessions
Expand Down
111 changes: 68 additions & 43 deletions Client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage } from "http";
import { URL } from "url";
import axios, { AxiosInstance } from "axios";
import axios, { AxiosError, AxiosInstance } from "axios";
import {
ClientConfig,
AuthenticationResponse,
Expand Down Expand Up @@ -141,17 +141,21 @@ export class Client {
}
});
return Promise.resolve<null>(null);
} catch(e) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.hit(hit, false);
} catch(e) {
return e;
} catch(e: any) {
if(e.response) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.hit(hit, false);
} catch(e) {
return e as APIError;
}
}

return Promise.reject<APIError>(e.response.data);
}

return Promise.reject<APIError>(e.response.data);
return Promise.reject(e);
}
}

Expand Down Expand Up @@ -185,17 +189,21 @@ export class Client {
}
});
return Promise.resolve<null>(null);
} catch(e) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.hit(hit, false);
} catch(e) {
return e;
} catch(e: any) {
if(e.response) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.hit(hit, false);
} catch(e) {
return e as APIError;
}
}

return Promise.reject<APIError>(e.response.data);
}

return Promise.reject<APIError>(e.response.data);
return Promise.reject(e);
}
}

Expand All @@ -222,17 +230,21 @@ export class Client {
}
});
return Promise.resolve<null>(null);
} catch(e) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.session(hit, false);
} catch(e) {
return e;
} catch(e: any) {
if(e.response) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.session(hit, false);
} catch(e) {
return e as APIError;
}
}

return Promise.reject<APIError>(e.response.data);
}

return Promise.reject<APIError>(e.response.data);
return Promise.reject(e);
}
}

Expand Down Expand Up @@ -280,17 +292,21 @@ export class Client {
}

return Promise.resolve<Domain>(resp.data);
} catch(e) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.domain(false);
} catch(e) {
return e;
} catch(e: any) {
if(e.response) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.domain(false);
} catch(e) {
return e as APIError;
}
}

return Promise.reject<APIError>(e.response.data);
}

return Promise.reject<APIError>(e.response.data);
return Promise.reject(e);
}
}

Expand Down Expand Up @@ -542,17 +558,21 @@ export class Client {
params: filter
});
return Promise.resolve<T>(resp.data);
} catch(e) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.performGet<T>(url, filter, false);
} catch(e) {
return e;
} catch(e: any) {
if(e.response) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.performGet<T>(url, filter, false);
} catch(e) {
return e as APIError;
}
}

return Promise.reject<APIError>(e.response.data);
}

return Promise.reject<APIError>(e.response.data);
return Promise.reject(e);
}
}

Expand All @@ -564,9 +584,14 @@ export class Client {
});
this.accessToken = resp.data.access_token;
return Promise.resolve<null>(null);
} catch(e) {
} catch(e: any) {
this.accessToken = "";
return Promise.reject<APIError>(e.response.data);

if(e.response) {
return Promise.reject<APIError>(e.response.data);
}

return Promise.reject(e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pirsch-sdk",
"version": "1.3.0",
"version": "1.3.1",
"description": "JavaScript client SDK for Pirsch.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 16db0cb

Please sign in to comment.