1
0
Fork 0

Only throw if actually an error.

This commit is contained in:
Alan Plum 2015-07-15 19:03:10 +02:00
parent 380e4b929b
commit 1a3a18ea3e
1 changed files with 23 additions and 20 deletions

View File

@ -29,20 +29,23 @@
/// @author Copyright 2015, triAGENS GmbH, Cologne, Germany /// @author Copyright 2015, triAGENS GmbH, Cologne, Germany
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
var internal = require('internal'); const internal = require('internal');
var Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
var extend = require('underscore').extend; const extend = require('underscore').extend;
var httperr = require('http-errors'); const httperr = require('http-errors');
var is = require('org/arangodb/is'); const is = require('org/arangodb/is');
var querystring = require('querystring'); const querystring = require('querystring');
var qs = require('qs'); const qs = require('qs');
var url = require('url'); const url = require('url');
class Response { class Response {
throw(msg) { throw(msg) {
var err = new httperr[this.status](msg || this.message); if (this.status >= 400) {
err.details = this; let HttpError = httperr[this.status] || httperr[500];
throw err; let err = new HttpError(msg || this.message);
err.details = this;
throw err;
}
} }
constructor(res, encoding, json) { constructor(res, encoding, json) {
this.status = this.statusCode = res.code; this.status = this.statusCode = res.code;
@ -81,14 +84,14 @@ function request(req) {
req = {url: req, method: 'GET'}; req = {url: req, method: 'GET'};
} }
var path = req.url || req.uri; let path = req.url || req.uri;
if (!path) { if (!path) {
throw new Error('Request URL must not be empty.'); throw new Error('Request URL must not be empty.');
} }
var pathObj = typeof path === 'string' ? url.parse(path) : path; let pathObj = typeof path === 'string' ? url.parse(path) : path;
if (pathObj.auth) { if (pathObj.auth) {
var auth = pathObj.auth.split(':'); let auth = pathObj.auth.split(':');
req = extend({ req = extend({
auth: { auth: {
username: decodeURIComponent(auth[0]), username: decodeURIComponent(auth[0]),
@ -97,14 +100,14 @@ function request(req) {
}, req); }, req);
delete pathObj.auth; delete pathObj.auth;
} }
var query = typeof req.qs === 'string' ? req.qs : querystringify(req.qs, req.useQuerystring); let query = typeof req.qs === 'string' ? req.qs : querystringify(req.qs, req.useQuerystring);
if (query) { if (query) {
pathObj.search = query; pathObj.search = query;
} }
path = url.format(pathObj); path = url.format(pathObj);
var contentType; let contentType;
var body = req.body; let body = req.body;
if (req.json) { if (req.json) {
body = JSON.stringify(body); body = JSON.stringify(body);
contentType = 'application/json'; contentType = 'application/json';
@ -127,7 +130,7 @@ function request(req) {
} }
} }
var headers = {'content-type': contentType}; let headers = {'content-type': contentType};
if (req.headers) { if (req.headers) {
Object.keys(req.headers).forEach(function (name) { Object.keys(req.headers).forEach(function (name) {
@ -146,7 +149,7 @@ function request(req) {
); );
} }
var options = { let options = {
method: (req.method || 'get').toUpperCase(), method: (req.method || 'get').toUpperCase(),
headers: headers, headers: headers,
returnBodyAsBuffer: true returnBodyAsBuffer: true
@ -162,7 +165,7 @@ function request(req) {
} else { } else {
options.maxRedirects = 10; options.maxRedirects = 10;
} }
var result = internal.download(path, body, options); let result = internal.download(path, body, options);
return new Response(result, req.encoding, req.json); return new Response(result, req.encoding, req.json);
} }