Skip to content

Property error handling - #389

Open
rwb27 wants to merge 9 commits into
mainfrom
property-error-handling
Open

Property error handling#389
rwb27 wants to merge 9 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

rwb27 added 4 commits August 3, 2026 11:22
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.
@barecheck

barecheck Bot commented Aug 3, 2026

Copy link
Copy Markdown

Barecheck - Code coverage report

Total: 97.33%

Your code coverage diff: 0.02% ▴

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

rwb27 added 4 commits August 3, 2026 23:56
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.
@rwb27
rwb27 marked this pull request as ready for review August 3, 2026 23:06
This adds a base level of unit testing for resetting properties.
@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
ReturnT = TypeVar("ReturnT", bound=Response)


def exceptions_to_problemdetails(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not important but it seems funny to snake_case for most of this but not for problemdetails

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

that's a good point. I'll change that for consistency. I don't think that merits rereview.

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

2 participants