Skip to content

Commit 296a2ea

Browse files
committed
Merge branch '3-confusing-error-message'
2 parents a310702 + 6013c4b commit 296a2ea

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

src/responses/account-response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export interface AccountResponse{
55
accountAccess: {
66
account: {
77
accountId: string;
8-
}
8+
};
99
}[];
1010
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { isNullOrUndefined } from '../utils';
2+
3+
export interface AferoErrorResponse{
4+
/** UNIX timestamp of the request */
5+
timestamp: number;
6+
/** Request response code */
7+
status: number;
8+
/** Description of the error */
9+
error_description: string;
10+
/** URL of the request */
11+
path: string;
12+
}
13+
14+
/**
15+
* Checks whether error is caused by Afero response
16+
* @param error Error object to check
17+
* @returns True if error is from Afero
18+
*/
19+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20+
export function isAferoError(error: any): error is AferoErrorResponse{
21+
if(error === null || typeof error !== 'object') return false;
22+
23+
return isNullOrUndefined(error.timestamp, error.status, error.status_description, error.path);
24+
}

src/services/device.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AxiosError, AxiosResponse } from 'axios';
66
import { DeviceStatusResponse } from '../responses/device-status-response';
77
import { CharacteristicValue } from 'homebridge';
88
import { convertNumberToHex } from '../utils';
9+
import { isAferoError } from '../responses/afero-error-response';
910

1011
/**
1112
* Service for interacting with devices
@@ -35,7 +36,8 @@ export class DeviceService{
3536
data: this.getDataValue(value)
3637
});
3738
}catch(ex){
38-
this._platform.log.error('Remote service is not reachable.', (<AxiosError>ex).message);
39+
this.handleError(<AxiosError>ex);
40+
3941
return;
4042
}
4143

@@ -59,7 +61,7 @@ export class DeviceService{
5961
.get<DeviceStatusResponse>(`accounts/${this._platform.accountService.accountId}/devices/${deviceId}?expansions=attributes`);
6062
deviceStatus = response.data;
6163
}catch(ex){
62-
this._platform.log.error('Remote service returned an error.', (<AxiosError>ex).message);
64+
this.handleError(<AxiosError>ex);
6365

6466
return undefined;
6567
}
@@ -117,4 +119,11 @@ export class DeviceService{
117119
throw new Error('The value type is not supported.');
118120
}
119121

122+
private handleError(error: AxiosError): void{
123+
const responseData = error.response?.data;
124+
const errorMessage = isAferoError(responseData) ? responseData.error_description : error.message;
125+
126+
this._platform.log.error('The remote service returned an error.', errorMessage);
127+
}
128+
120129
}

src/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* Checks whether value is null or undefined
3-
* @param value Value to check
4-
* @returns True if value is null or undefined otherwise false
2+
* Checks whether at least one value is null or undefined
3+
* @param values Values to check
4+
* @returns True if any value is null or undefined otherwise false
55
*/
6-
export function isNullOrUndefined(value: unknown): boolean{
7-
return value === undefined || value === null;
6+
export function isNullOrUndefined(...values: unknown[]): boolean{
7+
return values.some(v => v === undefined || v === null);
88
}
99

1010
/**

0 commit comments

Comments
 (0)