Skip to content

Commit

Permalink
Throw D-Bus error responses as proper Error instances
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Apr 20, 2024
1 parent 78453d0 commit 3a64572
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This is a fork of [dbus-native](https://github.com/sidorares/dbus-native), aimin
* Dropped examples
* Improved error handling:
* Throws an explicit error when sending a message to a closed stream, instead of silently never responding
* Turn D-Bus error responses into proper error instances where possible (rather than throwing arrays of strings)

Installation
------------
Expand Down
12 changes: 11 additions & 1 deletion lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,17 @@ module.exports = function bus(conn, opts) {
args = [null].concat(args); // first argument - no errors, null
handler.apply(props, args); // body as array of arguments
} else {
handler.call(props, args); // body as first argument
// Turn single-string error response into proper error instance:
if (args.length === 1 && typeof args[0] === 'string') {
const errMessage = args[0];
const errName = msg.errorName ?? 'DbusError';
args = Object.assign(
new Error(`${errName}: ${errMessage}`, {
name: errName
})
);
}
handler.call(props, args); // body as first (error) argument
}
}
} else if (msg.type === constants.messageType.signal) {
Expand Down

0 comments on commit 3a64572

Please sign in to comment.