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

Catch error when charset not recognized #21

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 12 additions & 5 deletions lib/needle.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var Needle = {
for (var h in options.headers) for (var h in options.headers)
config.headers[h] = options.headers[h]; config.headers[h] = options.headers[h];


if (options.username && options.password) { if (options.username && options.password) {
var b = new Buffer([options.username, options.password].join(':')); var b = new Buffer([options.username, options.password].join(':'));
var auth_header = options.proxy ? 'Proxy-Authorization' : 'Authorization'; var auth_header = options.proxy ? 'Proxy-Authorization' : 'Authorization';
config.headers[auth_header] = 'Basic ' + b.toString('base64'); config.headers[auth_header] = 'Basic ' + b.toString('base64');
Expand All @@ -104,7 +104,7 @@ var Needle = {
}); });
} else { } else {
var content_type = options.json ? 'application/json' : 'application/x-www-form-urlencoded'; var content_type = options.json ? 'application/json' : 'application/x-www-form-urlencoded';
var post_data = (typeof(data) === 'string') ? data : var post_data = (typeof(data) === 'string') ? data :
options.json ? JSON.stringify(data) : stringify(data); options.json ? JSON.stringify(data) : stringify(data);


config.headers['Content-Type'] = content_type; config.headers['Content-Type'] = content_type;
Expand Down Expand Up @@ -234,10 +234,17 @@ var Needle = {
handle_output(err, result); handle_output(err, result);
}); });
} else { } else {
if (opts.decode && opts.charset && !opts.charset.match(/utf-?8$/i)) // not utf-8 // Handle unrecognized charsets...
body = require('iconv-lite').decode(body, opts.charset); try {
if (opts.decode && opts.charset && !opts.charset.match(/utf-?8$/i)) // not utf-8
body = require('iconv-lite').decode(body, opts.charset);


handle_output(null, opts.text ? body.toString() : body); handle_output(null, opts.text ? body.toString() : body);

} catch (err) {

handle_output(err, body);
}
} }


} }
Expand Down