Improve error handling from remote

This way copy all properties available in the error object and keep the real stack trace
This commit is contained in:
Max Claus Nunes 2015-10-31 21:20:54 -02:00
parent cb91d4487b
commit bbb5aef5d2
2 changed files with 20 additions and 2 deletions

View file

@ -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
@ -49,6 +49,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)

View file

@ -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)