Commit Graph

10 Commits

Author SHA1 Message Date
Jiachi Liu 93e1a4c71e tests: migrate on-reuqest-error tests to new model (#83383) 2025-09-03 16:47:47 +02:00
Hendrik Liebau 604b87cc28 Increase retry duration for HMR fetch logging tests (#71170)
As we've learned in #70467, compilation times can be quite high in CI,
when testing HMR behaviour. The fetch logging HMR tests are frequently
failing. Increasing the `retry` timeout should mitigate the flakiness.
2024-10-12 02:27:01 +02:00
Jiachi Liu c9daf1fc8c Stablize instrumentation.js (#68853)
### What

- After adding a new API `onRequestError()` and a few fixes on `register()` for `instrumentation.js`, it's time to promote it as stable API.
- Add warning `"experimental.instrumentationHook is no longer needed to be configured in Next.js"` when you configure a `instrumentationHook` option in next.config.js

Related PRs
#67539, #67671, #67703, #67848, #67859, #67856, #67805, #68616, #68672, #68764, #68845 #68976, #68983

Closes NDX-89
2024-08-21 15:55:26 +02:00
Jiachi Liu 11fa91b89f Rename url to path for onRequestError request arg (#68672)
### What

* Rename the `request.url` in onRequestError callback arg to
`request.path`
* Align `routeType: "middleware"` with others, only contain the resource
path in the `request.path`, stripped out request origin.

### Why

For non middleware case, the `req.url` in next-server is actual resource
path, which only contains the pathname and search but without request
origin. For middleware case, the origin is included in the `req.url`.
This might be a implementation detail, but for users, having request
origin here might not be critical and super helpful since they will know
the deployment as a global context for error tracking.

Hence we align the property to a variable that not holding request
origin, given a better naming `request.path`. The idea is from `:path`
request header, which represents the resource path, only containing
pathname and search. e.g. `/a/b?c=2`

x-ref:
https://greenbytes.de/tech/webdav/draft-ietf-httpbis-http2-09.html#HttpRequest
Closes NDX-54
2024-08-12 11:48:49 +02:00
Jiachi Liu e48eb5a2a9 Drop the experimental env var for onRequestError API (#67856)
### What

Since the API is getting more stablized with few past iteration and it's
still under the experimental flag, it's good to ship as a API along with
the `instrumentation.js`
2024-07-17 22:27:14 +02:00
Jiachi Liu b2e711aaea test: add dynamic routes and suspense test case for onRequestError (#67848)
### What

Add tests cases for dynamic routes and suspense page rendering case coverage for onRequestError.

Mostly for checking the `routePath` where we can display the proper original route path

Closes NDX-22
Closes NDX-79
2024-07-17 21:35:17 +02:00
Jiachi Liu f2adcfeb16 Handle server actions error in onRequestError (#67769)
### What

Handling server action errors capturing in instrumentation.js
`onRequestError` API

Closes NDX-21
2024-07-16 20:47:47 +02:00
Jiachi Liu 33e14f09b6 Add render source to onRequestError context (#67703)
### What

Provide `renderSource` for app router pages rendering errors in
instrumentation.js `onRequestError`.

Discussed with @gnoff that we refactored the error handlers a bit to
decouple from the Error render source since the handlers act differently
in different render phase.

### Why

This provides an easy way to determine wether the renderign error is
actually from SSR or RSC rendering. Since the RSC error is embedded in
the flight data and the final errors is only reported through React SSR
rendering.

Previously you can use the `digest` property sent to browser to
associate the actual error logged with your o11y provider, but it still
takes some effort, this would be the easy way to capture the original
source

Closes NDX-24
2024-07-12 19:28:37 +02:00
Jiachi Liu 38a6b018fd test: the nextjs internal errors should not be caught (#67671)
### What

Cover the Next.js internal errors are not captured by instrumentation
onRequestError API

Also skip deployment tests for onRequestError API due to the private env
flag `__NEXT_EXPERIMENTAL_INSTRUMENTATION` cannot be used in deployment

<!-- Closes NDX-33 -->
2024-07-12 18:38:31 +02:00
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