Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/HttpAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ exports.runAction = function( post_options, post_data, https_action, callback){
var abstractResponse = function(res){
res.setEncoding('utf8');
res.on('data', function (String_chunk) {
var JSON_chunk = JSON.parse(String_chunk);
var JSON_chunk;
try {
JSON_chunk = JSON.parse(String_chunk);
} catch (err) {
err.source = String_chunk;
return callback(new Response(null, null, { err: err }));
}
var response = new Response(JSON_chunk, String_chunk);
callback(response);
});
res.on('error', function (err) {
callback(new Response(null, null, { err: err }));
});
};
if (https_action !== true) {
post_options.port = 80;
Expand Down
13 changes: 11 additions & 2 deletions lib/Response/Abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
var Success = require('./Success');
var Err = require('./Err');

function CallbackRes(JSON_chunk, String_chunk){
function CallbackRes(JSON_chunk, String_chunk, option){
option = option || {};
if (option.err) {
var response = Err.buildResponseFromErrorObject(option.err);
JSON_chunk = JSON_chunk || response;
String_chunk = String_chunk || JSON.stringify(response);
}
this.JSON_chunk = JSON_chunk;
this.String_chunk = String_chunk;
}
Expand Down Expand Up @@ -102,8 +108,11 @@ CallbackRes.prototype = {

getErrorDetails: function(){
return Err.getParameter('error',this.JSON_chunk);
}
},

getErrorObject: function(){
return Err.getParameter('err',this.JSON_chunk);
}
};

module.exports = CallbackRes;
20 changes: 19 additions & 1 deletion lib/Response/Err.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,22 @@

exports.getParameter = function(parameter, JSON_chunk){
return JSON_chunk[parameter];
};
};

exports.buildResponseFromErrorObject = function(err){
if (typeof err.toJSON === 'undefined') {
err.toJSON = function(){
return {
message: this.message,
code: this.code,
source: this.source
};
};
}
return {
type: 'Error',
code: 'ERROR_OBJECT',
error: err.message,
err: err
};
}