From 4c4cc585c74730c7e591688d1e1785a85d030883 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 31 Jan 2017 13:40:01 +0000 Subject: [PATCH] Throw errors on !==200 status codes from RTS --- src/RtsClient.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/RtsClient.js b/src/RtsClient.js index 0067d0ae10..fe1f0538ca 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -1,5 +1,25 @@ const q = require('q'); -const request = q.nfbind(require('browser-request')); +const request = (opts) => { + const expectingJSONOnSucess = opts.json; + if (opts.json) { + opts.json = false; + } + return q.nfbind(require('browser-request'))(opts).then((args) => { + const response = args[0]; + let body = args[1]; + + // Do not expect JSON on error status code, throw error instead + if (response.statusCode !== 200) { + throw new Error(body); + } + + if (expectingJSONOnSucess) { + body = JSON.parse(body); + } + + return [response, body]; + }); +}; export default class RtsClient { constructor(url) {