rebenitez1802 commented on code in PR #42581:
URL: https://github.com/apache/superset/pull/42581#discussion_r3684856223
##########
superset-frontend/src/features/datasets/AddDataset/DatasetPanel/index.tsx:
##########
@@ -78,74 +87,127 @@ const DatasetPanelWrapper = ({
}: IDatasetPanelWrapperProps) => {
const [columnList, setColumnList] = useState<ITableColumn[]>([]);
const [loading, setLoading] = useState(false);
- const [hasError, setHasError] = useState(false);
- const tableNameRef = useRef(tableName);
+ const [error, setError] = useState<SupersetError>();
+ const requestIdRef = useRef(0);
+ const currentRequestRef = useRef<TableMetadataRequest>();
+ const supportsSchemas = database?.supports_schemas;
- const getTableMetadata = async (props: IColumnProps) => {
- const { dbId, tableName, schema } = props;
- setLoading(true);
- setHasColumns?.(false);
- const path = `/api/v1/database/${dbId}/table_metadata/${toQueryString({
- name: tableName,
- catalog,
- schema,
- })}`;
- try {
- const response = await SupersetClient.get({
- endpoint: path,
- });
+ const getTableMetadata = useCallback(
+ async (props: TableMetadataRequest) => {
+ const { dbId, tableName, catalog, schema } = props;
+ requestIdRef.current += 1;
+ const requestId = requestIdRef.current;
+ setLoading(true);
+ setColumnList([]);
+ setError(undefined);
+ setHasColumns?.(false);
+ const path = `/api/v1/database/${dbId}/table_metadata/${toQueryString({
+ name: tableName,
+ catalog,
+ schema,
+ })}`;
+ try {
+ const response = await SupersetClient.get({
+ endpoint: path,
+ });
+
+ if (requestId !== requestIdRef.current) {
+ return;
+ }
- if (isIDatabaseTable(response?.json)) {
- const table: IDatabaseTable = response.json as IDatabaseTable;
- /**
- * The user is able to click other table columns while the http call
for last selected table column is made
- * This check ensures we process the response that matches the last
selected table name and ignore the others
- */
- if (table.name === tableNameRef.current) {
+ const table = isIDatabaseTable(response?.json)
+ ? (response.json as IDatabaseTable)
+ : undefined;
+ if (table?.name === tableName) {
setColumnList(table.columns);
setHasColumns?.(table.columns.length > 0);
- setHasError(false);
- }
- } else {
- setColumnList([]);
- setHasColumns?.(false);
- setHasError(true);
- addDangerToast(
- t(
- 'The API response from %s does not match the IDatabaseTable
interface.',
- path,
- ),
- );
- logging.error(
- t(
+ setError(undefined);
+ } else {
+ const message = t(
'The API response from %s does not match the IDatabaseTable
interface.',
path,
- ),
+ );
+ setColumnList([]);
+ setHasColumns?.(false);
+ setError({
+ error_type: ErrorTypeEnum.GENERIC_BACKEND_ERROR,
+ extra: null,
+ level: 'error',
+ message,
+ });
+ addDangerToast(message);
+ logging.error(message);
+ }
+ } catch (caughtError) {
+ const clientError = await getClientErrorObject(
+ caughtError as Parameters<typeof getClientErrorObject>[0],
);
+
+ if (requestId === requestIdRef.current) {
+ const parsedError = clientError.errors?.[0] ?? {
+ error_type: ErrorTypeEnum.GENERIC_BACKEND_ERROR,
+ extra: null,
+ level: 'error' as const,
+ message: clientError.error,
Review Comment:
`clientError.error` is `undefined` at runtime when the rejected body has no
`errors[]` and no usable `error`/`message` (e.g. a 500 with body `{}` or `{
"errors": [] }`) — `parseErrorJson` returns `error: error.error` → `undefined`.
Since this fallback pins `error_type: GENERIC_BACKEND_ERROR`, it renders
through `DatabaseErrorMessage`, whose `message.split('\n')` throws on
`undefined`. Guarantee a string so a generic backend failure shows an error
instead of crashing the preview:
```suggestion
message: clientError.error ?? t('Unable to load columns for the
selected table.'),
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]