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
////////////////////////////////////////////////////////////////////////////////
var internal = require('internal');
var Buffer = require('buffer').Buffer;
var extend = require('underscore').extend;
var httperr = require('http-errors');
var is = require('org/arangodb/is');
var querystring = require('querystring');
var qs = require('qs');
var url = require('url');
const internal = require('internal');
const Buffer = require('buffer').Buffer;
const extend = require('underscore').extend;
const httperr = require('http-errors');
const is = require('org/arangodb/is');
const querystring = require('querystring');
const qs = require('qs');
const url = require('url');
class Response {
throw(msg) {
var err = new httperr[this.status](msg || this.message);
err.details = this;
throw err;
if (this.status >= 400) {
let HttpError = httperr[this.status] || httperr[500];
let err = new HttpError(msg || this.message);
err.details = this;
throw err;
}
}
constructor(res, encoding, json) {
this.status = this.statusCode = res.code;
@ -81,14 +84,14 @@ function request(req) {
req = {url: req, method: 'GET'};
}
var path = req.url || req.uri;
let path = req.url || req.uri;
if (!path) {
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) {
var auth = pathObj.auth.split(':');
let auth = pathObj.auth.split(':');
req = extend({
auth: {
username: decodeURIComponent(auth[0]),
@ -97,14 +100,14 @@ function request(req) {
}, req);
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) {
pathObj.search = query;
}
path = url.format(pathObj);
var contentType;
var body = req.body;
let contentType;
let body = req.body;
if (req.json) {
body = JSON.stringify(body);
contentType = 'application/json';
@ -127,7 +130,7 @@ function request(req) {
}
}
var headers = {'content-type': contentType};
let headers = {'content-type': contentType};
if (req.headers) {
Object.keys(req.headers).forEach(function (name) {
@ -146,7 +149,7 @@ function request(req) {
);
}
var options = {
let options = {
method: (req.method || 'get').toUpperCase(),
headers: headers,
returnBodyAsBuffer: true
@ -162,7 +165,7 @@ function request(req) {
} else {
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);
}