SCR306: message-handler-payload-misuse
Message handler reads a payload key that no sender sends
| Code | SCR306 |
| Name | message-handler-payload-misuse |
| Category | Scripts (SCR) |
| Default severity | warning |
| Enabled by default | no |
| Fix available | no |
| ign-lint equivalent | none |
What it does
Section titled “What it does”Only with strict. For component message handlers and gateway and
session message handler scripts, compares the payload keys the handler
reads with payload['key'] with the keys of every literal payload sent to
it (system.perspective.sendMessage('type', {'key': ...}) for component
handlers, system.util.sendMessage(project, 'handler', {...}) for the
others), and reports keys that no sender passes.
It only reports when every send of the type to the project is known and
passes a dict literal: a payload that is a variable, a send with a computed
type, and a handler with no literal sender at all make the rule silent.
Reads with a default (payload.get('key')) and keys the handler tests
first ('key' in payload) are not reported.
Why is this bad?
Section titled “Why is this bad?”payload['key'] raises KeyError when the sender does not pass the key,
so the handler fails on every message. This is usually a key that was
renamed on one side only.
Example
Section titled “Example”# sendersystem.perspective.sendMessage('selectLine', {'lineId': 3})# handlerself.view.custom.line = payload['line']Use instead:
self.view.custom.line = payload['lineId']Generated from the rule metadata by iglint generate-docs; iglint rule SCR306 prints the same page in the terminal. Source: docs/rules/SCR306.md.