From bbb5aef5d268c4f64801c8f96c1bf3e18a9cf836 Mon Sep 17 00:00:00 2001 From: Max Claus Nunes Date: Sat, 31 Oct 2015 21:20:54 -0200 Subject: [PATCH] Improve error handling from remote This way copy all properties available in the error object and keep the real stack trace --- atom/browser/lib/rpc-server.coffee | 9 ++++++++- atom/renderer/api/lib/remote.coffee | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/atom/browser/lib/rpc-server.coffee b/atom/browser/lib/rpc-server.coffee index 24ece22..953436e 100644 --- a/atom/browser/lib/rpc-server.coffee +++ b/atom/browser/lib/rpc-server.coffee @@ -40,7 +40,7 @@ valueToMeta = (sender, value, optimizeSimpleObject=false) -> else if meta.type is 'promise' meta.then = valueToMeta(sender, value.then.bind(value)) else if meta.type is 'error' - meta.message = value.message + meta = errorValueToMeta(value, meta) else if meta.type is 'date' meta.value = value.getTime() else @@ -50,6 +50,13 @@ valueToMeta = (sender, value, optimizeSimpleObject=false) -> meta # Convert Error into meta data. +errorValueToMeta = (err, meta) -> + Object.getOwnPropertyNames(err).reduce((obj, key) -> + obj[key] = err[key] + obj + , meta) + +# Convert Error into meta data. exceptionToMeta = (error) -> type: 'exception', message: error.message, stack: (error.stack || error) diff --git a/atom/renderer/api/lib/remote.coffee b/atom/renderer/api/lib/remote.coffee index b5a3a69..d01027e 100644 --- a/atom/renderer/api/lib/remote.coffee +++ b/atom/renderer/api/lib/remote.coffee @@ -46,7 +46,7 @@ metaToValue = (meta) -> when 'array' then (metaToValue(el) for el in meta.members) when 'buffer' then new Buffer(meta.value) when 'promise' then Promise.resolve(then: metaToValue(meta.then)) - when 'error' then new Error(meta.message) + when 'error' then metaToError(meta) when 'date' then new Date(meta.value) when 'exception' throw new Error("#{meta.message}\n#{meta.stack}") @@ -110,6 +110,17 @@ metaToValue = (meta) -> ret +# Convert meta data from browser into Error. +metaToError = (meta) -> + Object.getOwnPropertyNames(meta).reduce((error, prop) -> + Object.defineProperty(error, prop, { + get: -> meta[prop], + enumerable: false, + configurable: false + }) + error + , new Error()) + # Browser calls a callback in renderer. ipc.on 'ATOM_RENDERER_CALLBACK', (id, args) -> callbacksRegistry.apply id, metaToValue(args) -- 2.7.4