-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(wait-until): Catch errors thrown during timeout
- Loading branch information
Showing
4 changed files
with
104 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Joi from 'joi'; | ||
|
||
import { RED } from '../../globals'; | ||
import { NodeDone } from '../../types/nodes'; | ||
import Status from '../status/Status'; | ||
import BaseError from './BaseError'; | ||
interface Dependencies { | ||
done?: NodeDone; | ||
status?: Status; | ||
} | ||
|
||
export function inputErrorHandler(e: unknown, deps?: Dependencies) { | ||
let statusMessage = RED._('home-assistant.status.error'); | ||
if (e instanceof Joi.ValidationError) { | ||
statusMessage = RED._('home-assistant.status.validation_error'); | ||
deps?.done?.(e); | ||
} else if (e instanceof BaseError) { | ||
statusMessage = e.statusMessage; | ||
deps?.done?.(e); | ||
} else if (e instanceof Error) { | ||
deps?.done?.(e); | ||
} else if (typeof e === 'string') { | ||
deps?.done?.(new Error(e)); | ||
} else { | ||
deps?.done?.(new Error(`Unrecognised error: ${e}`)); | ||
} | ||
deps?.status?.setFailed(statusMessage); | ||
} | ||
|
||
export function setTimeoutWithErrorHandling( | ||
callback: (...args: any[]) => void, | ||
timeout: number, | ||
deps?: Dependencies | ||
): NodeJS.Timeout { | ||
const timeoutId = setTimeout(() => { | ||
try { | ||
callback(); | ||
} catch (e) { | ||
inputErrorHandler(e, deps); | ||
} | ||
}, timeout); | ||
|
||
return timeoutId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters