Skip to content

Commit 88374de

Browse files
committed
Display a nicer error message when a file cannot be opened
1 parent e6a61fb commit 88374de

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

resources/scripts/components/screens/ServerError.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import React from 'react';
22
import PageContentBlock from '@/components/elements/PageContentBlock';
3+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
4+
import { faArrowLeft } from '@fortawesome/free-solid-svg-icons/faArrowLeft';
35

46
interface Props {
57
title?: string;
68
message: string;
9+
onBack?: () => void;
710
}
811

9-
export default ({ title, message }: Props) => (
12+
export default ({ title, message, onBack }: Props) => (
1013
<PageContentBlock>
1114
<div className={'flex justify-center'}>
12-
<div className={'w-full sm:w-3/4 md:w-1/2 p-12 md:p-20 bg-neutral-100 rounded-lg shadow-lg text-center'}>
15+
<div className={'w-full sm:w-3/4 md:w-1/2 p-12 md:p-20 bg-neutral-100 rounded-lg shadow-lg text-center relative'}>
16+
{typeof onBack === 'function' &&
17+
<div className={'absolute pin-l pin-t ml-4 mt-4'}>
18+
<button
19+
onClick={() => onBack()}
20+
className={'rounded-full btn btn-primary w-8 h-8 flex items-center justify-center'}
21+
>
22+
<FontAwesomeIcon icon={faArrowLeft}/>
23+
</button>
24+
</div>
25+
}
1326
<img src={'/assets/svgs/server_error.svg'} className={'w-2/3 h-auto select-none'}/>
14-
<h2 className={'mt-6 text-neutral-900 font-bold'}>{ title || 'Something went wrong!' }</h2>
27+
<h2 className={'mt-6 text-neutral-900 font-bold'}>{title || 'Something went wrong!'}</h2>
1528
<p className={'text-sm text-neutral-700 mt-2'}>
1629
{message}
1730
</p>

resources/scripts/components/server/files/FileEditContainer.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { lazy, useEffect, useState } from 'react';
22
import { ServerContext } from '@/state/server';
33
import getFileContents from '@/api/server/files/getFileContents';
44
import useRouter from 'use-react-router';
5-
import { Actions, useStoreActions, useStoreState } from 'easy-peasy';
5+
import { Actions, useStoreActions } from 'easy-peasy';
66
import { ApplicationStore } from '@/state';
77
import { httpErrorToHuman } from '@/api/http';
88
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
@@ -13,10 +13,12 @@ import FileNameModal from '@/components/server/files/FileNameModal';
1313
import Can from '@/components/elements/Can';
1414
import FlashMessageRender from '@/components/FlashMessageRender';
1515
import PageContentBlock from '@/components/elements/PageContentBlock';
16+
import ServerError from '@/components/screens/ServerError';
1617

1718
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
1819

1920
export default () => {
21+
const [ error, setError ] = useState('');
2022
const { action } = useParams();
2123
const { history, location: { hash } } = useRouter();
2224
const [ loading, setLoading ] = useState(action === 'edit');
@@ -31,12 +33,12 @@ export default () => {
3133
if (action !== 'new') {
3234
useEffect(() => {
3335
setLoading(true);
34-
clearFlashes('files:view');
36+
setError('');
3537
getFileContents(uuid, hash.replace(/^#/, ''))
3638
.then(setContent)
3739
.catch(error => {
3840
console.error(error);
39-
addError({ key: 'files:view', message: httpErrorToHuman(error) });
41+
setError(httpErrorToHuman(error));
4042
})
4143
.then(() => setLoading(false));
4244
}, [ uuid, hash ]);
@@ -49,9 +51,10 @@ export default () => {
4951

5052
setLoading(true);
5153
clearFlashes('files:view');
52-
fetchFileContent().then(content => {
53-
return saveFileContents(uuid, name || hash.replace(/^#/, ''), content);
54-
})
54+
fetchFileContent()
55+
.then(content => {
56+
return saveFileContents(uuid, name || hash.replace(/^#/, ''), content);
57+
})
5558
.then(() => {
5659
if (name) {
5760
history.push(`/server/${id}/files/edit#/${name}`);
@@ -67,6 +70,15 @@ export default () => {
6770
.then(() => setLoading(false));
6871
};
6972

73+
if (error) {
74+
return (
75+
<ServerError
76+
message={error}
77+
onBack={() => history.goBack()}
78+
/>
79+
);
80+
}
81+
7082
return (
7183
<PageContentBlock>
7284
<FlashMessageRender byKey={'files:view'} className={'mb-4'}/>

0 commit comments

Comments
 (0)