Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2272c54

Browse filesBrowse files
Filip-Feffervescentia
authored andcommitted
feat(BBD 842): Improper error handling in api-javascript (#81)
1 parent 528ac3d commit 2272c54
Copy full SHA for 2272c54

5 files changed

+237-65Lines changed: 237 additions & 65 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"chai": "^4.0.2",
5656
"condition-circle": "^1.5.0",
5757
"cz-conventional-changelog": "^2.0.0",
58+
"fetch-mock": "^5.13.1",
5859
"husky": "^0.13.4",
5960
"mocha": "^3.4.2",
6061
"mocha-suite": "^1.0.9",
@@ -69,13 +70,12 @@
6970
"xhr-mock": "^1.9.0"
7071
},
7172
"dependencies": {
72-
"@types/axios": "^0.9.35",
7373
"@types/clone": "^0.1.30",
7474
"@types/deep-equal": "^0.0.30",
7575
"@types/qs": "^6.2.30",
76-
"axios": "^0.12.0",
7776
"clone": "^1.0.2",
7877
"deep-equal": "^1.0.1",
78+
"fetch-ponyfill": "^4.1.0",
7979
"filter-object": "^2.1.0",
8080
"qs": "^6.1.0"
8181
}
Collapse file

‎src/core/bridge.ts‎

Copy file name to clipboardExpand all lines: src/core/bridge.ts
+53-11Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as axios from 'axios';
1+
import * as fetchPonyfill from 'fetch-ponyfill';
2+
import * as qs from 'qs';
23
import { Request } from '../models/request';
34
import { Navigation, RangeRefinement, Record, RefinementResults, Results } from '../models/response';
45
import { Query } from './query';
@@ -8,6 +9,13 @@ const REFINEMENTS = '/refinements';
89

910
const INVALID_QUERY_ERROR = 'query was not of a recognised type';
1011

12+
const createTimeoutPromise = (timeout: number) =>
13+
new Promise((resolve, reject) => {
14+
setTimeout(() => {
15+
reject(new BridgeTimeout(`Timed out in ${timeout} ms`));
16+
}, timeout);
17+
});
18+
1119
export interface RawRecord extends Record {
1220
_id: string;
1321
_u: string;
@@ -19,6 +27,24 @@ export interface BridgeCallback {
1927
(err?: Error, res?: Results): void;
2028
}
2129

30+
export class BridgeTimeout extends Error {
31+
/* istanbul ignore next */
32+
constructor (err: string) {
33+
super(err);
34+
}
35+
}
36+
37+
export class BridgeError extends Error {
38+
/* istanbul ignore next */
39+
constructor (statusText: string, public status: number, public data: any) {
40+
super(statusText);
41+
Object.setPrototypeOf(this, BridgeError.prototype);
42+
}
43+
get statusText() {
44+
return this.message;
45+
}
46+
}
47+
2248
export type BridgeQuery = string | Query | Request;
2349

2450
export const DEFAULT_CONFIG: BridgeConfig = {
@@ -28,6 +54,7 @@ export const DEFAULT_CONFIG: BridgeConfig = {
2854
export abstract class AbstractBridge {
2955

3056
config: BridgeConfig;
57+
fetch: fetchPonyfill = fetchPonyfill().fetch;
3158
headers: any = {};
3259
baseUrl: string;
3360
errorHandler: (error: Error) => void;
@@ -87,18 +114,33 @@ export abstract class AbstractBridge {
87114
}
88115
}
89116

90-
private fireRequest(url: string, body: Request | any, queryParams: any = {}): Axios.IPromise<any> {
117+
// tslint:disable-next-line max-line-length
118+
private fireRequest(url: string, body: Request | any, queryParams: any = {}): fetchPonyfill.Promise<any> {
91119
const options = {
92-
url,
93-
method: 'post',
94-
params: queryParams,
95-
data: this.augmentRequest(body),
96-
headers: this.headers,
97-
responseType: 'json',
98-
timeout: this.config.timeout
120+
method: 'POST',
121+
headers: {
122+
'Content-Type': 'application/json',
123+
...this.headers
124+
},
125+
body: JSON.stringify(this.augmentRequest(body)),
99126
};
100-
return axios(options)
101-
.then((res) => res.data)
127+
128+
const params = qs.stringify(queryParams);
129+
url = params ? `${url}?${params}` : url;
130+
return Promise.race([this.fetch(url, options), createTimeoutPromise(this.config.timeout)])
131+
.then((res) => {
132+
if (res.ok) {
133+
return res.json();
134+
} else {
135+
return res.json().then((err) => {
136+
throw new BridgeError(
137+
res.statusText,
138+
res.status,
139+
err
140+
);
141+
});
142+
}
143+
})
102144
.catch((err) => {
103145
if (this.errorHandler) {
104146
this.errorHandler(err);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.