Skip to content

fix: show errors thrown by functions that aren't Error instances - #10008

Open
kuldeepsinh19 wants to merge 1 commit into
netlify:mainfrom
kuldeepsinh19:fix/6215-custom-error-without-stack
Open

kuldeepsinh19 wants to merge 1 commit into
netlify:mainfrom
kuldeepsinh19:fix/6215-custom-error-without-stack

Conversation

@kuldeepsinh19

Copy link
Copy Markdown

Summary

Fixes #6215

When a function throws something that isn't an Error instance, such as airtable.js's AirtableError ({ error, message, statusCode }) or a plain string, netlify dev / functions:serve doesn't show the function's error. Instead, the request fails inside the CLI:

TypeError: Cannot read properties of undefined (reading 'map')
    at getNormalizedError (src/lib/functions/synchronous.ts:127:39)
    at handleSynchronousFunction (src/lib/functions/synchronous.ts:42:19)

and the response is Express's default error page with that stack trace.

lambda-local (2.2.0, build/lib/context.js) converts only Error instances into { errorType, errorMessage, stackTrace }, and leaves out stackTrace when the error has no stack. Every other thrown value is passed through unchanged. getNormalizedError() assumed all three properties were always there.

Changes:

  • getNormalizedError() (synchronous.ts) takes unknown. It falls back to name/message (or the value itself for a string, inspect() otherwise) and uses an empty stack trace when there's none. Error instances and lambda-local's errors are handled as before.
  • formatLambdaError() (utils.ts, used for scheduled and background functions) also threw on a thrown string (Cannot use 'in' operator to search for 'errorType' in boom), so it now handles non-object values too.

With the repro from the issue (an AirtableError-like throw), functions:serve now returns a 500 with Error: Could not find what you are looking for and logs Function hello has returned an error: Could not find what you are looking for.

Tests:

  • New tests/unit/lib/functions/synchronous.test.ts calls handleSynchronousFunction with an Error, a lambda-local error, a lambda-local error without a stack trace, a non-Error object (JSON and HTML responses) and a string. The last 3 shapes fail on main with the error above.
  • New case in scheduled.test.ts for a thrown string. It fails on main with the in error.
  • npm run test:unit (849 tests), npm run typecheck, eslint and oxfmt --check on the changed files pass (Node 24, in a container). I didn't run the integration suites.

This change was written with help from an AI coding assistant (Claude Code). I've reviewed every line and can explain any of it.


@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Error responses now handle thrown strings and other non-standard error values, displaying available error details instead of failing to format them.
    • Synchronous function errors now show a consistent error type and message, with stack traces included only when available.
    • Scheduled-function help responses now include the thrown value when an error is a string.

Synchronous function error normalization now accepts unknown values and handles missing or non-array stack traces. Scheduled function error formatting also accepts unknown values and applies fallbacks for non-object values and missing message fields. Tests cover both paths, including thrown strings and custom error objects.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: serhalp, eduardoboucas, ndhoule

Merge Risk: Minimal · up to 6748e

This change stops local function serving from crashing when a function throws a non-Error value or an error without a stack trace. Unit tests cover the new cases, and no concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: functions that throw values which are not Error instances now have their errors shown. The changeset covers getNormalizedError in synchronous.ts, formatLambdaError in …
Description check Passed The description states the problem, the files changed, the fix for getNormalizedError and formatLambdaError, the new tests, and the validation run. It matches the changeset closely.
Linked Issues check Passed Issue #6215 requires that netlify dev not crash when a function throws a non-Error object without a stackTrace property. At the reviewed head, getNormalizedError() in `src/lib/functions/synchr…
Out of Scope Changes check Passed All changes address the same root cause: code that assumes thrown values have Error or lambda-local shape. getNormalizedError() in synchronous.ts is the crash site named in #6215. `formatLambdaE…
Full details: Title check

Explanation

The title names the main change: functions that throw values which are not Error instances now have their errors shown. The changeset covers getNormalizedError in synchronous.ts, formatLambdaError in utils.ts, and matching tests, so the title matches the primary change.

Full details: Linked Issues check

Explanation

Issue #6215 requires that netlify dev not crash when a function throws a non-Error object without a stackTrace property. At the reviewed head, getNormalizedError() in src/lib/functions/synchronous.ts accepts unknown. For non-Error values it reads errorType/name, errorMessage/message, and stackTrace only when each has the expected type. It uses 'Error', the thrown string or inspect(error), and an empty array as fallbacks. This removes the unguarded error.stackTrace.map(...) call that caused the crash. The Airtable-style case ({ error, message, statusCode }) now falls back to message. New unit tests in tests/unit/lib/functions/synchronous.test.ts cover the non-Error object, the lambda-local error without a stack, and the string cases. The PR's additional string handling in formatLambdaError() and its scheduled-function test go beyond the issue's acceptance criteria. They are not required for this verdict. Limitation: the test results were not re-run in this review. The verdict relies on the diff and the PR's reported test results.

Full details: Out of Scope Changes check

Explanation

All changes address the same root cause: code that assumes thrown values have Error or lambda-local shape. getNormalizedError() in synchronous.ts is the crash site named in #6215. formatLambdaError() in utils.ts is the same pattern in the scheduled and background path. It throws on a thrown string because of the in operator. The change to formatLambdaError() is a small, defensive extension of the same fix and is not unrelated. The scheduled-function test in scheduled.test.ts and the new synchronous.test.ts support these changes. The PR does not add unrelated refactoring or features in the reviewed diff. Limitation: the diff reviewed covers synchronous.ts and utils.ts. The test file hunks were taken from the PR summary, not from a full diff read.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Linked repositories: This review skipped 1 repository. Cross-repository analysis may be incomplete. Check linked repository access and settings.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ebd8f089-0ebf-4b57-9bd1-e14f93d43e64
Commits

Reviewing files that changed from the base of the PR and between e31b80d and 6748e84.

Files selected for processing: 4

  • src/lib/functions/synchronous.ts
  • src/lib/functions/utils.ts
  • tests/unit/lib/functions/scheduled.test.ts
  • tests/unit/lib/functions/synchronous.test.ts
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

lambda-local only converts Error instances into its { errorType,
errorMessage, stackTrace } shape, and leaves out stackTrace when the
error has no stack. Anything else a function throws (a custom error
object that doesn't extend Error, or a string) is passed through as is,
so getNormalizedError() failed with "Cannot read properties of undefined
(reading 'map')" and the function's error was never shown.
formatLambdaError(), used for scheduled and background functions, threw
on a thrown string for the same reason.

Fixes netlify#6215
@kuldeepsinh19
kuldeepsinh19 force-pushed the fix/6215-custom-error-without-stack branch from 6fbf895 to 6748e84 Compare November 22, 2026 08:12
@kuldeepsinh19
kuldeepsinh19 marked this pull request as ready for review November 22, 2026 08:12
@kuldeepsinh19
kuldeepsinh19 requested a review from a team as a code owner November 22, 2026 08:12
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.

netlify dev crashes if function throws custom error with no stackTrace property

1 participant