From 42bd560e148c39855bdb4603d6c8157a43b59647 Mon Sep 17 00:00:00 2001 From: Eric Satterwhite Date: Wed, 18 May 2016 09:03:56 -0500 Subject: [PATCH] Attempt to call the parent function in a try/catch wrapper [#8] In the event that the parent function throws, and does not brint the process down ( node.js ) the parent property is never reset and the inheritance chain is forever broken --- prime/parentize.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/prime/parentize.js b/prime/parentize.js index 0d9dc28..e03c172 100644 --- a/prime/parentize.js +++ b/prime/parentize.js @@ -3,12 +3,31 @@ var prime = require("prime") var slice = require("mout/array/slice") +function attempt( fn, args, scope ){ + try{ + return [ null, fn.apply( scope, args ) ] + } catch( err ){ + return [ err, null ] + } +} + module.exports = prime({ parent: function(method){ - var parent = this._parent || this.constructor.parent - this._parent = parent.constructor.parent - var result = parent[method].apply(this, slice(arguments, 1)) - this._parent = parent - return result + var parent, result; + + parent = this._parent || this.constructor.parent; + this._parent = parent.constructor.parent; + result = attempt(parent[method], slice(arguments, 1) , this ); + this._parent = parent; + + if( result[0] ){ + throw result[0]; + } + + return result[1] } }) + + + +