Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ export const onRequestError: Instrumentation.onRequestError = async (
request,
context
) => {
const message = err instanceof Error ? err.message : String(err)
const digest =
typeof err === 'object' && err !== null && 'digest' in err
? String(err.digest)
: undefined

await fetch('https://.../report-error', {
method: 'POST',
body: JSON.stringify({
message: err.message,
message,
digest,
request,
context,
}),
Expand All @@ -64,10 +71,17 @@ export const onRequestError: Instrumentation.onRequestError = async (

```js filename="instrumentation.js" switcher
export async function onRequestError(err, request, context) {
const message = err instanceof Error ? err.message : String(err)
Comment thread
danyalahmed1995 marked this conversation as resolved.
const digest =
typeof err === 'object' && err !== null && 'digest' in err
? String(err.digest)
: undefined

await fetch('https://.../report-error', {
method: 'POST',
body: JSON.stringify({
message: err.message,
message,
digest,
request,
context,
}),
Expand All @@ -84,7 +98,7 @@ The function accepts three parameters: `error`, `request`, and `context`.

```ts filename="Types"
export function onRequestError(
error: { digest: string } & Error,
error: unknown,
request: {
path: string // resource path, e.g. /blog?name=foo
method: string // request method. e.g. GET, POST, etc
Expand All @@ -104,7 +118,7 @@ export function onRequestError(
): void | Promise<void>
```

- `error`: The caught error itself (type is always `Error`), and a `digest` property which is the unique ID of the error.
- `error`: The caught value is typed as `unknown`. Narrow it before reading properties like `message` or `digest`.
- `request`: Read-only request information associated with the error.
- `context`: The context in which the error occurred. This can be the type of router (App or Pages Router), and/or (Server Components (`'render'`), Route Handlers (`'route'`), Server Actions (`'action'`), or Proxy (`'proxy'`)).

Expand Down
Loading