tiny-pilot / tinypilot

Use your Raspberry Pi as a browser-based KVM.
https://tinypilotkvm.com
MIT License
3k stars 252 forks source link

Make error handling in `api.py` less verbose/repetitive #898

Open jotaen4tinypilot opened 2 years ago

jotaen4tinypilot commented 2 years ago

In https://github.com/tiny-pilot/tinypilot-pro/pull/364#pullrequestreview-862312155 the idea came up to centralize the error handling of the API layer in the backend. Currently, we need to write the same try/except all over again, but the only reason we do this is for assigning HTTP status codes.

Example: In the Pro api.py, we catch 17× request_parsers.errors and then return status 400. Then there are an additional 34× where we have an except block that maps to a specific error code.

I’d spontaneously see multiple ways how we could do this: (there might be others/more)

Not sure how much effort this is worth investing in. On the other hand, out current approach is indeed quite verbose/repetitive for no real “benefit”, and api.py continues to grow.

jotaen4tinypilot commented 2 years ago

It’s also worth keeping in mind that we don’t rely on HTTP status codes that much anyway, but we assign string codes in case we need to distinguish errors in the frontend.

So a refactoring like this would be mostly for code sanity, not so much for improved functionality.