Skip to content

Commit

Permalink
Improve error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanNienhuis committed Jan 8, 2022
1 parent 549cee2 commit a28f8f3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 58 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 94 additions & 58 deletions src/bold.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,132 @@
import axios, { Method } from 'axios';
import axios, { AxiosError, Method } from 'axios';
import { Logger } from 'homebridge';
import { Device } from './types';

interface APISuccess<Data> {
success: true;
data: Data;
}

interface APIError {
success: false;
error: {
code?: string | number;
message: string;
}
}

type APIResponse<Data> = APISuccess<Data> | APIError;

export class BoldAPI {

constructor(
private authToken: string,
private log: Logger
) {}

private async request(method: Method, endpoint: string) {
return await axios({
method: method,
url: `https://api.sesamtechnology.com${endpoint}`,
headers: {
'X-Auth-Token': this.authToken,
'Content-Type': 'application/json'
}
});
}

async getDevices(): Promise<Device[]> {
private async request(method: Method, endpoint: string): Promise<APIResponse<any>> {
try {
let response = await this.request('GET', '/v1/effective-device-permissions');

if (Array.isArray(response.data)) {
let devices = response.data as Device[];

return devices.filter((device) => device.id != null && device.name && device.gateway != null);
} else {
this.log.error(`Unknown reponse while getting devices: ${response.data}`);
return [];
let response = await axios.request<any>({
method: method,
url: `https://api.sesamtechnology.com${endpoint}`,
headers: {
'X-Auth-Token': this.authToken,
'Content-Type': 'application/json'
}
});

if (response.data.errorCode != null || response.data.errorMessage != null) {
return {
success: false,
error: {
code: response.data.errorCode,
message: response.data.errorMessage || 'Unknown error'
}
};
}

return {
success: true,
data: response.data
};
} catch (error) {
if (axios.isAxiosError(error)) {
this.log.error(`Request error while getting devices: ${error}`);

if (error.response?.data) {
this.log.error(error.response.data);
}
let axiosError = error as AxiosError<any>;

return {
success: false,
error: {
code: axiosError.response?.data.errorCode || axiosError.response?.status || axiosError.code,
message: axiosError.response?.data.errorMessage || `${axiosError}`
}
};
} else {
this.log.error(`Unknown error while getting devices: ${error}`);
return {
success: false,
error: {
message: `${error}`
}
};
}
}
}

async getDevices(): Promise<Device[]> {
this.log.debug('Getting all devices');

let response = await this.request('GET', '/v1/effective-device-permissions');

if (!response.success) {
this.log.error(`Error ${response.error.code ? `(${response.error.code}) ` : ''} while getting devices: ${response.error.message}`);

return [];
}

if (Array.isArray(response.data)) {
let devices = response.data as Device[];
let supportedDevices = devices.filter((device) => device.id != null && device.name && device.gateway != null);

console.debug(`Total device count: ${devices.length}, Supported device count: ${supportedDevices.length}`);

return supportedDevices;
} else {
this.log.error(`Unknown reponse while getting devices: ${response.data}`);
return [];
}
}

async activate(deviceId: number): Promise<boolean> {
this.log.debug(`Activating device with id ${deviceId}`);
this.log.debug(`Activating device (${deviceId})`);

try {
await this.request('POST', `/v1/devices/${deviceId}/remote-activation`);
let response = await this.request('POST', `/v1/devices/${deviceId}/remote-activation`);

return true;
} catch (error) {
if (axios.isAxiosError(error)) {
this.log.error(`Request error while activating device with id ${deviceId}: ${error}`);

if (error.response?.data) {
this.log.error(error.response.data);
}
} else {
this.log.error(`Unknown error while activating device with id ${deviceId}: ${error}`);
}
if (!response.success) {
this.log.error(`Error ${response.error.code ? `(${response.error.code}) ` : ''} while activating device (${deviceId}): ${response.error.message}`);

return false;
}

this.log.debug(`Successfully activated device (${deviceId})`);
return true;
}

async refreshToken(): Promise<string | undefined> {
this.log.debug('Refreshing auth token');

try {
let response = await this.request('PUT', `/v1/authentications/${this.authToken}`);
let data = response.data as any;
let response = await this.request('PUT', `/v1/authentications/${this.authToken}`);

this.authToken = data.token;
if (!response.success) {
this.log.error(`Error ${response.error.code ? `(${response.error.code}) ` : ''} while refreshing token: ${response.error.message}`);

this.log.debug('Successfully refreshed auth token');
return data.token;
} catch (error) {
if (axios.isAxiosError(error)) {
this.log.error(`Request error while refreshing auth token: ${error}`);

if (error.response?.data) {
this.log.error(error.response.data);
}
} else {
this.log.error(`Unknown error while refreshing auth token: ${error}`);
}
return;
}

let data = response.data as any;

this.authToken = data.token;

this.log.debug('Successfully refreshed auth token');
return data.token;
}

}

0 comments on commit a28f8f3

Please sign in to comment.