Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding error handler on the request object

  • Loading branch information...
commit abe393a4cae0bd2ebe720b673dba0133e5f83679 1 parent dc3de8e
@TauZero TauZero authored
View
176 lib/sailthru.js
@@ -1,6 +1,7 @@
+// Generated by CoffeeScript 1.3.3
(function() {
var LOGGING, SailthruClient, SailthruRequest, SailthruUtil, USER_AGENT, http, https, log, log2, querystring, rest, url, _ref,
- __slice = Array.prototype.slice;
+ __slice = [].slice;
http = require('http');
@@ -16,12 +17,14 @@
API client version
*/
+
exports.VERSION = '1.0.5';
/*
LOGGING Flag
*/
+
LOGGING = true;
USER_AGENT = 'Sailthru API Node/JavaScript Client';
@@ -32,14 +35,18 @@
helper logging function
*/
+
log2 = function(string) {
- if (LOGGING === true) return log(string);
+ if (LOGGING === true) {
+ return log(string);
+ }
};
/*
Private class to make HTTP request
*/
+
SailthruRequest = (function() {
var valid_methods;
@@ -49,7 +56,9 @@
SailthruRequest.prototype._http_request = function(uri, data, method, callback, binary_data_params) {
var http_protocol, options, parse_uri, query_string, req;
- if (binary_data_params == null) binary_data_params = [];
+ if (binary_data_params == null) {
+ binary_data_params = [];
+ }
parse_uri = url.parse(uri);
options = {
host: parse_uri.host,
@@ -110,6 +119,9 @@
}
});
});
+ req.on('error', function(err) {
+ return callback(err.message, err);
+ });
req.end();
if (method === 'POST') {
return req.write(url.format({
@@ -119,7 +131,9 @@
};
SailthruRequest.prototype._api_request = function(uri, data, request_method, callback, binary_data_params) {
- if (binary_data_params == null) binary_data_params = [];
+ if (binary_data_params == null) {
+ binary_data_params = [];
+ }
return this._http_request(uri, data, request_method, callback, binary_data_params);
};
@@ -131,13 +145,16 @@
/*
By default enable logging
*/
+
SailthruClient.logging = true;
function SailthruClient(api_key, api_secret, api_url) {
this.api_key = api_key;
this.api_secret = api_secret;
this.api_url = api_url != null ? api_url : false;
- if (this.api_url === false) this.api_url = 'https://api.sailthru.com';
+ if (this.api_url === false) {
+ this.api_url = 'https://api.sailthru.com';
+ }
this.request = new SailthruRequest;
}
@@ -145,6 +162,7 @@
prepare JSON payload
*/
+
SailthruClient.prototype._json_payload = function(data) {
var payload;
payload = {
@@ -161,6 +179,7 @@
Doesn't handle multipart request
*/
+
SailthruClient.prototype._apiRequest = function(action, data, method, callback) {
var json_payload, _url;
_url = url.parse(this.api_url);
@@ -180,6 +199,7 @@
GET call
*/
+
SailthruClient.prototype.apiGet = function(action, data, callback) {
return this._apiRequest(action, data, 'GET', callback);
};
@@ -188,8 +208,11 @@
POST call
*/
+
SailthruClient.prototype.apiPost = function(action, data, callback, binary_data_params) {
- if (binary_data_params == null) binary_data_params = [];
+ if (binary_data_params == null) {
+ binary_data_params = [];
+ }
if (binary_data_params.length > 0) {
return this.apiPostMultiPart(action, data, callback, binary_data_params);
} else {
@@ -201,9 +224,12 @@
POST call with Multipart
*/
+
SailthruClient.prototype.apiPostMultiPart = function(action, data, callback, binary_data_params) {
var binary_data, json_payload, param, value, _i, _len, _url;
- if (binary_data_params == null) binary_data_params = [];
+ if (binary_data_params == null) {
+ binary_data_params = [];
+ }
binary_data = {};
for (_i = 0, _len = binary_data_params.length; _i < _len; _i++) {
param = binary_data_params[_i];
@@ -232,6 +258,7 @@
DELETE call
*/
+
SailthruClient.prototype.apiDelete = function(action, data, callback) {
return this._apiRequest(action, data, 'DELETE', callback);
};
@@ -240,6 +267,7 @@
options mixin
*/
+
SailthruClient.prototype._getOptions = function(options) {
if (options !== null) {
return options;
@@ -256,7 +284,9 @@
SailthruClient.prototype.setEmail = function(email, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.email = email;
return this.apiPost('email', data, callback);
@@ -264,7 +294,9 @@
SailthruClient.prototype.send = function(template, email, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.template = template;
data.email = email;
@@ -273,7 +305,9 @@
SailthruClient.prototype.multiSend = function(template, emails, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.template = template;
data.email = emails instanceof Array ? emails.join(',') : emails;
@@ -321,7 +355,9 @@
SailthruClient.prototype.updateBlast = function(blastId, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.blast_id = blastId;
return this.apiPost('blast', data, callback);
@@ -329,7 +365,9 @@
SailthruClient.prototype.scheduleBlastFromBlast = function(blastId, scheduleTime, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.blast_id = blastId;
data.schedule_time = scheduleTime;
@@ -338,7 +376,9 @@
SailthruClient.prototype.scheduleBlastFromTemplate = function(blastId, template, list, scheduleTime, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.blast_id = blastId;
data.copy_template = template;
@@ -349,7 +389,9 @@
SailthruClient.prototype.scheduleBlast = function(name, list, scheduleTime, fromName, fromEmail, subject, contentHtml, contentText, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.name = name;
data.list = list;
@@ -384,7 +426,9 @@
SailthruClient.prototype.saveTemplate = function(template, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.template = template;
return this.apiPost('template', data, callback);
@@ -422,22 +466,30 @@
SailthruClient.prototype.importContacts = function(email, password, callback, includeNames) {
var data;
- if (includeNames == null) includeNames = true;
+ if (includeNames == null) {
+ includeNames = true;
+ }
data = {
email: email,
password: password
};
- if (includeNames === true) data.names = 1;
+ if (includeNames === true) {
+ data.names = 1;
+ }
return this.apiPost('contacts', data, callback);
};
SailthruClient.prototype.pushContent = function(title, url, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.title = title;
data.url = url;
- if (data.tags && data.tags instanceof Array) data.tags = data.tags.join(',');
+ if (data.tags && data.tags instanceof Array) {
+ data.tags = data.tags.join(',');
+ }
return this.apiPost('content', data, callback);
};
@@ -451,7 +503,9 @@
SailthruClient.prototype.saveAlert = function(email, type, template, callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.email = email;
data.type = type;
@@ -483,7 +537,9 @@
SailthruClient.prototype.statsList = function(callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.stat = 'blast';
return this.stats(data, callback);
@@ -491,7 +547,9 @@
SailthruClient.prototype.statsBlast = function(callback, options) {
var data;
- if (options == null) options = null;
+ if (options == null) {
+ options = null;
+ }
data = this._getOptions(options);
data.stat = 'blast';
return this.stats(data, callback);
@@ -499,21 +557,29 @@
SailthruClient.prototype.getHorizon = function(email, callback, hidOnly) {
var data;
- if (hidOnly == null) hidOnly = false;
+ if (hidOnly == null) {
+ hidOnly = false;
+ }
data = {
email: email
};
- if (hidOnly === true) data.hid_only = 1;
+ if (hidOnly === true) {
+ data.hid_only = 1;
+ }
return this.apiGet('horizon', data, callback);
};
SailthruClient.prototype.setHorizon = function(email, callback, tags) {
var data;
- if (tags == null) tags = null;
+ if (tags == null) {
+ tags = null;
+ }
data = {
email: email
};
- if (tags !== null) data.tags = tags instanceof Array ? tags.join(',') : tags;
+ if (tags !== null) {
+ data.tags = tags instanceof Array ? tags.join(',') : tags;
+ }
return this.apiPost('horizon', data, callback);
};
@@ -525,26 +591,44 @@
SailthruClient.prototype.processJob = function(job, callback, options, report_email, postback_url, binary_data_params) {
var data;
- if (options == null) options = null;
- if (report_email == null) report_email = false;
- if (postback_url == null) postback_url = false;
- if (binary_data_params == null) binary_data_params = Array;
+ if (options == null) {
+ options = null;
+ }
+ if (report_email == null) {
+ report_email = false;
+ }
+ if (postback_url == null) {
+ postback_url = false;
+ }
+ if (binary_data_params == null) {
+ binary_data_params = Array;
+ }
data = this._getOptions(options);
data['job'] = job;
- if (report_email !== false) data['report_email'] = report_email;
- if (postback_url !== false) data['postback_url'] = postback_url;
+ if (report_email !== false) {
+ data['report_email'] = report_email;
+ }
+ if (postback_url !== false) {
+ data['postback_url'] = postback_url;
+ }
return this.apiPost('job', data, callback, binary_data_params);
};
SailthruClient.prototype.receiveOptoutPost = function(params) {
- var param, sig, _i, _len, _ref2;
- if (typeof params === 'undefined') return false;
- _ref2 = ['action', 'email', 'sig'];
- for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
- param = _ref2[_i];
- if (typeof params[param] === 'undefined') return false;
- }
- if (params['action'] !== 'optout') return false;
+ var param, sig, _i, _len, _ref1;
+ if (typeof params === 'undefined') {
+ return false;
+ }
+ _ref1 = ['action', 'email', 'sig'];
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
+ param = _ref1[_i];
+ if (typeof params[param] === 'undefined') {
+ return false;
+ }
+ }
+ if (params['action'] !== 'optout') {
+ return false;
+ }
sig = params['sig'];
delete params['sig'];
if (sig !== SailthruUtil.getSignatureHash(params, this.api_secret)) {
@@ -563,9 +647,9 @@
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
- var child = new ctor, result = func.apply(child, args);
- return typeof result === "object" ? result : child;
- })(SailthruClient, args, function() {});
+ var child = new ctor, result = func.apply(child, args), t = typeof result;
+ return t == "object" || t == "function" ? result || child : child;
+ })(SailthruClient, args, function(){});
};
exports.createClient = function() {
@@ -573,9 +657,9 @@
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
- var child = new ctor, result = func.apply(child, args);
- return typeof result === "object" ? result : child;
- })(SailthruClient, args, function() {});
+ var child = new ctor, result = func.apply(child, args), t = typeof result;
+ return t == "object" || t == "function" ? result || child : child;
+ })(SailthruClient, args, function(){});
};
}).call(this);
View
1  lib/sailthru_util.js
@@ -1,3 +1,4 @@
+// Generated by CoffeeScript 1.3.3
(function() {
var SailthruUtil, VERSION, crypto, log;
View
3  src/sailthru.coffee
@@ -85,7 +85,8 @@ class SailthruRequest
error: 0,
errormsg: error.message
callback error.message, json_err
-
+ req.on 'error', (err) ->
+ callback err.message, err
req.end()
req.write url.format({query: options.query}).replace('?', ''), 'utf8' if method is 'POST'
View
26 test/test_sailthru.coffee
@@ -1,4 +1,7 @@
SailthruClient = require('../lib/sailthru').createSailthruClient('abcd12345','1324qwerty')
+SailthruClientBadUrl = require('../lib/sailthru').createSailthruClient('abcd12345','1324qwerty', 'http://foo')
+SailthruClient.disableLogging();
+SailthruClientBadUrl.disableLogging();
{testCase} = require 'nodeunit'
exports.receiveOptoutPost = (test) ->
@@ -56,3 +59,26 @@ exports.receiveOptoutPost = (test) ->
test.done()
+exports.connection = (test) ->
+ test.expect 2
+
+ connectErrMsg = 'getaddrinfo ENOENT'
+ finished = 0
+
+ # Connection Error
+ params1 = 'foo@bar.com'
+ callback1 = (err, res) ->
+ test.equal err, connectErrMsg
+ if finished
+ test.done()
+ finished++
+ SailthruClientBadUrl.getEmail params1, callback1
+
+ # Valid (Default) Connection
+ params2 = 'foo@bar.com'
+ callback2 = (err, res) ->
+ test.notEqual err, connectErrMsg
+ if finished
+ test.done()
+ finished++
+ SailthruClient.getEmail params2, callback2
Please sign in to comment.
Something went wrong with that request. Please try again.