Skip to content

fix: return nil from Resource.find on uncastable id#148

Merged
tfwright merged 1 commit into
mainfrom
fix/find-with-invalid-id
Jun 1, 2026
Merged

fix: return nil from Resource.find on uncastable id#148
tfwright merged 1 commit into
mainfrom
fix/find-with-invalid-id

Conversation

@tfwright
Copy link
Copy Markdown
Owner

@tfwright tfwright commented Jun 1, 2026

Summary

Visiting a show/edit URL with an id that can't be cast to the schema's primary key type (e.g. /foo/show/null or /foo/edit/abc when the pk is an integer) raised Ecto.Query.CastError inside LiveAdmin.Components.Container.handle_params/3 during connected mount, which LiveView reports to the client as a 400.

Rescue the cast error in Resource.find/5 and return nil — the existing record: nil branch in Components.Container.Form.render/1 already renders a "No record found" message, so callers see a graceful result instead of a crash.

Test plan

  • New test exercising /user/edit/not-a-number asserts the "No record found" message renders
  • mix test test/live_admin/components/container_test.exs — 31 tests, 0 failures

Visiting a show/edit URL with an id that can't be cast to the schema's
primary key type (e.g. "null" or "abc" for an integer pk) raised
Ecto.Query.CastError during connected mount, surfaced as a 400. Rescue
the cast error and return nil so the existing "No record found" path
renders instead.
@tfwright tfwright merged commit 085c3a6 into main Jun 1, 2026
6 checks passed
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.

1 participant