Skip to content

Emitting workflow errors #4

Open
@cressie176

Description

I'm writing a publish task for a rascal and need to respond to 'success' and 'error' events. However it's possible that I may get multiple events. In this circumstance it would make sense to emit a workflow exception rather than execute the done callback twice.

var _ = require('lodash')

module.exports = function(node) {

    return function (context) {

        execute.annotations = { inject: ['broker', 'publication', 'payload', 'routingKey', 'options']}

        function execute(broker, publicationId, payload, routingKey, options, done) {
            var self = this
            broker = broker || context.broker
            options = _.defaults({}, { routingKey: routingKey }, options)

            broker.publish(publicationId, payload, options, function(err, publication) {
                if (err) return done(err)
                publication.on('success', onSuccess).on('error', onError)
            })

            function onSuccess(messageId) {
                respond(null, messageId)
                respond = function() {
                    emitError(new Error('Success called after the task has already completed'))
                }
            }

            function onError(err) {
                respond(err)
                respond = emitError
            }

            function emitError(err) {
                self.workflow.emit('error', err)
            }

            function respond(err, messageId) {
                done(err, messageId)
            }
        }

        return execute
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Emitting workflow errors · Issue #4 · guidesmiths/worksmith