add more meaningful 404 error screens

This commit is contained in:
Keith Grant 2019-08-07 16:04:39 -07:00
parent 256fc74676
commit fe8df27811
7 changed files with 139 additions and 37 deletions

View File

@ -9,6 +9,7 @@ import {
EmptyStateBody,
} from '@patternfly/react-core';
import { ExclamationTriangleIcon } from '@patternfly/react-icons';
import { RootAPI } from '@api';
import ErrorDetail from '@components/ErrorDetail';
import NotFoundError from './NotFoundError';
@ -16,23 +17,33 @@ const EmptyState = styled(PFEmptyState)`
width: var(--pf-c-empty-state--m-lg--MaxWidth);
`;
async function logout() {
await RootAPI.logout();
window.location.replace('/#/login');
}
class ContentError extends React.Component {
render() {
const { error, i18n } = this.props;
const { error, children, i18n } = this.props;
if (error && error.response && error.response.status === 401) {
// TODO: check for session timeout & redirect to /login
if (!error.response.headers['session-timeout']) {
logout();
return null;
}
}
if (error && error.response && error.response.status === 404) {
return <NotFoundError error={error} />;
return <NotFoundError error={error}>{children}</NotFoundError>;
}
return (
<EmptyState>
<EmptyStateIcon icon={ExclamationTriangleIcon} />
<Title size="lg">{i18n._(t`Something went wrong...`)}</Title>
<EmptyStateBody>
{i18n._(
t`There was an error loading this content. Please reload the page.`
)}
{children ||
i18n._(
t`There was an error loading this content. Please reload the page.`
)}
</EmptyStateBody>
{error && <ErrorDetail error={error} />}
</EmptyState>

View File

@ -15,15 +15,13 @@ const EmptyState = styled(PFEmptyState)`
width: var(--pf-c-empty-state--m-lg--MaxWidth);
`;
function NotFoundError ({ i18n, error }) {
function NotFoundError({ i18n, error, children }) {
return (
<EmptyState>
<EmptyStateIcon icon={ExclamationTriangleIcon} />
<Title size="lg">
{i18n._(t`Not Found`)}
</Title>
<Title size="lg">{i18n._(t`Not Found`)}</Title>
<EmptyStateBody>
{i18n._(`The page you requested could not be found.`)}
{children || i18n._(`The page you requested could not be found.`)}
</EmptyStateBody>
{error && <ErrorDetail error={error} />}
</EmptyState>

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { Route, withRouter, Switch, Redirect } from 'react-router-dom';
import { Route, withRouter, Switch, Redirect, Link } from 'react-router-dom';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
import styled from 'styled-components';
@ -9,7 +9,7 @@ import {
PageSection,
} from '@patternfly/react-core';
import { JobsAPI } from '@api';
import ContentError from '@components/ContentError';
import ContentError, { NotFoundError } from '@components/ContentError';
import CardCloseButton from '@components/CardCloseButton';
import RoutedTabs from '@components/RoutedTabs';
@ -99,7 +99,14 @@ class Job extends Component {
return (
<PageSection>
<Card className="awx-c-card">
<ContentError error={contentError} />
<ContentError error={contentError}>
{contentError.response.status === 404 && (
<span>
{i18n._(`The page you requested could not be found.`)}{' '}
<Link to="/jobs">{i18n._(`View all Jobs.`)}</Link>
</span>
)}
</ContentError>
</Card>
</PageSection>
);
@ -139,6 +146,20 @@ class Job extends Component {
path="/jobs/:type/:id/output"
render={() => <JobOutput type={match.params.type} job={job} />}
/>,
<Route
key="not-found"
path="*"
render={() => (
<NotFoundError>
{i18n._(`The page you requested could not be found.`)}{' '}
<Link
to={`/jobs/${match.params.type}/${match.params.id}/details`}
>
{i18n._(`View Job Details`)}
</Link>
</NotFoundError>
)}
/>,
]}
</Switch>
</Card>

View File

@ -1,8 +1,12 @@
import React, { Component } from 'react';
import { Redirect } from 'react-router-dom';
import { Redirect, Link } from 'react-router-dom';
import { PageSection, Card } from '@patternfly/react-core';
import { UnifiedJobsAPI } from '@api';
import ContentError, { NotFoundError } from '@components/ContentError';
import { JOB_TYPE_URL_SEGMENTS } from '../../constants';
const NOT_FOUND = 'not found';
class JobTypeRedirect extends Component {
static defaultProps = {
view: 'details',
@ -12,8 +16,9 @@ class JobTypeRedirect extends Component {
super(props);
this.state = {
hasError: false,
error: null,
job: null,
isLoading: true,
};
this.loadJob = this.loadJob.bind(this);
}
@ -24,24 +29,44 @@ class JobTypeRedirect extends Component {
async loadJob() {
const { id } = this.props;
this.setState({ isLoading: true });
try {
const { data } = await UnifiedJobsAPI.read({ id });
const job = data.results[0];
this.setState({
job: data.results[0],
job,
isLoading: false,
error: job ? null : NOT_FOUND,
});
} catch (error) {
this.setState({
error,
isLoading: false,
});
} catch (err) {
this.setState({ hasError: true });
}
}
render() {
const { path, view } = this.props;
const { hasError, job } = this.state;
const { error, job, isLoading } = this.state;
if (hasError) {
return <div>Error</div>;
if (error) {
return (
<PageSection>
<Card className="awx-c-card">
{error === NOT_FOUND ? (
<NotFoundError>
The requested job could not be found. <Link to="/jobs">View all Jobs.</Link>
</NotFoundError>
) : (
<ContentError error={error} />
)}
</Card>
</PageSection>
);
}
if (!job) {
if (isLoading) {
// TODO show loading state
return <div>Loading...</div>;
}
const type = JOB_TYPE_URL_SEGMENTS[job.type];

View File

@ -1,7 +1,7 @@
import React, { Component } from 'react';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
import { Switch, Route, withRouter, Redirect } from 'react-router-dom';
import { Switch, Route, withRouter, Redirect, Link } from 'react-router-dom';
import {
Card,
CardHeader as PFCardHeader,
@ -10,7 +10,7 @@ import {
import styled from 'styled-components';
import CardCloseButton from '@components/CardCloseButton';
import RoutedTabs from '@components/RoutedTabs';
import ContentError from '@components/ContentError';
import ContentError, { NotFoundError } from '@components/ContentError';
import { OrganizationAccess } from './OrganizationAccess';
import OrganizationDetail from './OrganizationDetail';
import OrganizationEdit from './OrganizationEdit';
@ -168,7 +168,16 @@ class Organization extends Component {
return (
<PageSection>
<Card className="awx-c-card">
<ContentError error={contentError} />
<ContentError error={contentError}>
{contentError.response.status === 404 && (
<span>
{i18n._(`Organization not found.`)}{' '}
<Link to="/organizations">
{i18n._(`View all Organizations.`)}
</Link>
</span>
)}
</ContentError>
</Card>
</PageSection>
);
@ -226,6 +235,21 @@ class Organization extends Component {
)}
/>
)}
<Route
key="not-found"
path="*"
render={() => (
<NotFoundError>
{i18n._(`The page you requested could not be found.`)}{' '}
{match.params.id && (
<Link to={`/organizations/${match.params.id}/details`}>
{i18n._(`View Organization Details`)}
</Link>
)}
</NotFoundError>
)}
/>
,
</Switch>
</Card>
</PageSection>

View File

@ -2,9 +2,9 @@ import React, { Component } from 'react';
import { t } from '@lingui/macro';
import { withI18n } from '@lingui/react';
import { Card, CardHeader, PageSection } from '@patternfly/react-core';
import { Switch, Route, Redirect, withRouter } from 'react-router-dom';
import { Switch, Route, Redirect, withRouter, Link } from 'react-router-dom';
import CardCloseButton from '@components/CardCloseButton';
import ContentError from '@components/ContentError';
import ContentError, { NotFoundError } from '@components/ContentError';
import RoutedTabs from '@components/RoutedTabs';
import JobTemplateDetail from './JobTemplateDetail';
import { JobTemplatesAPI } from '@api';
@ -77,7 +77,14 @@ class Template extends Component {
return (
<PageSection>
<Card className="awx-c-card">
<ContentError error={contentError} />
<ContentError error={contentError}>
{contentError.response.status === 404 && (
<span>
{i18n._(`Template not found.`)}{' '}
<Link to="/templates">{i18n._(`View all Templates.`)}</Link>
</span>
)}
</ContentError>
</Card>
</PageSection>
);
@ -92,8 +99,9 @@ class Template extends Component {
to="/templates/:templateType/:id/details"
exact
/>
{template && (
{template && [
<Route
key="details"
path="/templates/:templateType/:id/details"
render={() => (
<JobTemplateDetail
@ -102,14 +110,29 @@ class Template extends Component {
template={template}
/>
)}
/>
)}
{template && (
/>,
<Route
key="edit"
path="/templates/:templateType/:id/edit"
render={() => <JobTemplateEdit template={template} />}
/>
)}
/>,
<Route
key="not-found"
path="*"
render={() => (
<NotFoundError>
{i18n._(`The page you requested could not be found.`)}{' '}
{match.params.id && (
<Link
to={`/templates/${match.params.templateType}/${match.params.id}/details`}
>
{i18n._(`View Template Details`)}
</Link>
)}
</NotFoundError>
)}
/>,
]}
</Switch>
</Card>
</PageSection>

View File

@ -1,11 +1,11 @@
import React, { Component, Fragment } from 'react';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
import { Route, withRouter, Switch } from 'react-router-dom';
import { Route, withRouter, Switch, Link } from 'react-router-dom';
import { Config } from '@contexts/Config';
import Breadcrumbs from '@components/Breadcrumbs/Breadcrumbs';
import { NotFoundError } from '@components/ContentError';
import { TemplateList } from './TemplateList';
import Template from './Template';
import JobTemplateAdd from './JobTemplateAdd';