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

add component and params to Error on visibility timeout #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 26 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ const Consumer = require('sqs-consumer')
const Producer = require('sqs-producer')
const tinygen = require('tinygen')

const {
always, bind, compose, curryN, dissoc, identity, merge,
mergeAll, nAry, once, partial, tap
} = require('ramda')
const { always, bind, compose, curryN, dissoc, identity, is,
merge, mergeAll, nAry, once, partial, tap, when } = require('ramda')

const { parse, stringify } = JSON

Expand Down Expand Up @@ -43,13 +41,30 @@ module.exports = opts => {
typeof handlers[type] === 'function' ? handlers[type] : missing(type)

const processJob = ({ type, payload }, callback) => {

const done = once(callback)
details = mergeAll([ options, { type, payload }, timeoutErr ]),
logTimeout = partial(timeoutLogger, [ details ]),
handleTimeout = compose(partial(done, [ new Error(timeoutErr.error) ]), tap(logTimeout))
timeout = setTimeout(handleTimeout, visibilityTimeout * 1000),
finish = compose(tap(partial(clearTimeout, [ timeout ])), done)
const done = once(callback)

const addErrorContext = error => {
error.params = mergeAll([ options, { type, payload }, timeoutErr ])
error.component = 'squiss-jobs'
return error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is mutable, and can sometimes be called twice on the same error, which is kinda 👃 .

}

const complete = error => {
const doneError = compose(done, when(is(Error), addErrorContext))
return arguments.length === 0
? done()
: doneError(error)
}

const handleTimeout = () => {
const error = new Error(timeoutErr.error)
addErrorContext(error)
timeoutLogger(params)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a params in scope at this point...which is probably my fault for suggesting you combine those two lines earlier where iirc, you had a separate const params = ....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not seeing any params anywhere. Do we have linting for this library?

complete(error)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying it's any clearer, but is it worth making this a pipe?

const logTimeout = partial(timeoutLogger, [ params ])
const handleTimeout = pipe(
  always(new Error(timeoutErr.error)),
  addErrorContext,
  tap(logTimeout),
  complete
)


const timeout = setTimeout(handleTimeout, visibilityTimeout * 1000),
finish = compose(tap(partial(clearTimeout, [ timeout ])), complete)

return Promise.resolve(payload)
.then(handlerFor(type))
Expand Down