Skip to content

Commit

Permalink
callback error cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Feb 13, 2018
1 parent ffc84f9 commit 112bb0d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 97 deletions.
22 changes: 16 additions & 6 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ exports.sub = function() {
var self = this
//var private$ = self.private$
var private_sub = self.private$.sub

var subargs = Common.parsePattern(self, arguments, 'action:f actdef:o?')
var pattern = subargs.pattern
if (
Expand All @@ -301,13 +301,23 @@ exports.sub = function() {
if (subfuncs) {
meta.sub = subfuncs.pattern
var actdef = subfuncs.actdef

_.each(subfuncs, function subfunc(subfunc) {
try {
for( var stI = 0, stlen = private_sub.tracers.length;
stI < stlen; stI++ ) {
for (
var stI = 0, stlen = private_sub.tracers.length;
stI < stlen;
stI++
) {
//private_sub.tracers[stI].call(self, self, msg, result, meta, actdef)
private_sub.tracers[stI].call(self, subfunc.instance$, msg, result, meta, actdef)
private_sub.tracers[stI].call(
self,
subfunc.instance$,
msg,
result,
meta,
actdef
)
}

//subfunc.call(self, msg, result, meta)
Expand Down Expand Up @@ -356,7 +366,7 @@ exports.sub = function() {
}
subs.push(subargs.action)
subargs.action.instance$ = self

return self
}

Expand Down
26 changes: 10 additions & 16 deletions lib/outward.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ function outward_res_object(ctxt, data) {
var msg = data.msg
var res = data.res

if( void 0 === data.res ) {
if (void 0 === data.res) {
data.res = null
}

var not_object =
res != null &&
!(
Expand All @@ -110,20 +110,18 @@ function outward_res_object(ctxt, data) {
)

if (so.strict.result && not_legacy && not_object) {

//data.res = outward.error || error(outward.code, outward.info)
data.res = ctxt.seneca.private$.error('result_not_objarr', {
pattern: ctxt.actdef.pattern,
args: Util.inspect(Common.clean(msg)).replace(/\n/g, ''),
result: res
pattern: ctxt.actdef.pattern,
args: Util.inspect(Common.clean(msg)).replace(/\n/g, ''),
result: res
})
data.meta.error = true
data.meta.error = true
}
}


function outward_announce(ctxt, data) {
if(!ctxt.actdef) return
if (!ctxt.actdef) return

if (_.isFunction(ctxt.seneca.on_act_out)) {
ctxt.seneca.on_act_out(ctxt.actdef, data.res, data.meta)
Expand All @@ -141,10 +139,9 @@ function outward_announce(ctxt, data) {
)
}


function outward_trace(ctxt, data) {
var private$ = ctxt.seneca.private$

var meta = data.meta
var reply_meta = data.reply_meta

Expand All @@ -166,7 +163,6 @@ function outward_trace(ctxt, data) {
}
}


function outward_act_error(ctxt, data) {
var delegate = ctxt.seneca
var actdef = ctxt.actdef
Expand Down Expand Up @@ -207,15 +203,13 @@ function outward_act_error(ctxt, data) {
delete data.err.meta$

data.res = null

data.meta = data.error_desc.err.meta$ || data.meta
}
else {
} else {
data.err = null
}
}


function outward_res_entity(ctxt, data) {
var delegate = ctxt.seneca
if (data.res && data.res.entity$ && delegate.make$) {
Expand Down
58 changes: 20 additions & 38 deletions seneca.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ function make_seneca(initial_options) {
.add(Outward.announce)
.add(Outward.act_error)


if (opts.$.test) {
root$.test('string' === typeof opts.$.test ? opts.$.test : 'print')
}
Expand Down Expand Up @@ -1288,8 +1287,6 @@ intern.handle_reply = function(meta, actctxt, actmsg, err, out, reply_meta) {
meta.end = Date.now()

var delegate = actctxt.seneca
var actdef = actctxt.actdef
var origmsg = actctxt.origmsg
var reply = actctxt.reply

var data = {
Expand All @@ -1305,29 +1302,16 @@ intern.handle_reply = function(meta, actctxt, actmsg, err, out, reply_meta) {
actctxt.duration = meta.end - meta.start
actctxt.actlog = actlog
actctxt.act_error = intern.act_error

meta.error = data.res instanceof Error

intern.process_outward(actctxt, data)

if (data.has_callback) {
try {
reply.call(delegate, data.err, data.res, data.meta)
} catch (e) {
var ex = Util.isError(e) ? e : new Error(Util.inspect(e))

intern.callback_error(
delegate,
ex,
actdef,
meta,
[err, out],
reply,
actctxt.duration,
actmsg,
origmsg,
actctxt.callpoint
)
} catch (thrown_obj) {
intern.callback_error(delegate, thrown_obj, actctxt, data)
}
}
}
Expand Down Expand Up @@ -1564,21 +1548,21 @@ intern.act_error = function(
}
}

intern.callback_error = function(
instance,
err,
actdef,
meta,
result,
cb,
duration,
msg,
origmsg,
act_callpoint
) {
var opts = instance.options()
intern.callback_error = function(instance, thrown_obj, actctxt, data) {
var duration = actctxt.duration
var act_callpoint = actctxt.callpoint
var actdef = actctxt.actdef || {}
var origmsg = actctxt.origmsg
var reply = actctxt.reply

actdef = actdef || {}
var meta = data.meta
var msg = data.msg

var err = Util.isError(thrown_obj)
? thrown_obj
: new Error(Util.inspect(thrown_obj))

var opts = instance.options()

if (!err.seneca) {
err = error(
Expand All @@ -1588,13 +1572,11 @@ intern.callback_error = function(
message: err.message,
pattern: actdef.pattern,
fn: actdef.func,
cb: cb,
callback: reply,
instance: instance.toString(),
callpoint: act_callpoint
})
)

result[0] = err
}

err.details = err.details || {}
Expand All @@ -1612,7 +1594,7 @@ intern.callback_error = function(
})
)

instance.emit('act-err', msg, err, result[1])
instance.emit('act-err', msg, err, data.res)

if (opts.errhandler) {
opts.errhandler.call(instance, err, err.meta$)
Expand Down
64 changes: 27 additions & 37 deletions test/seneca.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ describe('seneca', function() {
})

it('strict-result', function(fin) {
var si = Seneca({ log: 'silent', legacy: {transport: false} })
var si = Seneca({ log: 'silent', legacy: { transport: false } })

si
.add('a:1', function(msg, reply) {
Expand Down Expand Up @@ -1383,7 +1383,7 @@ describe('seneca', function() {
.ready(done)
})

it('memory', function(done) {
it('memory', { timeout: 2222 * tmx }, function(done) {
var SIZE = 1000

Seneca({ log: 'silent' })
Expand All @@ -1406,7 +1406,7 @@ describe('seneca', function() {

function validate(start) {
var end = Date.now()
expect(end - start).below(1500*tmx)
expect(end - start).below(1500 * tmx)

var mem = process.memoryUsage()
expect(mem.rss).below(200000000)
Expand Down Expand Up @@ -1490,58 +1490,48 @@ describe('seneca', function() {
.ready(fin)
})


it('pattern-types', function(fin) {
Seneca()
.test(fin)
// Just the value types from json.org, excluding object and array

// Just the value types from json.org, excluding object and array

.add({s:'s'}, function(msg, reply) {
reply({s: msg.s})
.add({ s: 's' }, function(msg, reply) {
reply({ s: msg.s })
})

.add({i:1}, function(msg, reply) {
reply({i: msg.i})
.add({ i: 1 }, function(msg, reply) {
reply({ i: msg.i })
})

.add({f:1.1}, function(msg, reply) {
reply({f: msg.f})
.add({ f: 1.1 }, function(msg, reply) {
reply({ f: msg.f })
})

.add({bt:true}, function(msg, reply) {
reply({bt: msg.bt})
.add({ bt: true }, function(msg, reply) {
reply({ bt: msg.bt })
})

.add({bf:false}, function(msg, reply) {
reply({bf: msg.bf})
.add({ bf: false }, function(msg, reply) {
reply({ bf: msg.bf })
})

.add({n:null}, function(msg, reply) {
reply({n: msg.n})
.add({ n: null }, function(msg, reply) {
reply({ n: msg.n })
})

.gate()

.act({s:'s'}, function(ignore, out) {
.act({ s: 's' }, function(ignore, out) {
expect(Util.inspect(out)).equal("{ s: 's' }")
})
.act({i:1}, function(ignore, out) {
expect(Util.inspect(out)).equal("{ i: 1 }")
.act({ i: 1 }, function(ignore, out) {
expect(Util.inspect(out)).equal('{ i: 1 }')
})
.act({f:1.1}, function(ignore, out) {
expect(Util.inspect(out)).equal("{ f: 1.1 }")
.act({ f: 1.1 }, function(ignore, out) {
expect(Util.inspect(out)).equal('{ f: 1.1 }')
})
.act({bt:true}, function(ignore, out) {
expect(Util.inspect(out)).equal("{ bt: true }")
.act({ bt: true }, function(ignore, out) {
expect(Util.inspect(out)).equal('{ bt: true }')
})
.act({bf:false}, function(ignore, out) {
expect(Util.inspect(out)).equal("{ bf: false }")
.act({ bf: false }, function(ignore, out) {
expect(Util.inspect(out)).equal('{ bf: false }')
})
.act({n:null}, function(ignore, out) {
expect(Util.inspect(out)).equal("{ n: null }")
.act({ n: null }, function(ignore, out) {
expect(Util.inspect(out)).equal('{ n: null }')
})
.ready(fin)
})

})

0 comments on commit 112bb0d

Please sign in to comment.