diff --git a/app.ts b/app.ts index 7fe4f1a..59c4145 100644 --- a/app.ts +++ b/app.ts @@ -1,6 +1,6 @@ import { App } from 'homey' // eslint-disable-line import/no-extraneous-dependencies import axios from 'axios' -import withAPI, { getErrorMessage } from './mixins/withAPI' +import withAPI, { getApiErrorMessage } from './mixins/withAPI' import type { LoginCredentials, LoginData, @@ -9,6 +9,16 @@ import type { HomeySettingValue, } from './types' +function getErrorMessage(error: unknown): string { + let errorMessage = String(error) + if (axios.isAxiosError(error)) { + errorMessage = getApiErrorMessage(error) + } else if (error instanceof Error) { + errorMessage = error.message + } + return errorMessage +} + axios.defaults.baseURL = 'https://www.ariston-net.remotethermo.com/api/v2' export = class AristonApp extends withAPI(App) { @@ -50,13 +60,7 @@ export = class AristonApp extends withAPI(App) { this.refreshLogin(loginCredentials) return true } catch (error: unknown) { - let errorMessage = String(error) - if (axios.isAxiosError(error)) { - errorMessage = getErrorMessage(error) - } else if (error instanceof Error) { - errorMessage = error.message - } - throw new Error(errorMessage) + throw new Error(getErrorMessage(error)) } } diff --git a/mixins/withAPI.ts b/mixins/withAPI.ts index 4d2f544..e8a0da8 100644 --- a/mixins/withAPI.ts +++ b/mixins/withAPI.ts @@ -14,7 +14,7 @@ type APIClass = new (...args: any[]) => { api: AxiosInstance } -export function getErrorMessage(error: AxiosError): string { +export function getApiErrorMessage(error: AxiosError): string { return error.message } @@ -66,7 +66,11 @@ export default function withAPI(base: T): APIClass & T { type: 'request' | 'response', error: AxiosError, ): Promise { - this.error(`Error in ${type}:`, error.config?.url, getErrorMessage(error)) + this.error( + `Error in ${type}:`, + error.config?.url, + getApiErrorMessage(error), + ) await this.setErrorWarning(error) return Promise.reject(error) } @@ -75,7 +79,7 @@ export default function withAPI(base: T): APIClass & T { if (!this.setWarning) { return } - await this.setWarning(getErrorMessage(error)) + await this.setWarning(getApiErrorMessage(error)) } } }