Skip to content

Commit

Permalink
factorize error message
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Nov 13, 2023
1 parent dd79a05 commit 8efb1b4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export = class AristonApp extends withAPI(App) {
try {
await this.login(loginCredentials)
} catch (error: unknown) {
this.error(error instanceof Error ? error.message : error)
// Logged by `withAPI`
}
}

Expand Down
13 changes: 6 additions & 7 deletions mixins/withAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type APIClass = new (...args: any[]) => {
api: AxiosInstance
}

function getErrorMessage(error: AxiosError): string {
return error.message
}

export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
return class extends base {
public api: AxiosInstance
Expand Down Expand Up @@ -62,12 +66,7 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
type: 'request' | 'response',
error: AxiosError,
): Promise<AxiosError> {
const { data } = error.response ?? {}
this.error(
`Error in ${type}:`,
error.config?.url,
data !== undefined && data !== '' ? data : error.message,
)
this.error(`Error in ${type}:`, error.config?.url, getErrorMessage(error))
await this.setErrorWarning(error)
return Promise.reject(error)
}
Expand All @@ -76,7 +75,7 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
if (!this.setWarning) {
return
}
await this.setWarning(error.message)
await this.setWarning(getErrorMessage(error))
}
}
}

0 comments on commit 8efb1b4

Please sign in to comment.