Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
fix: Catch and reject API errors (#83)
Browse files Browse the repository at this point in the history
* fix: Correctly catch and return API errors

Fixes moltin/workflow#1775

* refactor: Destructure params object for buildQueryParams function

* chore: Bumping yarn.lock

* build: Rollup
  • Loading branch information
ynnoj authored May 12, 2017
1 parent 4abc77a commit 2c35edc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dist/moltin.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/moltin.cjs.js.map

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions src/factories/request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StorageFactory from './storage';

import { setHeaderContentType, buildRequestBody } from '../utils/helpers';
import { setHeaderContentType, buildRequestBody, parseJSON } from '../utils/helpers';

class RequestFactory {
constructor(config) {
Expand Down Expand Up @@ -34,19 +34,20 @@ class RequestFactory {
},
body: Object.keys(body).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(body[k])}`).join('&'),
})
.then(parseJSON)
.then((response) => {
if (response.status === 200) {
const data = response.json();

resolve(data);
if (response.ok) {
resolve(response.json);
}

reject(response.json);
})
.then(null, error => reject(error));
.catch(() => reject('Fetch error - check your network'));
});

promise.then((data) => {
storage.set('mtoken', data.access_token);
storage.set('mexpires', data.expires);
promise.then((response) => {
storage.set('mtoken', response.access_token);
storage.set('mexpires', response.expires);
});

return promise;
Expand Down Expand Up @@ -74,10 +75,15 @@ class RequestFactory {
headers,
body: buildRequestBody(method, body),
})
.then(parseJSON)
.then((response) => {
resolve(response.json());
if (response.ok) {
resolve(response.json);
}

reject(response.json);
})
.catch(error => reject(error));
.catch(() => reject('Fetch error - check your network'));
};

if (!storage.get('mtoken') || Date.now().toString() >= storage.get('mexpires')) {
Expand Down
31 changes: 20 additions & 11 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export function cartIdentifier(reset = false, id = false) {
return id;
}

export function parseJSON(response) {
return new Promise(resolve => response.json()
.then(json => resolve({
status: response.status,
ok: response.ok,
json,
})));
}

export function setHeaderContentType(uri, method) {
let contentType = 'application/json';

Expand Down Expand Up @@ -75,27 +84,27 @@ function formatQueryString(key, value) {
return `${key}=${value}`;
}

function buildQueryParams(params) {
function buildQueryParams({ includes, sort, limit, offset, filter }) {
const query = {};

if (params.includes) {
query.include = params.includes;
if (includes) {
query.include = includes;
}

if (params.sort) {
query.sort = `(${params.sort})`;
if (sort) {
query.sort = `(${sort})`;
}

if (params.limit) {
query.limit = `[limit]=${params.limit}`;
if (limit) {
query.limit = `[limit]=${limit}`;
}

if (params.offset) {
query.offset = `[offset]=${params.offset}`;
if (offset) {
query.offset = `[offset]=${offset}`;
}

if (params.filter) {
query.filter = params.filter;
if (filter) {
query.filter = filter;
}

return Object.keys(query).map(k => formatQueryString(k, query[k])).join('&');
Expand Down
19 changes: 15 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
# yarn lockfile v1


"@krux/condition-jenkins@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@krux/condition-jenkins/-/condition-jenkins-1.0.1.tgz#be991b9867949115ea98040e43676fc60220410b"
dependencies:
"@semantic-release/error" "^1.0.0"
semver "^5.0.3"

"@semantic-release/error@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@semantic-release/error/-/error-1.0.0.tgz#bb8f8eeedd5c7f8c46f96b37ef39e1b8c376c1cc"

acorn-jsx@^3.0.0, acorn-jsx@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-3.0.1.tgz#afdf9488fb1ecefc8348f6fb22f464e32a58b36b"
Expand Down Expand Up @@ -1144,6 +1155,10 @@ rx-lite@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-3.1.2.tgz#19ce502ca572665f3b647b10939f97fd1615f102"

semver@^5.0.3:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"

shelljs@^0.7.5:
version "0.7.7"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.7.tgz#b2f5c77ef97148f4b4f6e22682e10bba8667cff1"
Expand Down Expand Up @@ -1174,10 +1189,6 @@ sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"

storage-available@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/storage-available/-/storage-available-1.0.0.tgz#404109e5fd992e1ab0a1796672f20207ace2963a"

string-width@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"
Expand Down

0 comments on commit 2c35edc

Please sign in to comment.