| |
@@ -12,29 +12,37 @@
|
| |
fetchConfig = Object.assign({}, baseFetchConfig, fetchConfig);
|
| |
return fetch(url, {...baseFetchConfig, ...fetchConfig}).then(
|
| |
(response) => {
|
| |
- if (response.ok) {
|
| |
- return json ? response.json() : response.text();
|
| |
- } else {
|
| |
- return response.text().then((msg) => {
|
| |
- throw new Error(msg);
|
| |
+ let resultPromise = json ? response.json() : response.text();
|
| |
+ if (!response.ok) {
|
| |
+ resultPromise = resultPromise.then((content) => {
|
| |
+ if (json) {
|
| |
+ // It's an object describing the error.
|
| |
+ throw content;
|
| |
+ } else {
|
| |
+ // It's already a text message.
|
| |
+ throw new Error(content);
|
| |
+ }
|
| |
});
|
| |
}
|
| |
+ return resultPromise;
|
| |
}
|
| |
- ).catch((error) => {
|
| |
- console.error(error.message);
|
| |
- throw error;
|
| |
- });
|
| |
+ );
|
| |
}
|
| |
|
| |
|
| |
export function apiCall(url, fetchConfig) {
|
| |
- return backendCall(url, fetchConfig, true).then(result => {
|
| |
- if (result.status === "OK") {
|
| |
- return result.data;
|
| |
- } else {
|
| |
- throw new Error(result.message);
|
| |
+ return backendCall(url, fetchConfig, true).then(
|
| |
+ result => {
|
| |
+ if (result.status === "OK") {
|
| |
+ return result.data;
|
| |
+ } else {
|
| |
+ throw new Error(result.message);
|
| |
+ }
|
| |
+ },
|
| |
+ error => {
|
| |
+ throw new Error(error.message);
|
| |
}
|
| |
- });
|
| |
+ );
|
| |
}
|
| |
|
| |
|
| |
AddWidgetDialog
respect the widget'sis_large
attributedispatch()
on actions, don't just return thembackendCall
andapiCall
error handling.