This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch hugh/add-validation-to-alerts
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/hugh/add-validation-to-alerts 
by this push:
     new b1d6e93  added database errors (#15374)
b1d6e93 is described below

commit b1d6e93a82c5a3f9010ca017e93f27164af7abfb
Author: AAfghahi <[email protected]>
AuthorDate: Thu Jun 24 21:42:41 2021 -0400

    added database errors (#15374)
---
 .../CRUD/data/database/DatabaseModal/index.tsx     | 61 +++++++++++++++-------
 superset-frontend/src/views/CRUD/hooks.ts          | 21 ++++----
 2 files changed, 53 insertions(+), 29 deletions(-)

diff --git 
a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx 
b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx
index 0854724..fa1444e 100644
--- a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx
+++ b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx
@@ -74,6 +74,29 @@ import {
 } from './styles';
 import ModalHeader, { DOCUMENTATION_LINK } from './ModalHeader';
 
+const errorAlertMapping = {
+  CONNECTION_MISSING_PARAMETERS_ERROR: {
+    message: 'Missing Required Fields',
+    description: 'Please complete all required fields.',
+  },
+  CONNECTION_INVALID_HOSTNAME_ERROR: {
+    message: 'Could not verify the host',
+    description:
+      'The host is invalid. Please verify that this field is entered 
correctly.',
+  },
+  CONNECTION_PORT_CLOSED_ERROR: {
+    message: 'Port is closed',
+    description: 'Please verify that port is open to connect.',
+  },
+  CONNECTION_INVALID_PORT_ERROR: {
+    message: 'The port must be a whole number less than or equal to 65535.',
+  },
+  database_name: {
+    title: 'Invalid Database Name',
+    message: 'The display name is already taken. Please create a unique name.',
+  },
+};
+
 interface DatabaseModalProps {
   addDangerToast: (msg: string) => void;
   addSuccessToast: (msg: string) => void;
@@ -316,7 +339,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> 
= ({
 
   // Database fetch logic
   const {
-    state: { loading: dbLoading, resource: dbFetched, error: dbError },
+    state: { loading: dbLoading, resource: dbFetched, error: dbErrors },
     fetchResource,
     createResource,
     updateResource,
@@ -326,6 +349,9 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> 
= ({
     addDangerToast,
   );
 
+  const showDBError = validationErrors || dbErrors;
+  const isEmpty = (data: Object) => Object.keys(data).length === 0;
+
   const dbModel: DatabaseForm =
     availableDbs?.databases?.find(
       (available: { engine: string | undefined }) =>
@@ -369,7 +395,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> 
= ({
 
     // Validate DB before saving
     await getValidation(dbToUpdate, true);
-    if (validationErrors) {
+    if (validationErrors && !isEmpty(validationErrors)) {
       return;
     }
 
@@ -442,7 +468,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> 
= ({
         });
       }
       setLoading(true);
-      const dbId = await createResource(dbToUpdate as DatabaseObject);
+      const dbId = await createResource(dbToUpdate as DatabaseObject, true);
       if (dbId) {
         setHasConnectedDb(true);
         if (onDatabaseAdd) {
@@ -651,21 +677,16 @@ const DatabaseModal: 
FunctionComponent<DatabaseModalProps> = ({
   };
 
   const errorAlert = () => {
-    const errorAlertMapping = {
-      CONNECTION_MISSING_PARAMETERS_ERROR: {
-        message: 'Missing Required Fields',
-        description: 'Please complete all required fields.',
-      },
-      CONNECTION_INVALID_HOSTNAME_ERROR: {
-        message: 'Could not verify the host',
-        description:
-          'The host is invalid. Please verify that this field is entered 
correctly.',
-      },
-      CONNECTION_PORT_CLOSED_ERROR: {
-        message: 'Port is closed',
-        description: 'Please verify that port is open to connect.',
-      },
-    };
+    if (dbErrors?.database_name) {
+      return (
+        <Alert
+          type="error"
+          css={(theme: SupersetTheme) => antDErrorAlertStyles(theme)}
+          message={errorAlertMapping.database_name.title}
+          description={errorAlertMapping.database_name.message}
+        />
+      );
+    }
 
     if (
       validationErrors &&
@@ -929,7 +950,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> 
= ({
               onChange(ActionType.extraEditorChange, payload);
             }}
           />
-          {validationErrors && errorAlert()}
+          {showDBError && errorAlert()}
         </Tabs.TabPane>
       </Tabs>
     </Modal>
@@ -1055,7 +1076,7 @@ const DatabaseModal: 
FunctionComponent<DatabaseModalProps> = ({
                   />
                 </div>
                 {/* Step 2 */}
-                {validationErrors && errorAlert()}
+                {showDBError && errorAlert()}
               </>
             ))}
         </>
diff --git a/superset-frontend/src/views/CRUD/hooks.ts 
b/superset-frontend/src/views/CRUD/hooks.ts
index 42aae83..0815f91 100644
--- a/superset-frontend/src/views/CRUD/hooks.ts
+++ b/superset-frontend/src/views/CRUD/hooks.ts
@@ -211,7 +211,7 @@ export function useListViewResource<D extends object = any>(
 interface SingleViewResourceState<D extends object = any> {
   loading: boolean;
   resource: D | null;
-  error: string | Record<string, string[] | string> | null;
+  error: any | null;
 }
 
 export function useSingleViewResource<D extends object = any>(
@@ -269,7 +269,7 @@ export function useSingleViewResource<D extends object = 
any>(
   );
 
   const createResource = useCallback(
-    (resource: D) => {
+    (resource: D, hideToast = false) => {
       // Set loading state
       updateState({
         loading: true,
@@ -289,13 +289,16 @@ export function useSingleViewResource<D extends object = 
any>(
             return json.id;
           },
           createErrorHandler((errMsg: Record<string, string[] | string>) => {
-            handleErrorMsg(
-              t(
-                'An error occurred while creating %ss: %s',
-                resourceLabel,
-                parsedErrorMessage(errMsg),
-              ),
-            );
+            // we did not want toasts for db-connection-ui but did not want to 
disable it everywhere
+            if (!hideToast) {
+              handleErrorMsg(
+                t(
+                  'An error occurred while creating %ss: %s',
+                  resourceLabel,
+                  parsedErrorMessage(errMsg),
+                ),
+              );
+            }
 
             updateState({
               error: errMsg,

Reply via email to