Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Nov 13, 2023
1 parent 3162fb9 commit 834c892
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 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, { getApiErrorMessage } from './mixins/withAPI'
import withAPI, { getAPIErrorMessage } from './mixins/withAPI'
import type {
LoginCredentials,
LoginData,
Expand All @@ -9,18 +9,18 @@ import type {
HomeySettingValue,
} from './types'

axios.defaults.baseURL = 'https://www.ariston-net.remotethermo.com/api/v2'

function getErrorMessage(error: unknown): string {
let errorMessage = String(error)
if (axios.isAxiosError(error)) {
errorMessage = getApiErrorMessage(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) {
#loginTimeout!: NodeJS.Timeout

Expand Down
6 changes: 3 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 getApiErrorMessage(error: AxiosError): string {
export function getAPIErrorMessage(error: AxiosError): string {
return error.message
}

Expand Down Expand Up @@ -69,7 +69,7 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
this.error(
`Error in ${type}:`,
error.config?.url,
getApiErrorMessage(error),
getAPIErrorMessage(error),
)
await this.setErrorWarning(error)
return Promise.reject(error)
Expand All @@ -79,7 +79,7 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
if (!this.setWarning) {
return
}
await this.setWarning(getApiErrorMessage(error))
await this.setWarning(getAPIErrorMessage(error))
}
}
}

0 comments on commit 834c892

Please sign in to comment.