From a79a700a45136b6530a1b4103ddcb68b216caf8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Moura=20Ribeiro?= Date: Thu, 31 May 2018 18:32:21 -0300 Subject: [PATCH 1/5] Update error-info-interpreter.js This way you can also use custom errors issued per throw --- lib/translator/error-info-interpreter.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/translator/error-info-interpreter.js b/lib/translator/error-info-interpreter.js index 4339b1d..73936cf 100644 --- a/lib/translator/error-info-interpreter.js +++ b/lib/translator/error-info-interpreter.js @@ -17,7 +17,12 @@ class ErrorInfoInterpreter { code = err; } else if (err instanceof Error) { - code = err.message; + if (err.message instanceof Object) { + code = err.message.code; + custom = err.message.custom; + } else { + code = err.message; + } } else if (err instanceof Object) { code = err.code; From 1165598da108218bea3ad1d9e49af4a8d4ec43e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Moura=20Ribeiro?= Date: Thu, 31 May 2018 18:48:53 -0300 Subject: [PATCH 2/5] Update error-info-interpreter.js --- lib/translator/error-info-interpreter.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/translator/error-info-interpreter.js b/lib/translator/error-info-interpreter.js index 73936cf..8ad0e02 100644 --- a/lib/translator/error-info-interpreter.js +++ b/lib/translator/error-info-interpreter.js @@ -17,11 +17,19 @@ class ErrorInfoInterpreter { code = err; } else if (err instanceof Error) { - if (err.message instanceof Object) { - code = err.message.code; - custom = err.message.custom; + let parseOfMessage + + try { + parseOfMessage = JSON.parse(err.message); + } catch (e) { + parseOfMessage = err.message + } + + if (parseOfMessage instanceof Object) { + code = parseOfMessage.code; + custom = parseOfMessage.custom; } else { - code = err.message; + code = parseOfMessage; } } else if (err instanceof Object) { From 6e9f33525bfcef16b0ea879142cd5abc2b2eb39a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Moura=20Ribeiro?= Date: Thu, 31 May 2018 19:04:51 -0300 Subject: [PATCH 3/5] Update error-info-interpreter-tests.js --- .../lib/translator/error-info-interpreter-tests.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/unit/lib/translator/error-info-interpreter-tests.js b/test/unit/lib/translator/error-info-interpreter-tests.js index 16dcd6e..730f4e4 100644 --- a/test/unit/lib/translator/error-info-interpreter-tests.js +++ b/test/unit/lib/translator/error-info-interpreter-tests.js @@ -58,6 +58,20 @@ describe('ErrorInfoInterpreter', function () { errorInfo.should.have.property('code', code); errorInfo.should.have.property('custom').which.is.Undefined(); }); + + it('should interpret an error thrown (string of json) as an error', function () { + let interpreter = new ErrorInfoInterpreter(errors); + + let code = 'INVALID_REQUEST'; + let custom = { data: 'anything' }; + + let errorInfo = interpreter.interpret(new Error(JSON.stringify({ code, custom }))); + + should.exist(errorInfo); + Object.keys(errorInfo).should.have.length(2); + errorInfo.should.have.property('code', code); + errorInfo.should.have.property('custom').which.is.Undefined(); + }); it('should interpret an error thrown as an object', function () { let interpreter = new ErrorInfoInterpreter(errors); From 84b43eeb725327a36d8ae7016d4b343e4011740b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Moura=20Ribeiro?= Date: Thu, 31 May 2018 19:14:42 -0300 Subject: [PATCH 4/5] Update error-info-interpreter.js --- lib/translator/error-info-interpreter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/translator/error-info-interpreter.js b/lib/translator/error-info-interpreter.js index 8ad0e02..5126d8e 100644 --- a/lib/translator/error-info-interpreter.js +++ b/lib/translator/error-info-interpreter.js @@ -17,12 +17,12 @@ class ErrorInfoInterpreter { code = err; } else if (err instanceof Error) { - let parseOfMessage + let parseOfMessage; try { parseOfMessage = JSON.parse(err.message); } catch (e) { - parseOfMessage = err.message + parseOfMessage = err.message; } if (parseOfMessage instanceof Object) { From 1e3238ca3934dcfe8650802154831f9e0adfd8a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Moura=20Ribeiro?= Date: Thu, 31 May 2018 19:17:24 -0300 Subject: [PATCH 5/5] Update error-info-interpreter.js --- lib/translator/error-info-interpreter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/translator/error-info-interpreter.js b/lib/translator/error-info-interpreter.js index 5126d8e..e7b852d 100644 --- a/lib/translator/error-info-interpreter.js +++ b/lib/translator/error-info-interpreter.js @@ -20,9 +20,9 @@ class ErrorInfoInterpreter { let parseOfMessage; try { - parseOfMessage = JSON.parse(err.message); + parseOfMessage = JSON.parse(err.message); } catch (e) { - parseOfMessage = err.message; + parseOfMessage = err.message; } if (parseOfMessage instanceof Object) {