Skip to content

Commit

Permalink
improve messages
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Nov 13, 2023
1 parent 4a36abc commit 3162fb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 12 additions & 8 deletions app.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
}
}

Expand Down
10 changes: 7 additions & 3 deletions mixins/withAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -66,7 +66,11 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
type: 'request' | 'response',
error: AxiosError,
): Promise<AxiosError> {
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)
}
Expand All @@ -75,7 +79,7 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
if (!this.setWarning) {
return
}
await this.setWarning(getErrorMessage(error))
await this.setWarning(getApiErrorMessage(error))
}
}
}

0 comments on commit 3162fb9

Please sign in to comment.