Skip to content

fix(errors): render user-config and base-alias failures as structured errors#1082

Open
tonyandrewmeyer wants to merge 8 commits into
canonical:mainfrom
tonyandrewmeyer:agent/structured-validation-errors
Open

fix(errors): render user-config and base-alias failures as structured errors#1082
tonyandrewmeyer wants to merge 8 commits into
canonical:mainfrom
tonyandrewmeyer:agent/structured-validation-errors

Conversation

@tonyandrewmeyer

@tonyandrewmeyer tonyandrewmeyer commented May 26, 2026

Copy link
Copy Markdown

Unhandled pydantic ValidationErrors and host base-alias lookup failures fall through to the generic handler and are reported as ' internal error: ...' with exit code 70, leaking a raw Python / pydantic traceback (including the errors.pydantic.dev link) for what are plainly user configuration problems.

This PR changes handle_runtime_error to convert pydantic.ValidationError into a structured CraftValidationError with a 'Recommended resolution' (exit EX_DATAERR), and surfaces 'not a valid BuilddBaseAlias' ValueErrors as a structured config error (exit EX_CONFIG). pydantic.ValidationError is a ValueError subclass, so it is matched first. A TODO marks a potential deeper fix for the base-alias lookup site.

  • Have you followed the guidelines for contributing?
  • Have you signed the CLA?
  • Have you successfully run make lint && make test? (I only get the same failures as main).
  • Have you added an entry to the changelog (docs/reference/changelog.rst)?

@tonyandrewmeyer tonyandrewmeyer force-pushed the agent/structured-validation-errors branch from e5276c2 to dff017d Compare May 26, 2026 08:14
@tonyandrewmeyer tonyandrewmeyer marked this pull request as ready for review May 26, 2026 08:14
@tonyandrewmeyer tonyandrewmeyer requested a review from a team as a code owner May 26, 2026 08:14
Copilot AI review requested due to automatic review settings May 26, 2026 08:14
Comment thread craft_application/util/logging.py Outdated
Comment on lines +120 to +123
# TODO: This is a shallow guard keyed off the exception # noqa: FIX002
# message. The proper fix is to raise a typed CraftError at the
# base-alias lookup site (in craft_providers/craft_platforms) so
# this string match can be removed.

@bepri bepri May 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with the fix proposed by this, but instead of silencing the lint error could you please:

  1. Create an issue on the relevant repository to fix it at the lookup site
  2. Create an issue on this repository that says "this block of code should be removed/improved"
  3. Link the first issue to the second, noting that the first one blocks the second
  4. Add a link to the second issue in this comment in place of the "TODO:"

return_code = os.EX_DATAERR
transformed = errors.CraftValidationError.from_pydantic(
error,
file_name=f"{app.name}.yaml",

@bepri bepri May 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's not a guarantee that this is the offending file. For example, Snapcraft could also be any one of:

  • .snapcraft.yaml
  • snap/snapcraft.yaml
  • build-aux/snap/snapcraft.yaml

I'm not sure what the right fix is here to pass this information down. @lengau WDYT about a solution involving kwargs to optionally pass the output of ProjectService.resolve_project_file_path() into this method?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Longer term yes, but let's not block this PR on it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

very well then

Comment thread tests/unit/util/test_logging.py Outdated
@bepri bepri requested review from a team and lengau May 26, 2026 15:19
@bepri

bepri commented May 26, 2026

Copy link
Copy Markdown
Member

Requesting an author's review because this adds new user-oriented error messages.

… errors

Unhandled pydantic ValidationErrors and host base-alias lookup failures
fell through to the generic handler and were reported as
'<app> internal error: ...' with exit code 70, leaking a raw Python/
pydantic traceback (including the errors.pydantic.dev link) for what are
plainly user configuration problems.

handle_runtime_error now converts pydantic.ValidationError into a
structured CraftValidationError with a 'Recommended resolution' (exit
EX_DATAERR), and surfaces 'not a valid BuilddBaseAlias' ValueErrors as a
structured config error (exit EX_CONFIG). pydantic.ValidationError is a
ValueError subclass, so it is matched first. A TODO marks the deeper fix
for the base-alias lookup site.
Signed-off-by: Tony Meyer <tony.meyer@canonical.com>
Per review on canonical#1082: replace the TODO/lint-silencer with references to
canonical#1086 (remove this guard) and
canonical/craft-providers#969 (raise typed error at the lookup site).

Signed-off-by: Tony Meyer <tony.meyer@canonical.com>
@tonyandrewmeyer tonyandrewmeyer requested a review from bepri June 2, 2026 11:05
Comment thread docs/reference/changelog.rst Outdated
Comment on lines +70 to +81
Fixes
=====

- Unhandled ``pydantic.ValidationError`` is now reported as a structured
project configuration error (exit ``EX_DATAERR``) with a recommended
resolution, rather than as an "internal error" with a raw pydantic
traceback.
- ``ValueError`` failures from a host base-alias lookup
(``'<release>' is not a valid BuilddBaseAlias``) are now reported as a
structured configuration error (exit ``EX_CONFIG``) instead of as an
"internal error".

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

7.0.0 is released, so let's move this to 7.1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you move it to the top of the version sections so it comes before 7.0.0?

@lengau lengau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks!

tonyandrewmeyer and others added 2 commits June 5, 2026 21:40
7.0.0 has already been released without these entries, so move them
to a new 7.1.0 (unreleased) section.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

3 participants