Skip to content

Commit

Permalink
fix: add exception handling and error logging when refreshing aws ses…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
kyle-seongwoo-jun committed Feb 18, 2024
1 parent 379e6cd commit 0843462
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/navien/navien.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import fetch from 'node-fetch';
import { URLSearchParams } from 'url';

import { API_URL, LOGIN_API_URL, USER_AGENT } from './constants';
import { AuthException } from './exceptions';
import { CommonResponse, Login2Response, LoginResponse, RefreshTokenResponse } from './interfaces';
import { ApiException, AuthException } from './exceptions';
import { CommonResponse, Login2Response, LoginResponse, RefreshTokenResponse, ResponseCode } from './interfaces';

export class NavienAuth {
constructor(
Expand Down Expand Up @@ -66,6 +66,10 @@ export class NavienAuth {
});

const json = await response.json() as Login2Response;
if (json.code !== ResponseCode.SUCCESS) {
throw ApiException.from(json);
}

return json;
}

Expand Down
14 changes: 12 additions & 2 deletions src/navien/navien.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ export class NavienService {
// refresh aws session and reconnect if connection is disrupted
if (connectionState === ConnectionState.ConnectionDisrupted) {
this.log.info('[AWS PubSub] Refreshing AWS session and reconnecting...');
const newSession = await this.sessionManager.refreshAwsSession();
pubsub.setSession(newSession);
try {
const newSession = await this.sessionManager.refreshAwsSession();
pubsub.setSession(newSession);
} catch (error) {
if (error instanceof ApiException) {
this.log.error('[AWS PubSub] APIException:', error.message);
return;
}
this.log.error('[AWS PubSub] Failed to refresh AWS session:', error);
}
}
});
}
Expand Down Expand Up @@ -86,6 +94,7 @@ export class NavienService {
} else {
this.log.error('Failed to set power to', power, 'for device', device.name);
}

}

public async setTemperature(device: NavienDevice, temperature: number) {
Expand All @@ -105,5 +114,6 @@ export class NavienService {
} else {
this.log.error('Failed to set temperature to', temperature, 'for device', device.name);
}

}
}

0 comments on commit 0843462

Please sign in to comment.