Skip to content

Commit

Permalink
Drop the experimental env var for onRequestError API (vercel#67856)
Browse files Browse the repository at this point in the history
### 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`
  • Loading branch information
huozhi committed Jul 17, 2024
1 parent b2e711a commit e48eb5a
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 24 deletions.
5 changes: 1 addition & 4 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,7 @@ export default abstract class Server<
) {
const [err, req, ctx] = args

if (
process.env.__NEXT_EXPERIMENTAL_INSTRUMENTATION &&
this.instrumentation
) {
if (this.instrumentation) {
try {
await this.instrumentation.onRequestError?.(
err,
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/server/instrumentation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ export type InstrumentationModule = {
register?(): void
onRequestError?: InstrumentationOnRequestError
}

export namespace Instrumentation {
export type onRequestError = InstrumentationOnRequestError
}
7 changes: 1 addition & 6 deletions packages/next/src/server/web/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ export async function edgeInstrumentationOnRequestError(
) {
const instrumentation = await getEdgeInstrumentationModule()
try {
if (
process.env.__NEXT_EXPERIMENTAL_INSTRUMENTATION &&
instrumentation?.onRequestError
) {
await instrumentation.onRequestError(...args)
}
await instrumentation?.onRequestError?.(...args)
} catch (err) {
// Log the soft error and continue, since the original error has already been thrown
console.error('Error in instrumentation.onRequestError:', err)
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export type {
ResolvedViewport,
} from './lib/metadata/types/metadata-interface'

export type { Instrumentation } from './server/instrumentation/types'

/**
* Stub route type for typedRoutes before `next dev` or `next build` is run
* @link https://nextjs.org/docs/app/building-your-application/configuring/typescript#statically-typed-links
Expand Down
3 changes: 0 additions & 3 deletions test/e2e/on-request-error/basic/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ describe('on-request-error - basic', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
env: {
__NEXT_EXPERIMENTAL_INSTRUMENTATION: '1',
},
})

if (skipped) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export function onRequestError(err, request, context) {
import { type Instrumentation } from 'next'

export const onRequestError: Instrumentation.onRequestError = (
err,
request,
context
) => {
fetch(`http://localhost:${process.env.PORT}/write-log`, {
method: 'POST',
body: JSON.stringify({
message: err.message,
message: (err as Error).message,
request,
context,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ describe('on-request-error - dynamic-routes', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
env: {
__NEXT_EXPERIMENTAL_INSTRUMENTATION: '1',
},
})

if (skipped) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ describe('on-request-error - server-action-error', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
env: {
__NEXT_EXPERIMENTAL_INSTRUMENTATION: '1',
},
})

if (skipped) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ describe('on-request-error - skip-next-internal-error', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
env: {
__NEXT_EXPERIMENTAL_INSTRUMENTATION: '1',
},
})

if (skipped) {
Expand Down

0 comments on commit e48eb5a

Please sign in to comment.