-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
61 lines (54 loc) · 1.42 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Action helpers
* author: @patr
*/
// Router requirements
// import { browserHistory } from 'react-router';
/* global Promise */
module.exports = {
/**
* parses json
*/
parseJSON: function parseJSON(response) {
return response.json();
},
parseText: function parseText(response) {
return response.text();
},
/**
* checks status of API call
*/
checkStatus: async function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response.statusText);
error.response = response;
error.message = await response.json()
return Promise.reject(error);
},
/***
* Handles the general error cases from an API call.
* 403 pushes to /login
* 500 pushes to a bad status page
*/
handleError: function handleError(error) {
if (error.response.status === 403) {
// browserHistory.push('/login');
}
},
errorMsg: function errorMsg(data) {
if (data.errors) {
var err = "";
for (var key in data.errors) {
if (data.errors.hasOwnProperty(key)) {
if (err !== "") {
err += " and ";
}
err += data.errors[key];
}
}
return err;
}
}
};