Skip to content

Property error handling - #389

Merged
rwb27 merged 12 commits into
mainfrom
property-error-handling
Aug 6, 2026
Merged

Property error handling#389
rwb27 merged 12 commits into
mainfrom
property-error-handling

Conversation

@rwb27

@rwb27 rwb27 commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

This builds on the improved error handling for Actions in #333 and #370 to also handle errors in property getter/setter code. Things added by this PR:

  • A new decorator that handles exceptions by returning a Response object containing ProblemDetails describing the error.
  • Using that new decorator to return helpful error messages if a property getter or setter fails. Happily, this also tidies up the code for handling pydantic errors that occur when serialising property values.

An incidental change is that setting or resetting a property now returns a Response containing None, rather than returning None directly. I believe this is not a change in behaviour, but it keeps things consistent and means my type hints make sense. I added a test for resetting properties as well, as I made the same change to that function.

Things to do before merging:

  • Check the logs in tests, to make sure logs go to helpful places.
  • Verify that the response from setting/resetting properties has not changed.
  • Add explicit testing of the new functions in problem_details

Closes #386
Closes #391

@barecheck

barecheck Bot commented Aug 3, 2026

Copy link
Copy Markdown

Barecheck - Code coverage report

Total: 97.34%

Your code coverage diff: 0.02% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/properties.py502, 520, 969, 992

@rwb27
rwb27 marked this pull request as ready for review August 3, 2026 23:06
@rwb27
rwb27 requested a review from julianstirling August 3, 2026 23:26
@rwb27 rwb27 added this to the v0.3.0 milestone Aug 5, 2026
Comment thread src/labthings_fastapi/problem_details.py Outdated
Comment thread tests/test_property_errors.py Outdated
Comment thread tests/test_property_errors.py Outdated
rwb27 and others added 12 commits August 6, 2026 09:55
This ought to be a helpful way to check when I've improved the error handling in properties.
This is in response to #386.

Unfortunately, too many of these tests already pass: this is an annoying artifact of the way
TestClient is implemented.
This commit adds a decorator that provides a catch-all exception handler.
If an exception occurs in a decorated function, it will log, and return a JSONResponse containing
a ProblemDetails object, with an appropriate status code.

This new wrapper is then used to ensure errors that occur in property get/set/reset functions
are properly logged and a sensible HTTP response is given to the user.

This needs testing explicitly in `test_problemdetails.py` and also
needs tests fixing in `test_property_errors.py`.
This is required by new error handling code in properties.
Previously, setting a property didn't return a response.
I changed this to return an empty response, which then caused errors in ThingClient.
I've now changed this to return a JSONResponse with a value of `None`.
I believe this matches the previous behaviour, and fixes the test failures.
I've run this test on both this branch and `main` to check that the response is identical.
I added a __name__ == "__main__" block for manual
testing during development - this is no longer needed.
This adds a base level of unit testing for resetting properties.
Co-authored-by: Beth <167304066+bprobert97@users.noreply.github.com>
Thanks to @julianstirling for pointing out my inconsistent snake case before it hit `main`.
@rwb27
rwb27 force-pushed the property-error-handling branch from ec4d00d to 10a5b3a Compare August 6, 2026 08:55
@rwb27
rwb27 merged commit 9649bbc into main Aug 6, 2026
16 checks passed
@rwb27
rwb27 deleted the property-error-handling branch August 6, 2026 08:59
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.

Test property reset via HTTP Property setter errors are eaten by ASGI error

3 participants