-
Notifications
You must be signed in to change notification settings - Fork 16
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
126 add error handling mechanism to the UI #518
Open
ChengShi-1
wants to merge
8
commits into
develop
Choose a base branch
from
126-add-error-handling-mechanism-to-the-ui
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−12
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
709b8fc
feat: error handling UI page
ChengShi-1 545bb9b
test: useErrorLogger hook test
ChengShi-1 3186877
chore: useErrorLogger hook test
ChengShi-1 de6b9b7
bug: fix some bugs in useErrorLogger hook test
ChengShi-1 b2159f4
feat: add errorElement to the children routes for adding header and f…
ChengShi-1 dd21944
feat: add text variables to the page
ChengShi-1 6d39627
Merge branch 'develop' into 126-add-error-handling-mechanism-to-the-ui
ChengShi-1 1bc573e
fix: adjust min-height of css as a dynamic variable
ChengShi-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"message": { | ||
"heading": "Oops,", | ||
"errorText": "something went wrong..." | ||
}, | ||
"brandName": "Dataverse", | ||
"backToHomepage": "Back to {{brandName}} Homepage" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@use 'sass:color'; | ||
@import 'node_modules/@iqss/dataverse-design-system/src/lib/assets/styles/design-tokens/colors.module'; | ||
@import 'src/assets/variables'; | ||
|
||
.section-wrapper { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
min-height: var(--error-min-height); | ||
} | ||
|
||
.middle-search-cta-wrapper { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 2rem; | ||
align-items: center; | ||
justify-content: center; | ||
width: 100%; | ||
padding-block: 2rem; | ||
} | ||
|
||
.icon-layout { | ||
display: flex; | ||
gap: 1rem; | ||
align-items: center; | ||
justify-content: center; | ||
width: 100%; | ||
padding-block: 2rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useTranslation } from 'react-i18next' | ||
import { useRouteError, Link } from 'react-router-dom' | ||
import styles from './ErrorPage.module.scss' | ||
import { useErrorLogger } from './useErrorLogger' | ||
import { ExclamationCircle } from 'react-bootstrap-icons' | ||
import { useTheme } from '@iqss/dataverse-design-system' | ||
|
||
export function ErrorPage() { | ||
const { t } = useTranslation('errorPage') | ||
const error = useRouteError() | ||
useErrorLogger(error) | ||
const theme = useTheme() | ||
|
||
const header = document.querySelector('nav') | ||
const footer = document.querySelector('footer') | ||
const newMinHeight = header && footer ? '$main-container-available-height' : '100vh' | ||
document.documentElement.style.setProperty('--error-min-height', newMinHeight) | ||
|
||
return ( | ||
<section className={styles['section-wrapper']}> | ||
<div className={styles['middle-search-cta-wrapper']}> | ||
<div className={styles['icon-layout']}> | ||
<ExclamationCircle color={theme.color.dangerColor} size={62} /> | ||
<div aria-label="error-page"> | ||
<h1>{t('message.heading')}</h1> | ||
<h4>{t('message.errorText')}</h4> | ||
</div> | ||
</div> | ||
|
||
<Link to="/" className="btn btn-secondary"> | ||
{t('backToHomepage', { brandName: t('brandName') })} | ||
</Link> | ||
</div> | ||
</section> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useCallback, useEffect } from 'react' | ||
|
||
const loggedErrors = new Set<string>() | ||
|
||
export function useErrorLogger(error: Error | unknown): (error: Error) => void { | ||
const logErrorOnce = useCallback((err: Error & { data?: unknown }): void => { | ||
const errorString = String(err.data) | ||
if (!loggedErrors.has(errorString)) { | ||
loggedErrors.add(errorString) | ||
console.error('Error:', err) | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (error) { | ||
logErrorOnce(error as Error) | ||
} | ||
}, [error, logErrorOnce]) | ||
|
||
return logErrorOnce | ||
} |
100 changes: 100 additions & 0 deletions
100
tests/component/sections/error-page/useErrorLogger.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { useErrorLogger } from '../../../../src/sections/error-page/useErrorLogger' | ||
|
||
interface RouterError { | ||
status: number | ||
statusText: string | ||
internal: boolean | ||
data: string | ||
error: Error | ||
} | ||
|
||
describe('useErrorLogger', () => { | ||
beforeEach(() => { | ||
cy.window().then((win) => { | ||
cy.stub(win.console, 'error').as('consoleError') | ||
}) | ||
}) | ||
|
||
it('should only log the same errors once when multiple occur, and log different errors separately', () => { | ||
const testError = { | ||
status: 404, | ||
statusText: 'Not Found', | ||
internal: true, | ||
data: 'Test Error: Not Found', | ||
error: new Error('Test Error') | ||
} | ||
|
||
const newError = { | ||
status: 500, | ||
statusText: 'Internal Server Error', | ||
internal: true, | ||
data: 'Error: Another error occurred', | ||
error: new Error('Another error occurred') | ||
} | ||
|
||
cy.window().then(() => { | ||
const { rerender } = renderHook( | ||
({ error }: { error: Error | RouterError | null }) => useErrorLogger(error), | ||
{ initialProps: { error: testError } } | ||
) | ||
cy.get('@consoleError').should('have.been.calledOnceWith', 'Error:', testError) | ||
cy.then(() => { | ||
rerender({ error: testError }) | ||
}) | ||
cy.get('@consoleError').should('have.been.calledOnce') | ||
cy.then(() => { | ||
rerender({ error: newError }) | ||
}) | ||
cy.get('@consoleError').should('have.been.calledWith', 'Error:', newError) | ||
}) | ||
}) | ||
|
||
it('should not log anything if no error is provided', () => { | ||
cy.window().then(() => { | ||
const { rerender } = renderHook( | ||
({ error }: { error: Error | RouterError | null }) => useErrorLogger(error), | ||
{ initialProps: { error: null } } | ||
) | ||
cy.get('@consoleError').should('not.have.been.called') | ||
cy.then(() => { | ||
rerender({ error: null }) | ||
}) | ||
cy.get('@consoleError').should('not.have.been.called') | ||
}) | ||
}) | ||
|
||
it('should be used to log errors manually only once for the same error, and log different errors separately', () => { | ||
const badRequestError = { | ||
status: 400, | ||
statusText: 'Bad Request', | ||
internal: true, | ||
data: 'Error: Manually logged error', | ||
error: new Error('Manually logged error') | ||
} | ||
|
||
const newError = { | ||
status: 500, | ||
statusText: 'Internal Server Error', | ||
internal: true, | ||
data: 'Error: Another error occurred', | ||
error: new Error('Another error occurred') | ||
} | ||
|
||
cy.window().then(() => { | ||
const { result } = renderHook(() => useErrorLogger(null)) | ||
cy.then(() => { | ||
return result.current(badRequestError.error) | ||
}) | ||
cy.get('@consoleError').should('have.been.calledOnceWith', 'Error:', badRequestError.error) | ||
cy.then(() => { | ||
result.current(badRequestError.error) | ||
}) | ||
cy.get('@consoleError').should('have.been.calledOnce') | ||
cy.then(() => { | ||
result.current(newError.error) | ||
}) | ||
cy.get('@consoleError').should('have.been.calledWith', 'Error:', newError.error) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid running natives javascript dom selectors to find elements by their tag name or even by a class name, these might change and might not even be the only ones you find.
Take a look at this, is from my lazy imports PR, see this tsx file and this scss one.
So then you only need to pass that prop to the ErrorPage on the root layout path object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll follow the example. Thank you!