Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(database): make to display validation error msg when all cases #20095

Merged
merged 13 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const mockedProps = {
subtitle: 'Error subtitle',
title: 'Error title',
source: 'dashboard' as ErrorSource,
description: 'we are unable to connect db.',
};

test('should render', () => {
Expand Down Expand Up @@ -63,6 +64,11 @@ test('should render the error title', () => {
expect(screen.getByText('Error title')).toBeInTheDocument();
});

test('should render the error description', () => {
render(<ErrorAlert {...mockedProps} />, { useRedux: true });
expect(screen.getByText('we are unable to connect db.')).toBeInTheDocument();
});

test('should render the error subtitle', () => {
render(<ErrorAlert {...mockedProps} />, { useRedux: true });
const button = screen.getByText('See more');
Expand Down
19 changes: 18 additions & 1 deletion superset-frontend/src/components/ErrorMessage/ErrorAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ interface ErrorAlertProps {
source?: ErrorSource;
subtitle: ReactNode;
title: ReactNode;
description?: string;
}

export default function ErrorAlert({
Expand All @@ -96,6 +97,7 @@ export default function ErrorAlert({
source = 'dashboard',
subtitle,
title,
description,
}: ErrorAlertProps) {
const theme = useTheme();

Expand All @@ -116,7 +118,7 @@ export default function ErrorAlert({
)}
<strong>{title}</strong>
</LeftSideContent>
{!isExpandable && (
{!isExpandable && !description && (
<span
role="button"
tabIndex={0}
Expand All @@ -127,6 +129,21 @@ export default function ErrorAlert({
</span>
)}
</div>
{description && (
<div className="error-body">
<p>{description}</p>
{!isExpandable && (
<span
role="button"
tabIndex={0}
className="link"
onClick={() => setIsModalOpen(true)}
>
{t('See more')}
</span>
)}
</div>
)}
{isExpandable ? (
<div className="error-body">
<p>{subtitle}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Props = {
copyText?: string;
stackTrace?: string;
source?: ErrorSource;
description?: string;
};

export default function ErrorMessageWithStackTrace({
Expand All @@ -43,6 +44,7 @@ export default function ErrorMessageWithStackTrace({
link,
stackTrace,
source,
description,
}: Props) {
// Check if a custom error message component was registered for this message
if (error) {
Expand All @@ -66,6 +68,7 @@ export default function ErrorMessageWithStackTrace({
title={title}
subtitle={subtitle}
copyText={copyText}
description={description}
source={source}
body={
link || stackTrace ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import IconButton from 'src/components/IconButton';
import InfoTooltip from 'src/components/InfoTooltip';
import withToasts from 'src/components/MessageToasts/withToasts';
import ValidatedInput from 'src/components/Form/LabeledErrorBoundInput';
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';
import {
testDatabaseConnection,
useSingleViewResource,
Expand All @@ -62,7 +63,6 @@ import ExtraOptions from './ExtraOptions';
import SqlAlchemyForm from './SqlAlchemyForm';
import DatabaseConnectionForm from './DatabaseConnectionForm';
import {
antDErrorAlertStyles,
antDAlertStyles,
antdWarningAlertStyles,
StyledAlertMargin,
Expand Down Expand Up @@ -114,6 +114,12 @@ const TabsStyled = styled(Tabs)`
}
`;

const ErrorAlertContainer = styled.div`
${({ theme }) => `
margin: ${theme.gridUnit * 8}px ${theme.gridUnit * 4}px;
`};
`;

interface DatabaseModalProps {
addDangerToast: (msg: string) => void;
addSuccessToast: (msg: string) => void;
Expand Down Expand Up @@ -466,7 +472,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
)?.parameters !== undefined;
const showDBError = validationErrors || dbErrors;
const isEmpty = (data?: Object | null) =>
data && Object.keys(data).length === 0;
!data || (data && Object.keys(data).length === 0);

const dbModel: DatabaseForm =
availableDbs?.databases?.find(
Expand Down Expand Up @@ -1086,22 +1092,24 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
// eslint-disable-next-line consistent-return
const errorAlert = () => {
let alertErrors: string[] = [];
if (isEmpty(dbErrors) === false) {
if (!isEmpty(dbErrors)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, this was if (isEmpty(dbErrors) === false)
This new line of if (!isEmpty(dbErrors)) is logically the same, which is great...

However, now I'm a little worried. Our QA round didn't catch anything wrong when this logic was indeed wrong. So... what does this exactly DO, and how do we test it to make sure it's working as expected?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests to cover this area of code and prove it works would be ideal. Spinning up an ephemeral for manual testing now that CI has passed. Please let us know what manual testing steps would help here.

Copy link
Contributor Author

@prosdev0107 prosdev0107 Jun 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rusackas
I tried to add the unit test for this case but couldn't do it because of that getValidation function is a call in a hooks and so it is invalid hook call in jest file. And so I add manual testing steps into the Test instructions

alertErrors = typeof dbErrors === 'object' ? Object.values(dbErrors) : [];
} else if (db?.engine === Engines.Snowflake) {
} else if (!isEmpty(validationErrors)) {
alertErrors =
validationErrors?.error_type === 'GENERIC_DB_ENGINE_ERROR'
? [validationErrors?.description]
? [
'We are unable to connect to your database. Click "See more" for database-provided information that may help troubleshoot the issue.',
]
: [];
}

if (alertErrors.length) {
return (
<Alert
type="error"
css={(theme: SupersetTheme) => antDErrorAlertStyles(theme)}
message={t('Database Creation Error')}
<ErrorMessageWithStackTrace
title={t('Database Creation Error')}
description={t(alertErrors[0])}
subtitle={t(validationErrors?.description)}
copyText={t(validationErrors?.description)}
/>
);
}
Expand Down Expand Up @@ -1406,7 +1414,9 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
onChange(ActionType.extraEditorChange, payload);
}}
/>
{showDBError && errorAlert()}
{showDBError && (
<ErrorAlertContainer>{errorAlert()}</ErrorAlertContainer>
)}
</Tabs.TabPane>
</TabsStyled>
</Modal>
Expand Down Expand Up @@ -1565,7 +1575,9 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
)}
</div>
{/* Step 2 */}
{showDBError && errorAlert()}
{showDBError && (
<ErrorAlertContainer>{errorAlert()}</ErrorAlertContainer>
)}
</>
))}
</>
Expand Down