From df55494c1636fae6bce2d0e1cf0bdd6d21c93840 Mon Sep 17 00:00:00 2001 From: Andrew Naylor Date: Fri, 8 Oct 2010 17:47:48 +0100 Subject: [PATCH] Introduced some better error handling with an exports.error object containing error values for message error response code and local errors. --- lib/apn.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/apn.js b/lib/apn.js index 67fae601..7cf99e4a 100644 --- a/lib/apn.js +++ b/lib/apn.js @@ -4,6 +4,19 @@ var sys = require('sys'); var fs = require('fs'); var Buffer = require('buffer').Buffer; +var Errors = { + 'noErrorsEncountered': 0 + , 'processingError': 1 + , 'missingDeviceToken': 2 + , 'missingTopic': 3 + , 'missingPayload': 4 + , 'invalidTokenSize': 5 + , 'invalidTopicSize': 6 + , 'invalidPayloadSize': 7 + , 'invalidToken': 8 + , 'none': 255 +} + var Connection = function (optionArgs) { this.socket = new net.Stream(); this.credentials = crypto.createCredentials(); @@ -62,7 +75,12 @@ var Connection = function (optionArgs) { var messageLength = Buffer.byteLength(message); var pos = 0; - // Check notification length here. Return non-zero as error + if(token === undefined) { + return Errors['missingDeviceToken']; + } + if(messageLength > 255) { + return Errors['invalidPayloadSize']; + } note._uid = this.currentId++; @@ -344,4 +362,5 @@ function invoke_after(callback) { exports.connection = Connection; exports.notification = Notification; exports.device = Device; -exports.feedback = Feedback; \ No newline at end of file +exports.feedback = Feedback; +exports.error = Errors; \ No newline at end of file