Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve nested objects in failure payloads #261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,29 @@ yarn add pg-boss
## Documentation
* [Usage](docs/usage.md)
* [Configuration](docs/configuration.md)

## Contributing

To setup a development environment for this library:

```bash
git clone https://github.com/timgit/pg-boss.git
npm install

```

To run the test suite you will need to pgboss access to an empty postgres database. You can set one up using the following commands on a local postgres instance:

```sql
CREATE DATABASE pgboss;
CREATE user postgres WITH PASSWORD 'postgres';
GRANT ALL PRIVILEGES ON DATABASE pgboss to postgres;
```

If you use a different database name, username or password, or want to run the test suite against a database that is running on a remote machine then you will need to edit the `test/config.json` file with the appropriate connection values.

You can then run the linter and test suite using:

```bash
npm run test
```
6 changes: 5 additions & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,11 @@ class Manager extends EventEmitter {
mapCompletionDataArg (data) {
if (data === null || typeof data === 'undefined' || typeof data === 'function') { return null }

if (data instanceof Error) { data = JSON.parse(JSON.stringify(data, Object.getOwnPropertyNames(data))) }
if (data instanceof Error) {
const newData = {}
Object.getOwnPropertyNames(data).forEach(key => { newData[key] = data[key] })
data = newData
}

return (typeof data === 'object' && !Array.isArray(data))
? data
Expand Down
17 changes: 17 additions & 0 deletions test/failureTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ describe('failure', function () {
assert.strictEqual(job.data.response.someReason, failPayload.someReason)
})

it('should preserve nested objects within a payload that is an instance of Error', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)

const queue = 'fail-payload'
const failPayload = new Error('Something went wrong')
failPayload.some = { deeply: { nested: { reason: 'nuna' } } }

const jobId = await boss.publish(queue, null, { onComplete: true })

await boss.fail(jobId, failPayload)

const job = await boss.fetchCompleted(queue)

assert.strictEqual(job.data.state, 'failed')
assert.strictEqual(job.data.response.some.deeply.nested.reason, failPayload.some.deeply.nested.reason)
})

it('subscribe failure via done() should pass error payload to failed job', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)

Expand Down