-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use error handler for event class (#1018)
- Loading branch information
Showing
16 changed files
with
77 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,17 @@ | ||
import EventEmitter from 'events'; | ||
import Joi from 'joi'; | ||
import { Node } from 'node-red'; | ||
|
||
import { RED } from '../../globals'; | ||
import { ClientEvent } from '../../homeAssistant/Websocket'; | ||
import BaseError from '../errors/BaseError'; | ||
import Status from '../status/Status'; | ||
import Events, { EventHandler } from './Events'; | ||
import Events from './Events'; | ||
|
||
export default class ClientEvents extends Events { | ||
#status?: Status; | ||
|
||
constructor({ node, emitter }: { node: Node; emitter: EventEmitter }) { | ||
super({ node, emitter }); | ||
|
||
this.emitter.on(ClientEvent.Error, this.onHaEventsError.bind(this)); | ||
} | ||
|
||
addListener( | ||
event: string | symbol, | ||
handler: EventHandler, | ||
options = { once: false } | ||
): void { | ||
super.addListener(event, this.#errorHandler(handler), options); | ||
} | ||
|
||
onHaEventsError(err: Error) { | ||
if (err?.message) this.node.error(err.message); | ||
} | ||
|
||
// set status for error reporting | ||
setStatus(status: Status) { | ||
this.#status = status; | ||
} | ||
|
||
#errorHandler(callback: any) { | ||
return (...args: any) => { | ||
try { | ||
// eslint-disable-next-line n/no-callback-literal | ||
callback(...args); | ||
} catch (e) { | ||
let statusMessage = RED._('home-assistant.status.error'); | ||
if (e instanceof Joi.ValidationError) { | ||
statusMessage = RED._( | ||
'home-assistant.status.validation_error' | ||
); | ||
this.node.error(e); | ||
} else if (e instanceof BaseError) { | ||
statusMessage = e.statusMessage; | ||
this.node.error(e); | ||
} else if (e instanceof Error) { | ||
this.node.error(e); | ||
} else if (typeof e === 'string') { | ||
this.node.error(new Error(e)); | ||
} else { | ||
this.node.error(new Error(`Unrecognised error: ${e}`)); | ||
} | ||
this.#status?.setFailed(statusMessage); | ||
} | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,17 +1,18 @@ | ||
import OutputController from '../../common/controllers/OutputController'; | ||
import { HassEntity } from '../../types/home-assistant'; | ||
import { NodeMessage } from '../../types/nodes'; | ||
import { ButtonNode } from '.'; | ||
|
||
export default class ButtonController extends OutputController<ButtonNode> { | ||
public onTrigger(data: { entity: HassEntity }) { | ||
this.status.setSuccess('home-assistant.status.pressed'); | ||
const message = {}; | ||
const message: NodeMessage = {}; | ||
this.setCustomOutputs(this.node.config.outputProperties, message, { | ||
config: this.node.config, | ||
entity: data.entity, | ||
entityState: data.entity.state, | ||
triggerId: data.entity.entity_id, | ||
}); | ||
this.status.setSuccess('home-assistant.status.pressed'); | ||
this.node.send(message); | ||
} | ||
} |
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
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
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
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
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