Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed SOAP Fault errors not being raised as errors #676

Merged
merged 1 commit into from Jun 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/client.js
Expand Up @@ -16,7 +16,8 @@ var HttpClient = require('./http'),
events = require('events'),
util = require('util'),
debug = require('debug')('node-soap'),
url = require('url');
url = require('url'),
_ = require('lodash');

var Client = function(wsdl, endpoint, options) {
events.EventEmitter.call(this);
Expand Down Expand Up @@ -215,6 +216,15 @@ Client.prototype._invoke = function(method, args, location, callback, options, e
self.emit('message', message);
self.emit('request', xml);

var tryJSONparse = function(body) {
try {
return JSON.parse(body);
}
catch(err) {
return undefined;
}
};

req = self.httpClient.request(location, xml, function(err, response, body) {
var result;
var obj;
Expand All @@ -225,14 +235,19 @@ Client.prototype._invoke = function(method, args, location, callback, options, e
if (err) {
callback(err);
} else {

try {
obj = self.wsdl.xmlToObject(body);
} catch (error) {
//When the output element cannot be looked up in the wsdl,
//Then instead of sending the error, We pass the body in the response.
// When the output element cannot be looked up in the wsdl and the body is JSON
// instead of sending the error, we pass the body in the response.
if(!output.$lookupTypes) {
debug('Response element is not present. Unable to convert response xml to json.');
return callback(null,response,body);
// If the response is JSON then return it as-is.
var json = _.isObject(body) ? body : tryJSONparse(body);
if (json) {
return callback(null, response, json);
}
}
error.response = response;
error.body = body;
Expand Down
6 changes: 5 additions & 1 deletion lib/wsdl.js
Expand Up @@ -17,6 +17,7 @@ var assert = require('assert').ok;
var stripBom = require('strip-bom');
var debug = require('debug')('node-soap');
var _ = require('lodash');
var selectn = require('selectn');

var Primitives = {
string: 1,
Expand Down Expand Up @@ -1393,7 +1394,10 @@ WSDL.prototype.xmlToObject = function(xml) {
if (root.Envelope) {
var body = root.Envelope.Body;
if (body.Fault) {
var error = new Error(body.Fault.faultcode + ': ' + body.Fault.faultstring + (body.Fault.detail ? ': ' + body.Fault.detail : ''));
var code = selectn('faultcode.$value', body.Fault);
var string = selectn('faultstring.$value', body.Fault);
var detail = selectn('detail.$value', body.Fault);
var error = new Error(code + ': ' + string + (detail ? ': ' + detail : ''));
error.root = root;
throw error;
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -7,11 +7,12 @@
},
"author": "Vinay Pulim <v@pulim.com>",
"dependencies": {
"debug": "~0.7.4",
"lodash": "~2.4.1",
"request": ">=2.9.0",
"sax": ">=0.6",
"strip-bom": "~0.3.1",
"debug": "~0.7.4"
"selectn": "^0.9.6",
"strip-bom": "~0.3.1"
},
"repository": {
"type": "git",
Expand Down
30 changes: 30 additions & 0 deletions test/client-test.js
Expand Up @@ -516,4 +516,34 @@ describe('SOAP Client', function() {
});

});

it('should return error in the call when Fault was returned', function(done) {
var server = null;
var hostname = '127.0.0.1';
var port = 15099;
var baseUrl = 'http://' + hostname + ':' + port;

server = http.createServer(function (req, res) {
res.statusCode = 200;
res.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\n xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">\n<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type=\"xsd:string\">Test</faultcode><faultactor xsi:type=\"xsd:string\"></faultactor><faultstring xsi:type=\"xsd:string\">test error</faultstring><detail xsi:type=\"xsd:string\">test detail</detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>");
res.end();
}).listen(port, hostname, function() {
soap.createClient(__dirname+'/wsdl/json_response.wsdl', function(err, client) {
assert.ok(client);
assert.ok(!err);

client.MyOperation({}, function(err, result, body) {
server.close();
server = null;
assert.ok(err);
assert.strictEqual(err.message, 'Test: test error: test detail');
assert.ok(result);
assert.ok(body);
done();
});
}, baseUrl);
});

});

});