-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle session timeout/destroy errors globally
- Loading branch information
Showing
3 changed files
with
42 additions
and
3 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
30 changes: 30 additions & 0 deletions
30
packages/replay-next/components/errors/withSessionTimeoutCheck.ts
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 @@ | ||
import assert from "assert"; | ||
import { ComponentType, createElement, useContext } from "react"; | ||
import { ErrorBoundaryContext } from "react-error-boundary"; | ||
|
||
import { ProtocolError, isCommandError } from "shared/utils/error"; | ||
|
||
export function withSessionTimeoutCheck<Props extends object>(component: ComponentType<Props>) { | ||
const Wrapper = (props: Props) => { | ||
const context = useContext(ErrorBoundaryContext); | ||
assert(context, "ErrorBoundaryContext is not available"); | ||
|
||
const { didCatch, error } = context; | ||
if (didCatch) { | ||
if ( | ||
isCommandError(error, ProtocolError.UnknownSession) || | ||
isCommandError(error, ProtocolError.SessionDestroyed) | ||
) { | ||
throw error; | ||
} | ||
} | ||
|
||
return createElement(component, props); | ||
}; | ||
|
||
// Format for display in DevTools | ||
const name = component.displayName || component.name || "Unknown"; | ||
Wrapper.displayName = `withSessionTimeoutCheck(${name})`; | ||
|
||
return Wrapper; | ||
} |
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