From 231806771087cc5a9d58c784c55cfd88640e4b64 Mon Sep 17 00:00:00 2001 From: ans4175 Date: Wed, 26 Oct 2016 16:47:50 +0700 Subject: [PATCH] handle error #18 --- lib/fcm.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/fcm.js b/lib/fcm.js index 1e230fa..21d4a92 100644 --- a/lib/fcm.js +++ b/lib/fcm.js @@ -65,17 +65,22 @@ FCM.prototype.send = function(payload, CB) { } function respond() { - var error = null, id = null, parsed_data = null; + var error = null, id = null; if (data.indexOf('\"multicast_id\":') > -1) { //handle multicast_id, send by devive token - anyFail = ((JSON.parse(data)).failure > 0); + var anyFail = ((JSON.parse(data)).failure > 0); if (anyFail) { - error = data.substring(0).trim(); + var isResults = ((JSON.parse(data)).results); + if (isResults) { + error = isResults[0].error; + } else { + error = data.substring(0).trim(); + } } - anySuccess = ((JSON.parse(data)).success > 0); + var anySuccess = ((JSON.parse(data)).success > 0); if (anySuccess) { id = data.substring(0).trim(); @@ -96,8 +101,9 @@ FCM.prototype.send = function(payload, CB) { return; } - // Success, return message id (without id=) - resolve(id); + // Success, return message id (without id=), or something error happened + if (id) resolve(id); + if (error) reject(error); } res.on('data', function(chunk) {