From 6871a0fe9117fc37e56cbce1b6ba8497c009ff2c Mon Sep 17 00:00:00 2001 From: Nicolas Chambrier Date: Fri, 16 Mar 2012 16:30:28 +0100 Subject: [PATCH] Better handling of 5xx errors: still not perfect as we could add more detailed error messages, but at least the chain of callbacks is not broken as it was before --- src/octonode/client.coffee | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/octonode/client.coffee b/src/octonode/client.coffee index 0c2b8d2..2cebed2 100644 --- a/src/octonode/client.coffee +++ b/src/octonode/client.coffee @@ -39,8 +39,12 @@ class Client uri+= if @token then "?access_token=#{@token}" else '' errorHandle: (res, body, callback) -> - body = JSON.parse(body || '{}') - # TODO: Unprocessable entity + # TODO More detailed HTTP error message + return callback(new Error('Error ' + res.statusCode)) if Math.floor(res.statusCode/100) is 5 + try + body = JSON.parse(body || '{}') + catch err + return callback(err) return callback(new Error(body.message)) if res.statusCode is 422 return callback(new Error(body.message)) if res.statusCode in [400, 401, 404] callback null, res.statusCode, body