Skip to content

feat: improve error handling in edge functions#246

Merged
eduardoboucas merged 6 commits intomainfrom
eduardo/frb-1865-handle-error-from-deno-initialisation-in-edge-functions
Jun 2, 2025
Merged

feat: improve error handling in edge functions#246
eduardoboucas merged 6 commits intomainfrom
eduardo/frb-1865-handle-error-from-deno-initialisation-in-edge-functions

Conversation

@eduardoboucas
Copy link
Member

Two separate commits:

  • 3ccb579: Improves the error handling for when we fail to initialise the Deno server
  • 056b12f: Moves the invocation timeout to the runner and actually start enforcing it in case the worker doesn't send headers in time


error.stack = `${prefix} ${error.stack}`

reject(error)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start to reject the Promise if there has been an error rather than silently returning a successful response without any configs.

// The timeout imposed by the edge nodes. It's important to keep this in place
// as a fallback in case we're unable to patch `fetch` to add our own here.
// https://github.com/netlify/stargate/blob/b5bc0eeb79bbbad3a8a6f41c7c73f1bcbcb8a9c8/proxy/deno/edge.go#L77
const UPSTREAM_REQUEST_TIMEOUT = 37_000
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved these timeouts upstream so we can override them as needed.

@eduardoboucas eduardoboucas marked this pull request as ready for review June 2, 2025 11:30
@eduardoboucas eduardoboucas requested a review from a team as a code owner June 2, 2025 11:30
@@ -0,0 +1,23 @@
export const getErrorResponse = (error: unknown) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same format produced by the bootstrap layer, which is understood by the error page renderer.

Copy link
Member

@serhalp serhalp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

import { fileURLToPath, pathToFileURL } from 'node:url'

import { renderFunctionErrorPage, type Geolocation } from '@netlify/dev-utils'
import { Logger, renderFunctionErrorPage, type Geolocation } from '@netlify/dev-utils'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just enable verbatimModuleSyntax so this is automatic. I'll stop pointing these out for now. 😄

Suggested change
import { Logger, renderFunctionErrorPage, type Geolocation } from '@netlify/dev-utils'
import { type Logger, renderFunctionErrorPage, type Geolocation } from '@netlify/dev-utils'

Comment on lines +168 to +180
const initMessage = setTimeout(() => {
if (this.initialized) {
return
}

this.logger.log(
'Setting up the Netlify Edge Functions environment. This may take up to a couple of minutes, depending on your internet connection.',
)
}, 1_500)

const { denoPort, success } = await this.initialization
if (!success) {
clearTimeout(initMessage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
const initMessage = setTimeout(() => {
if (this.initialized) {
return
}
this.logger.log(
'Setting up the Netlify Edge Functions environment. This may take up to a couple of minutes, depending on your internet connection.',
)
}, 1_500)
const { denoPort, success } = await this.initialization
if (!success) {
clearTimeout(initMessage)
const initMessageTimeout = setTimeout(() => {
if (this.initialized) {
return
}
this.logger.log(
'Setting up the Netlify Edge Functions environment. This may take up to a couple of minutes, depending on your internet connection.',
)
}, 1_500)
const { denoPort, success } = await this.initialization
if (!success) {
clearTimeout(initMessageTimeout)

Comment on lines +17 to +21
errors[name] = {
message: error.message,
name: error.name,
stack: error.stack,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance any of these Errors have a cause property that we want to not discard?

@eduardoboucas eduardoboucas merged commit c0be696 into main Jun 2, 2025
7 checks passed
@eduardoboucas eduardoboucas deleted the eduardo/frb-1865-handle-error-from-deno-initialisation-in-edge-functions branch June 2, 2025 11:59
@token-generator-app token-generator-app bot mentioned this pull request Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants