Skip to content

Commit

Permalink
Fix Unhandled Promise Rejections (#14)
Browse files Browse the repository at this point in the history
* Fix uncaught promise rejection during auth token refresh

* Fix unhandled promise rejection during auth token connect and upgrade

* Version 2.0.7
  • Loading branch information
samrum authored Jan 24, 2020
1 parent 07a8bff commit 67a1395
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "onstarjs",
"version": "2.0.6",
"version": "2.0.7",
"description": "Unofficial package for making OnStar API requests",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
28 changes: 19 additions & 9 deletions src/RequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,16 @@ class RequestService {

private async refreshAuthToken(): Promise<OAuthToken> {
if (!this.tokenRefreshPromise) {
this.tokenRefreshPromise = new Promise(async resolve => {
const token = await this.createNewAuthToken();
this.tokenRefreshPromise = new Promise(async (resolve, reject) => {
try {
const token = await this.createNewAuthToken();

resolve(token);
} catch (e) {
reject(e);
}

this.tokenRefreshPromise = undefined;
resolve(token);
});
}

Expand All @@ -272,16 +277,21 @@ class RequestService {

private async connectAndUpgradeAuthToken(): Promise<OAuthToken> {
if (!this.tokenUpgradePromise) {
this.tokenUpgradePromise = new Promise(async resolve => {
await this.connectRequest();
await this.upgradeRequest();
this.tokenUpgradePromise = new Promise(async (resolve, reject) => {
try {
await this.connectRequest();
await this.upgradeRequest();

if (this.authToken) {
this.authToken.upgraded = true;
}

if (this.authToken) {
this.authToken.upgraded = true;
resolve(this.authToken);
} catch (e) {
reject(e);
}

this.tokenUpgradePromise = undefined;
resolve(this.authToken);
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const onStar = OnStar.create(config);
);
}
} catch (e) {
console.error(e.message);
console.error("Functional Test Error", e.message);

if (e.request) {
console.error(`Request: ${e.request.path}`);
Expand Down

0 comments on commit 67a1395

Please sign in to comment.