Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Better stacktrace and fix problem on async jobs failing response
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Sep 22, 2020
1 parent 5752ecd commit 1558a5c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/ApiException.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -81,7 +84,7 @@ export default class Client {

return {
url: this.buildUrl(url, params),
options: { options, ...headers },
options: { ...options, headers },
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/InvalidApiRequestException.js
Original file line number Diff line number Diff line change
@@ -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}`;
}
18 changes: 13 additions & 5 deletions src/utils/generateClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 1558a5c

Please sign in to comment.