Skip to content

Commit 517f0e0

Browse files
authored
Expose TS type for onRequestError (#67859)
## What Expose the TS types for onRequestError API ### API ```ts type RequestErrorContext = { routerKind: 'Pages Router' | 'App Router'; routePath: string; routeType: 'render' | 'route' | 'action' | 'middleware'; renderSource?: 'react-server-components' | 'react-server-components-payload' | 'server-rendering'; }; export declare namespace Instrumentation { type onRequestError = (error: unknown, errorRequest: Readonly<{ method: string; url: string; headers: NodeJS.Dict<string | string[]>; }>, errorContext: Readonly<RequestErrorContext>) => void | Promise<void>; ``` ### Usage ```ts // instrumentation.ts import { type Instrumentation } from 'next' export const onRequestError: Instrumentation.onRequestError = ( err, request, context ) => { //... } ```
1 parent 8f08c7c commit 517f0e0

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

packages/next/src/server/instrumentation/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ export type InstrumentationModule = {
2323
register?(): void
2424
onRequestError?: InstrumentationOnRequestError
2525
}
26+
27+
export namespace Instrumentation {
28+
export type onRequestError = InstrumentationOnRequestError
29+
}

packages/next/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export type {
3838
ResolvedViewport,
3939
} from './lib/metadata/types/metadata-interface'
4040

41+
export type { Instrumentation } from './server/instrumentation/types'
42+
4143
/**
4244
* Stub route type for typedRoutes before `next dev` or `next build` is run
4345
* @link https://nextjs.org/docs/app/building-your-application/configuring/typescript#statically-typed-links

test/e2e/on-request-error/basic/instrumentation.js renamed to test/e2e/on-request-error/basic/instrumentation.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
export function onRequestError(err, request, context) {
1+
import { type Instrumentation } from 'next'
2+
3+
export const onRequestError: Instrumentation.onRequestError = (
4+
err,
5+
request,
6+
context
7+
) => {
28
fetch(`http://localhost:${process.env.PORT}/write-log`, {
39
method: 'POST',
410
body: JSON.stringify({
5-
message: err.message,
11+
message: (err as Error).message,
612
request,
713
context,
814
}),

0 commit comments

Comments
 (0)