Skip to content

Commit b7c4c2f

Browse files
committed
Dry
1 parent 5699376 commit b7c4c2f

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

superset-frontend/src/dashboard/actions/dashboardInfo.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* under the License.
1818
*/
1919
import { Dispatch } from 'redux';
20-
import { makeApi, CategoricalColorNamespace, t } from '@superset-ui/core';
20+
import { makeApi, CategoricalColorNamespace } from '@superset-ui/core';
2121
import { isString } from 'lodash';
22-
import { getClientErrorObject } from 'src/utils/getClientErrorObject';
22+
import { getErrorText } from 'src/utils/getClientErrorObject';
2323
import { addDangerToast } from 'src/components/MessageToasts/actions';
2424
import {
2525
DashboardInfo,
@@ -169,18 +169,7 @@ export function saveFilterBarOrientation(orientation: FilterBarOrientation) {
169169
dispatch(onSave(lastModifiedTime));
170170
}
171171
} catch (errorObject) {
172-
const { error, message } = await getClientErrorObject(errorObject);
173-
let errorText = t('Sorry, an unknown error occurred.');
174-
175-
if (error) {
176-
errorText = t(
177-
'Sorry, there was an error saving this dashboard: %s',
178-
error,
179-
);
180-
}
181-
if (typeof message === 'string' && message === 'Forbidden') {
182-
errorText = t('You do not have permission to edit this dashboard');
183-
}
172+
const errorText = await getErrorText(errorObject, 'dashboard');
184173
dispatch(addDangerToast(errorText));
185174
throw errorObject;
186175
}
@@ -214,18 +203,7 @@ export function saveCrossFiltersSetting(crossFiltersEnabled: boolean) {
214203
dispatch(onSave(lastModifiedTime));
215204
}
216205
} catch (errorObject) {
217-
const { error, message } = await getClientErrorObject(errorObject);
218-
let errorText = t('Sorry, an unknown error occurred.');
219-
220-
if (error) {
221-
errorText = t(
222-
'Sorry, there was an error saving this dashboard: %s',
223-
error,
224-
);
225-
}
226-
if (typeof message === 'string' && message === 'Forbidden') {
227-
errorText = t('You do not have permission to edit this dashboard');
228-
}
206+
const errorText = await getErrorText(errorObject, 'dashboard');
229207
dispatch(addDangerToast(errorText));
230208
throw errorObject;
231209
}

superset-frontend/src/utils/getClientErrorObject.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ interface TimeoutError {
4141
timeout: number;
4242
}
4343

44+
type ErrorType =
45+
| SupersetClientResponse
46+
| TimeoutError
47+
| { response: Response }
48+
| string;
49+
50+
type ErrorTextSource = 'dashboard' | 'chart' | 'query' | 'dataset' | 'database';
51+
4452
export function parseErrorJson(responseObject: JsonObject): ClientErrorObject {
4553
let error = { ...responseObject };
4654
// Backwards compatibility for old error renderers with the new error object
@@ -78,6 +86,29 @@ export function parseErrorJson(responseObject: JsonObject): ClientErrorObject {
7886
return { ...error, error: error.error }; // explicit ClientErrorObject
7987
}
8088

89+
/*
90+
* Utility to get standardized error text for generic update failures
91+
*/
92+
export async function getErrorText(
93+
errorObject: ErrorType,
94+
source: ErrorTextSource,
95+
) {
96+
const { error, message } = await getClientErrorObject(errorObject);
97+
let errorText = t('Sorry, an unknown error occurred.');
98+
99+
if (error) {
100+
errorText = t(
101+
'Sorry, there was an error saving this %s: %s',
102+
source,
103+
error,
104+
);
105+
}
106+
if (typeof message === 'string' && message === 'Forbidden') {
107+
errorText = t('You do not have permission to edit this %s', source);
108+
}
109+
return errorText;
110+
}
111+
81112
export function getClientErrorObject(
82113
response:
83114
| SupersetClientResponse

0 commit comments

Comments
 (0)