Skip to content

sdk: fail an entrypoint call with its own error - #93

Open
vito wants to merge 10 commits into
mainfrom
entrypoint-function-error
Open

vito wants to merge 10 commits into
mainfrom
entrypoint-function-error

Conversation

@vito

@vito vito commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

A function that fails on a generated entrypoint now fails with its own error, values and all, instead of with the exit code of the python -m dagger.mod call exec that runs it.

Depends on the engine in dagger/dagger#15249, which defines the contract this uses. Keep this a draft until that lands.

Why not returnError

The process an entrypoint starts has no current function call to return an error to. The engine gives that exec the module but not the call, because an exec that holds a call cannot be persisted (dagger/dagger withExecDynamicInputs). So the error comes back beside the exec, the way the result comes back through /dagger/result.json.

The dispatcher

On an engine with dagger/dagger#15249, the call's process gets DAGGER_FUNCTION_ERROR_PATH. When the function fails, python -m dagger.mod call writes a report there and still exits non-zero, so a failed call is not cached:

{
  "message": "exit code: 1 [traceparent:…]",
  "values": {
    "exception.type": "dagger.ExecError",
    "exception.message": "exit code: 1 [traceparent:…]",
    "exception.stacktrace": "Traceback (most recent call last): …",
    "_type": "EXEC_ERROR",
    "cmd": ["sh", "-c", "echo exec stdout; …; exit 1"],
    "exitCode": 1,
    "stderr": "exec stderr",
    "stdout": "exec stdout"
  }
}

(This is what a function that lets an ExecError propagate writes.)

  • message is the error in full, any [traceparent:…] included, so an API error keeps the failed call it names as its origin.
    • For an error a function raises, it is the exception's own text (boom, not ValueError: boom). The traceback, which names the type, is already in the function's logs, and the type is in exception.type.
    • An ExceptionGroup gives each of its errors, the way a task group raises them.
  • values are the ones record_exception gives a dagger.Error:
    • exception.type, exception.message and exception.stacktrace;
    • extra.* for a ModuleError's extra. A FunctionError raised from another error keeps its own extra next to its cause's values.
    • an API error's GraphQL extensions, verbatim. That's the error itself, or one the exception was raised from. An exception raised while handling an API error is its own error and doesn't take the API error's values.
  • Size: the engine reads at most 1 MiB. A report over that is trimmed so it still fits: the stack trace goes first, then the message and string values are cut to 16 Ki characters, keeping any trailing [traceparent:…]. If it still doesn't fit, only the message is written.
  • The engine fails the call with an Error built from the report. A caller then gets typed values, e.g. an ExecError with the inner exit code and output, and dagger call exits with the inner exec's code.

The traceback is logged as before. With the file set, a ModuleError/QueryError message is not logged a second time, since the engine shows it.

Error origins in logs

With the file set, the dagger.mod logger gets an ErrorOriginFilter. It strips [traceparent:…] from logged messages and tracebacks: the engine reads the origin from the report, and in the function's logs it's noise.

Older engines

Without DAGGER_FUNCTION_ERROR_PATH, the dispatcher falls back to a marker, the same one the Go and Java SDKs use. It ends its stderr with ::dagger-function-error::<JSON string>: the message with the exception's type, e.g. ValueError: boom, cut to 16 Ki characters with any [traceparent:…] from the cut part kept, because a failed exec only passes on the end of its stderr.

The generated entrypoint's call() rescues the exec's GraphQLError and raises the last marker line's message.

  • Without a marker, it re-raises the exec's error unchanged. That covers a process that died before reporting (e.g. os._exit, OOM), and on a new engine it hands the engine the exec error that carries the report.
  • The install is synced before the rescued exec, so a failed build keeps its own error values and output.

On those engines only the message gets past the entrypoint: a Dang raise carries no values. The README's note on function errors describes both cases.

Existing modules pick this up on dagger generate.

Checks

  • pytest tests passes (605 tests). New unit tests cover:
    • the report's message and values: an exec error whole, wrapped and handled API errors, ModuleError/FunctionError extras;
    • the 1 MiB trim;
    • the marker line, the message rules, task groups and truncation.
  • ruff check and ruff format --check pass. mypy reports one error at _exceptions.py:99 (transform_error), which was already there.
  • End to end, core, idtui: readable errors from entrypoint SDK modules dagger#15249 pins this branch by SHA (a012142) in its viztest SDK fixture. The sdks-python-* TUI goldens there pass against it. They cover native errors, exit codes, propagated and wrapped ExecErrors, failing dependencies and a broken module.
  • The earlier entrypoint-function-error-check e2e commit is gone from this branch. Those goldens, and the engine's own function-error integration tests in that PR, take its place.

A function that failed on a generated entrypoint failed with the exit code
of `python -m dagger.mod call`, the exec that runs it: "exit code: 2". Its
error, and the error origin it names, stayed in the exec's log. An API
error's message names the call that failed, so the process's exec became
the origin instead of that call, such as a failed lint exec.

The process has no current function call to return an error to, as the old
runtime did with returnError. As in the Go and Java SDKs, `call` now ends
its stderr with the error on a `::dagger-function-error::` line, and the
generated entrypoint raises that error. The exec still fails, so a failed
call is not cached.

The error reported is the function's own: an exception with its type, an
API error whole, and each error of a task group's group. A Dang raise
carries only a message, so the error's values, such as an exec's exit code,
stop at the entrypoint. The build is synced before the rescued call, so its
failures still carry theirs.

