Skip to content

Commit

Permalink
fix(@cubejs-client/core): response error handling (#2703)
Browse files Browse the repository at this point in the history
* fix(@cubejs-client/core): response error handling

* test fix

* fixes
  • Loading branch information
vasilev-alex committed May 13, 2021
1 parent a542d69 commit de31373
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
10 changes: 0 additions & 10 deletions packages/cubejs-client-core/src/HttpTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ class HttpTransport {
},
credentials: this.credentials,
body: this.method === 'POST' ? JSON.stringify(params) : null
}).then((res) => {
if (!res.ok) {
return new Promise((_, reject) => {
res.json().then((json) => {
reject(json.error?.toString() || JSON.stringify(json));
});
});
}

return res;
});

return {
Expand Down
10 changes: 7 additions & 3 deletions packages/cubejs-client-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,22 @@ class CubejsApi {
return continueWait(true);
}

if (response.status < 200 || response.status > 299) {
throw new Error(`Request error. Response status: ${response.status}`);
let body = {};

try {
body = await response.clone().json();
} catch (_) {
body.error = await response.text();
}

const body = await response.json();
if (body.error === 'Continue wait') {
await checkMutex();
if (options.progressCallback) {
options.progressCallback(new ProgressResult(body));
}
return continueWait();
}

if (response.status !== 200) {
await checkMutex();
if (!options.subscribe && requestInstance.unsubscribe) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-client-vue/tests/unit/__mocks__/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,10 @@ export default (body = {}, status = 200) => () => ({
{
status,
json: async () => body,
text: async () => JSON.stringify(body),
clone: function() {
return this;
},
ok: status >= 200 && status <= 399
},
() => this.subscribe(callback)
Expand Down
9 changes: 9 additions & 0 deletions packages/cubejs-client-ws-transport/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class WebSocketTransportResult {
public async json() {
return this.result;
}

public clone() {
// no need to actually clone it
return this;
}

public async text() {
return typeof this.result === 'string' ? this.result : JSON.stringify(this.result);
}
}

type WebSocketTransportOptions = {
Expand Down

0 comments on commit de31373

Please sign in to comment.