-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1073: Improve type narrowing and error handling
- Loading branch information
Showing
8 changed files
with
195 additions
and
77 deletions.
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
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
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,54 @@ | ||
import React, { ErrorInfo, ReactNode } from "react"; | ||
import { ErrorContainer } from "../../pages/404"; | ||
|
||
export class InternalError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} | ||
|
||
interface Props { | ||
children: ReactNode; | ||
} | ||
|
||
interface State { | ||
hasError: boolean; | ||
errorMessage: string; | ||
} | ||
|
||
export class ErrorBoundary extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
this.state = { | ||
hasError: false, | ||
errorMessage:"", | ||
}; | ||
} | ||
|
||
static getDerivedStateFromError(error: Error): State { | ||
return { | ||
hasError: true, | ||
errorMessage: error instanceof InternalError ? error.message : "Unknown error (thrown value was not an InternalError)", | ||
}; | ||
} | ||
|
||
override componentDidCatch(error: Error, info: ErrorInfo) { | ||
console.error(error); | ||
console.error(info); | ||
} | ||
|
||
override render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<ErrorContainer> | ||
{"Something isn't working!"} | ||
<br/> | ||
{`Error: ${this.state.errorMessage}`} | ||
<br/> | ||
{"Please "}<a href="/contact" style={{fontWeight: 300}}>get in touch</a>{" if this keeps happening"} | ||
</ErrorContainer> | ||
) | ||
} | ||
return this.props.children; | ||
} | ||
} |
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
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
Oops, something went wrong.