From 16db0cb84b0ec72c75517071728b681ad7719a5d Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Tue, 2 Nov 2021 20:59:12 +0100 Subject: [PATCH] Fixed error handling. --- CHANGELOG.md | 4 ++ Client.ts | 111 +++++++++++++++++++++++++++++++-------------------- package.json | 2 +- 3 files changed, 73 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ded960e..3b462a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.3.1 + +* fixed error handling + ## 1.3.0 * added endpoint to extend sessions diff --git a/Client.ts b/Client.ts index 2f2b6e0..b8ff64e 100644 --- a/Client.ts +++ b/Client.ts @@ -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, @@ -141,17 +141,21 @@ export class Client { } }); return Promise.resolve(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(e.response.data); } - return Promise.reject(e.response.data); + return Promise.reject(e); } } @@ -185,17 +189,21 @@ export class Client { } }); return Promise.resolve(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(e.response.data); } - return Promise.reject(e.response.data); + return Promise.reject(e); } } @@ -222,17 +230,21 @@ export class Client { } }); return Promise.resolve(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(e.response.data); } - return Promise.reject(e.response.data); + return Promise.reject(e); } } @@ -280,17 +292,21 @@ export class Client { } return Promise.resolve(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(e.response.data); } - return Promise.reject(e.response.data); + return Promise.reject(e); } } @@ -542,17 +558,21 @@ export class Client { params: filter }); return Promise.resolve(resp.data); - } catch(e) { - if(e.response.status === 401 && retry) { - try { - await this.refreshToken(); - return this.performGet(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(url, filter, false); + } catch(e) { + return e as APIError; + } } + + return Promise.reject(e.response.data); } - return Promise.reject(e.response.data); + return Promise.reject(e); } } @@ -564,9 +584,14 @@ export class Client { }); this.accessToken = resp.data.access_token; return Promise.resolve(null); - } catch(e) { + } catch(e: any) { this.accessToken = ""; - return Promise.reject(e.response.data); + + if(e.response) { + return Promise.reject(e.response.data); + } + + return Promise.reject(e); } } diff --git a/package.json b/package.json index 347ce2d..4447800 100644 --- a/package.json +++ b/package.json @@ -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",