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 834c892 commit 6bf6890
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 1 addition & 11 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, { getErrorMessage } from './mixins/withAPI'
import type {
LoginCredentials,
LoginData,
Expand All @@ -11,16 +11,6 @@ import type {

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)
} else if (error instanceof Error) {
errorMessage = error.message
}
return errorMessage
}

export = class AristonApp extends withAPI(App) {
#loginTimeout!: NodeJS.Timeout

Expand Down
10 changes: 10 additions & 0 deletions mixins/withAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export function getAPIErrorMessage(error: AxiosError): string {
return error.message
}

export 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
}

export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
return class extends base {
public api: AxiosInstance
Expand Down

0 comments on commit 6bf6890

Please sign in to comment.