From 1a3a18ea3ee9adddcde22df5461d41e953aadb0c Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Wed, 15 Jul 2015 19:03:10 +0200 Subject: [PATCH] Only throw if actually an error. --- js/common/modules/org/arangodb/request.js | 43 ++++++++++++----------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/js/common/modules/org/arangodb/request.js b/js/common/modules/org/arangodb/request.js index 06f5ad1e94..1d3eba55be 100644 --- a/js/common/modules/org/arangodb/request.js +++ b/js/common/modules/org/arangodb/request.js @@ -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); }