Skip to content

Commit d678940

Browse files
authored
Merge pull request #44 from tinystacks/error-handling
better error handling and display
2 parents 60e13ef + 30d7974 commit d678940

File tree

11 files changed

+220
-88
lines changed

11 files changed

+220
-88
lines changed

package-lock.json

Lines changed: 74 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"@next/bundle-analyzer": "^13.1.6",
3030
"@next/font": "^13.1.6",
3131
"@reduxjs/toolkit": "^1.9.2",
32-
"@tinystacks/ops-core": "^0.3.2",
33-
"@tinystacks/ops-model": "^0.4.0",
32+
"@tinystacks/ops-core": "^0.4.0",
33+
"@tinystacks/ops-model": "^0.5.0",
3434
"@types/node": "18.11.18",
3535
"@types/react": "18.0.27",
3636
"@types/react-dom": "18.0.10",
@@ -47,6 +47,7 @@
4747
"lodash.isempty": "^4.4.0",
4848
"lodash.isequal": "^4.4.0",
4949
"lodash.kebabcase": "^4.1.1",
50+
"lodash.upperfirst": "^4.3.1",
5051
"next": "13.2.4",
5152
"next-compose-plugins": "^2.2.1",
5253
"next-optimized-images": "^2.6.2",
@@ -71,6 +72,7 @@
7172
"@types/lodash.isempty": "^4.4.7",
7273
"@types/lodash.isequal": "^4.4.7",
7374
"@types/lodash.kebabcase": "^4.1.7",
75+
"@types/lodash.upperfirst": "^4.3.7",
7476
"@types/react-redux": "^7.1.25",
7577
"@types/react-router-dom": "^5.3.3",
7678
"@typescript-eslint/eslint-plugin": "^5.50.0",

src/components/common/dismissable-error-banner.tsx

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
11
import {
22
Alert,
3+
Box,
34
Container,
45
Center,
56
Flex,
67
AlertIcon,
78
AlertTitle,
89
AlertDescription,
9-
CloseButton
10+
CloseButton,
11+
Text
1012
} from '@chakra-ui/react';
13+
import upperFirst from 'lodash.upperfirst';
14+
import { ShowableError } from 'ops-frontend/types';
1115

1216
type DismissableErrorBannerProps = {
13-
error: {
14-
title: string;
15-
message: string;
16-
},
17+
error: ShowableError,
1718
dismissError: () => void;
1819
};
1920

2021
export default function DismissableErrorBanner (props: DismissableErrorBannerProps) {
2122
const {
2223
error: {
2324
title,
24-
message
25+
message,
26+
cause,
27+
context,
28+
fields = {}
2529
},
2630
dismissError
2731
} = props;
2832

33+
let extraDetail;
34+
if (cause || context) {
35+
const punctuationCheck = new RegExp('.*[,.?!;:=-]');
36+
let causePunctuation;
37+
if (cause) {
38+
causePunctuation = punctuationCheck.test(cause) ? '' :
39+
(
40+
context ? ':' : '.'
41+
);
42+
}
43+
extraDetail = (
44+
<Box display='block' alignContent='start'>
45+
<Text>
46+
{`${upperFirst(cause)}${causePunctuation}`}
47+
</Text>
48+
<Text whiteSpace='pre' textAlign='left' fontFamily='monospace'>
49+
{
50+
context ? context :
51+
Object.entries(fields).map(([property, propError]) => {
52+
return `${property} - ${propError}`
53+
}).join('\n')
54+
}
55+
</Text>
56+
</Box>
57+
)
58+
}
59+
2960
return (
30-
<Alert status='error' alignContent='center' w='auto'>
61+
<Alert
62+
status='error'
63+
w='auto'
64+
flexDirection='column'
65+
alignItems='center'
66+
justifyContent='center'
67+
textAlign='center'
68+
>
3169
<Container maxW="7xl">
3270
<Center>
33-
<Flex>
71+
<Flex alignContent='space-between'>
3472
<AlertIcon
3573
alignSelf='center'
3674
position='relative'
@@ -49,6 +87,7 @@ export default function DismissableErrorBanner (props: DismissableErrorBannerPro
4987
</Flex>
5088
</Center>
5189
</Container>
90+
{extraDetail}
5291
</Alert>
5392
);
5493
}

0 commit comments

Comments
 (0)