Skip to content

Commit

Permalink
Merge branch '3-confusing-error-message'
Browse files Browse the repository at this point in the history
  • Loading branch information
sajmonr committed Mar 2, 2023
2 parents a310702 + 6013c4b commit 296a2ea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/responses/account-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export interface AccountResponse{
accountAccess: {
account: {
accountId: string;
}
};
}[];
}
24 changes: 24 additions & 0 deletions src/responses/afero-error-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isNullOrUndefined } from '../utils';

export interface AferoErrorResponse{
/** UNIX timestamp of the request */
timestamp: number;
/** Request response code */
status: number;
/** Description of the error */
error_description: string;
/** URL of the request */
path: string;
}

/**
* Checks whether error is caused by Afero response
* @param error Error object to check
* @returns True if error is from Afero
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isAferoError(error: any): error is AferoErrorResponse{
if(error === null || typeof error !== 'object') return false;

return isNullOrUndefined(error.timestamp, error.status, error.status_description, error.path);
}
13 changes: 11 additions & 2 deletions src/services/device.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AxiosError, AxiosResponse } from 'axios';
import { DeviceStatusResponse } from '../responses/device-status-response';
import { CharacteristicValue } from 'homebridge';
import { convertNumberToHex } from '../utils';
import { isAferoError } from '../responses/afero-error-response';

/**
* Service for interacting with devices
Expand Down Expand Up @@ -35,7 +36,8 @@ export class DeviceService{
data: this.getDataValue(value)
});
}catch(ex){
this._platform.log.error('Remote service is not reachable.', (<AxiosError>ex).message);
this.handleError(<AxiosError>ex);

return;
}

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

return undefined;
}
Expand Down Expand Up @@ -117,4 +119,11 @@ export class DeviceService{
throw new Error('The value type is not supported.');
}

private handleError(error: AxiosError): void{
const responseData = error.response?.data;
const errorMessage = isAferoError(responseData) ? responseData.error_description : error.message;

this._platform.log.error('The remote service returned an error.', errorMessage);
}

}
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Checks whether value is null or undefined
* @param value Value to check
* @returns True if value is null or undefined otherwise false
* Checks whether at least one value is null or undefined
* @param values Values to check
* @returns True if any value is null or undefined otherwise false
*/
export function isNullOrUndefined(value: unknown): boolean{
return value === undefined || value === null;
export function isNullOrUndefined(...values: unknown[]): boolean{
return values.some(v => v === undefined || v === null);
}

/**
Expand Down

0 comments on commit 296a2ea

Please sign in to comment.