A module picks this up when its entrypoint is generated again.

Signed-off-by: Alex Suraci <alex@dagger.io>
vito added a commit to dagger/dagger that referenced this pull request Nov 22, 2026
Pin the Python fixture's SDK to a fork of dagger/python-sdk#93, rebased on
main, whose dispatcher writes the function's error to the engine's
DAGGER_FUNCTION_ERROR_PATH, and regenerate it. Every sdks-python golden now
renders as its hand-edited target, with these differences:

- Stack traces are deferred, like wrapping: the function's logs keep the
  traceback Python prints, "ERROR [dagger.mod]" header included, in
  fail-native, fail-exec, call-failing-dep and broken-default. The error
  origins and the error echoed after the traceback are gone.
- call-failing-dep keeps the go-sdk dependency's rows as they render today:
  its ::dagger-function-error:: echo and its failed "call module entrypoint"
  row are the Go SDK's to fix.
- fail-exec-wrapped is still today's output, which these fixes change: no
  entrypoint row or error origins, and collapsed, the inner withExec with
  its logs instead of the dispatcher's "exit code: 2". The wrapping message
  is still lost.

The fixture notes drop the failures the engine no longer has.

Signed-off-by: Alex Suraci <alex@dagger.io>
@vito
vito force-pushed the entrypoint-function-error branch from db1e9c6 to 775e8d0 Compare November 22, 2026 03:22
@vito
vito marked this pull request as draft November 22, 2026 03:22
vito added 4 commits November 22, 2026 05:55
When the engine gives a call's process DAGGER_FUNCTION_ERROR_PATH, a
function's own exception, and an error importing the module's code, are
shown as Python shows an uncaught exception, with only the user's frames:
the SDK's, the generated clients', the standard library's and installed
packages' frames go, in each chained exception too, and so does the
"Unhandled exception while executing function" log line above it. The
error file keeps the whole traceback.

An API error that names no origin, such as an ID that fails to load, is
shown the same way: the engine shows no call it comes from. One that names
its origin is still not shown, the engine showing the call that failed
with its logs. Old engines keep the full logs.

Signed-off-by: Alex Suraci <alex@dagger.io>
The short traceback left out every frame under site-packages, but the
runtime installs the members of a module's workspace, the user's own
libraries among them, from their directories, not editable: an exception
raised in one lost the line that raised it. Frames of a package installed
from a local directory (PEP 610's direct_url.json) are the user's.

An API error a task group raises arrives in an ExceptionGroup: the errors
whose origin the engine shows are left out of it too, and nothing is shown
when none is left.

Signed-off-by: Alex Suraci <alex@dagger.io>
A namespace package is shared by the distributions in it, so a library
from an index in the same namespace as a local workspace member counted
as the user's: a frame is the user's when its file is one a distribution
installed from a local directory installed.

A group that loses no API error is shown as it is, keeping the user's own
group type, rather than as the plain group split derives.

Signed-off-by: Alex Suraci <alex@dagger.io>
An API error can fail a call before or after the function runs: naming
the module's declarations, or loading its result's ID. With the function
error file, `dispatch` now shows any API error the call fails with as one
from the function is: a short traceback when it names no origin, nothing
when the engine shows where it comes from. Such an error goes on as the
API error, not wrapped as an invalid input or result, so the engine gets
its origin. Old engines keep the wrapping and its log.

Signed-off-by: Alex Suraci <alex@dagger.io>
vito added 5 commits November 22, 2026 14:00
An engine that implements the function error file sets
DAGGER_FUNCTION_ERROR_PATH on a call's process. The process then writes the
function's error there instead of the ::dagger-function-error:: line, and
the engine fails the call with it as the function's own error, as it does an
error returned with returnError. The exec still fails, so a failed call is
not cached.

The file holds {"message": ..., "values": {...}}: the error's message in
full, with the origin an API error names, and the values record_exception
gives a dagger.Error: the exception's type, message and stack trace, a
ModuleError's extra values, and an API error's extensions, whole, also when
the error wraps it. A calling module so sees an ExecError, exit code and
output included, as from a runtime's function.

The function's logs then say nothing the engine shows already: the error
isn't logged again after the traceback, an exception the function raised is
reported without its type, which the traceback names, and the error origins
([traceparent:...]) are left out of what the process logs.

On an engine without the variable, the process reports the error as before.

Signed-off-by: Alex Suraci <alex@dagger.io>
A report over the engine's 1 MiB limit is dropped whole, so trim it to fit:
the stack trace goes first, then the message and string values are cut.
Take an API error's values only from the exception it was raised from, not
one it was raised while handling, and keep a FunctionError's own extra
values beside its cause's.

Signed-off-by: Alex Suraci <alex@dagger.io>
function_error_values also takes an API error's values from the exception
it was raised from, which record_exception does not.

Signed-off-by: Alex Suraci <alex@dagger.io>
On an engine that gives the call's process DAGGER_FUNCTION_ERROR_PATH, a
function's error keeps its values; only older engines get the message alone.

Signed-off-by: Alex Suraci <alex@dagger.io>
When the engine gives a call's process DAGGER_FUNCTION_ERROR_PATH, an API
error that escapes a function is no longer logged with its traceback. The
engine fails the call with the error the file reports and shows its origin,
the call that failed with its logs; the traceback ending in `dagger.ExecError:
exit code: 1` only dominated the output. The report keeps the full values,
stack trace included, and old engines still get the log and the marker.

Signed-off-by: Alex Suraci <alex@dagger.io>
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