From 1558a5c2f38fc0ea109584e2c6500235291b9d88 Mon Sep 17 00:00:00 2001 From: Stefano Verna Date: Tue, 22 Sep 2020 09:41:34 +0200 Subject: [PATCH] Better stacktrace and fix problem on async jobs failing response --- src/ApiException.js | 2 +- src/Client.js | 9 ++++++--- src/InvalidApiRequestException.js | 10 ++++++++++ src/utils/generateClient.js | 18 +++++++++++++----- 4 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 src/InvalidApiRequestException.js diff --git a/src/ApiException.js b/src/ApiException.js index 37f3ffc4..eccb3763 100644 --- a/src/ApiException.js +++ b/src/ApiException.js @@ -18,7 +18,7 @@ export default function ApiException( } this.body = body; - this.headers = response.headers.raw(); + this.headers = response.headers ? response.headers.raw() : {}; this.statusCode = response.status; this.statusText = response.statusText; this.requestUrl = url; diff --git a/src/Client.js b/src/Client.js index 5172d04c..40ddc4b0 100644 --- a/src/Client.js +++ b/src/Client.js @@ -12,7 +12,7 @@ function queryString(query) { export default class Client { constructor(token, extraHeaders, baseUrl) { - this.baseUrl = baseUrl; + this.baseUrl = baseUrl.replace(/\/$/, ''); this.token = token; this.extraHeaders = extraHeaders; } @@ -67,10 +67,13 @@ export default class Client { buildFetchRequest(method, url, params, body, extraOptions) { const options = { method, - body: body && JSON.stringify(body, undefinedToNull), ...(extraOptions || {}), }; + if (body) { + options.body = JSON.stringify(body, undefinedToNull); + } + const headers = { ...this.defaultHeaders(), ...this.extraHeaders, @@ -81,7 +84,7 @@ export default class Client { return { url: this.buildUrl(url, params), - options: { options, ...headers }, + options: { ...options, headers }, }; } diff --git a/src/InvalidApiRequestException.js b/src/InvalidApiRequestException.js new file mode 100644 index 00000000..65efccb3 --- /dev/null +++ b/src/InvalidApiRequestException.js @@ -0,0 +1,10 @@ +export default function InvalidApiRequestException(message, preCallStack) { + if ('captureStackTrace' in Error) { + Error.captureStackTrace(this, InvalidApiRequestException); + } else { + this.stack = new Error().stack; + } + + this.message = message; + this.stack += `\nCaused By:\n${preCallStack}`; +} diff --git a/src/utils/generateClient.js b/src/utils/generateClient.js index 1acd1f62..2ae88adc 100644 --- a/src/utils/generateClient.js +++ b/src/utils/generateClient.js @@ -7,6 +7,7 @@ import serializeJsonApi from './serializeJsonApi'; import RawClient from '../Client'; import fetchAllPages from './fetchAllPages'; import ApiException from '../ApiException'; +import InvalidApiRequestException from '../InvalidApiRequestException'; import wait from './wait'; const identityRegexp = /\{\(.*?definitions%2F(.*?)%2Fdefinitions%2Fidentity\)}/g; @@ -206,11 +207,18 @@ export default function generateClient(subdomain, cache, extraMethods = {}) { (link.method === 'PUT' || link.method === 'POST') && serializeRequest ) { - body = serializeJsonApi( - body, - link, - link.method === 'PUT' && lastUrlId, - ); + try { + body = serializeJsonApi( + body, + link, + link.method === 'PUT' && lastUrlId, + ); + } catch (e) { + throw new InvalidApiRequestException( + e.message, + preCallStack, + ); + } } if (link.method === 'POST') {