Commit Graph

1 Commits

Author SHA1 Message Date
Jiachi Liu ba3959bb46 feat: instrumentation onRequestError (#67539)
## What

This PR introduces a new API `onRequestError` in `instrumentation.js`
convention, which can help you track the errors thrown from pages and
routes on server side.

### API

```ts
type RequestInfo = {
  url: string
  method: string
  headers: Record<string, string | string [] | undefined>
}

type ErrorContext = {
  routerKind: 'Pages Router' | 'App Router'
  routePath: string
  routeType: 'render' | 'route' | 'middleware'
}

export function onRequestError(error: unknown, request: RequestInfo, errorContext: ErrorContext) {
}
```

This experimental feature is now scoped behind an experimental env var
`__NEXT_EXPERIMENTAL_INSTRUMENTATION` now. You need to enable to use it
before the feature is fully ready off from experimental.

## Why

The purpose is to provide a way to track the server errors from Next.js
much easier, especially when users're uing an o11y provider such as
sentry/datadog/newrelic etc. to monitor server side exceptions. There're
different runtime (Node.js or Edge) and different type of routes (App
Router pages/API routes, Pages Router pages/API routes, middleware) that
makes the error tracking story more complex. This API will be an
universal way to get all the errors.

The reason of providing the related arguments like request info and
error context is aimed to provide more insights about associated
request, also the context about Next.js framework itself, like which
feature is throwing the error.
2024-07-10 19:05:30 +02:00