forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle AJAX errors by showing a notification (elastic#717)
* timeout property for axios instance * remove TODOS * notifier class * TODO * second notification with the context * redirect to home if workpad could not be fetched * less noisy error checking
- Loading branch information
Showing
9 changed files
with
45 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import axios from 'axios'; | ||
import { FETCH_TIMEOUT } from './constants'; | ||
|
||
export const fetch = axios.create({ | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
'kbn-xsrf': 'professionally-crafted-string-of-text', | ||
}, | ||
timeout: FETCH_TIMEOUT, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
import chrome from 'ui/chrome'; | ||
import { API_ROUTE_WORKPAD } from '../../common/lib/constants'; | ||
import { notify } from '../lib/notify'; | ||
import { fetch } from '../../common/lib/fetch'; | ||
|
||
const basePath = chrome.getBasePath(); | ||
const apiPath = `${basePath}${API_ROUTE_WORKPAD}`; | ||
|
||
/* | ||
* The error data will be in `err.response` if the error comes from the server (example: 404) | ||
* The error object will be error data if it comes directly from the fetch library, (example: network error) | ||
*/ | ||
const notifyError = source => { | ||
return err => { | ||
const errData = err.response || err; | ||
notify.error(source); | ||
notify.error(errData); | ||
}; | ||
}; | ||
|
||
export function create(workpad) { | ||
return fetch.post(apiPath, { ...workpad, assets: workpad.assets || {} }); | ||
return fetch | ||
.post(apiPath, { ...workpad, assets: workpad.assets || {} }) | ||
.catch(notifyError('Could not create workpad')); | ||
} | ||
|
||
export function get(workpadId) { | ||
return fetch | ||
.get(`${apiPath}/${workpadId}`) | ||
.then(res => res.data) | ||
.catch(({ response }) => Promise.reject(response)); | ||
.catch(notifyError('Could not get workpad')); | ||
} | ||
|
||
export function update(id, workpad) { | ||
return fetch.put(`${apiPath}/${id}`, workpad); | ||
return fetch.put(`${apiPath}/${id}`, workpad).catch(notifyError('Could not update workpad')); | ||
} | ||
|
||
export function remove(id) { | ||
return fetch.delete(`${apiPath}/${id}`); | ||
return fetch.delete(`${apiPath}/${id}`).catch(notifyError('Could not remove workpad')); | ||
} | ||
|
||
export function find(searchTerm) { | ||
const validSearchTerm = typeof searchTerm === 'string' && searchTerm.length > 0; | ||
|
||
return fetch | ||
.get(`${apiPath}/find?name=${validSearchTerm ? searchTerm : ''}`) | ||
.then(resp => resp.data); | ||
.then(resp => resp.data) | ||
.catch(notifyError('Could not find workpads')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters