3 ms·
In some API code, we adopted and use the heck of out the pattern of making Exception subclasses that mapped to HTTP error codes, like: class NotFoundError(Cl
by kstrauser 4y ago
In some API code, we adopted and use the heck of out the pattern of making Exception subclasses that mapped to HTTP error codes, like:
class NotFoundError(ClientError):
"""Not Found."""
status = 404
The top level error handler would catch exceptions raised like:
raise NotFoundError(f"The requested object ID {object_id} doesn't exist.")
and turn them into consistently formatted error messages. The end result was that you could write very readable, understandable code like:
def handle(request):
validate_signature(request)
check_authorization(request)
person = fetch_from_database(request.object_id)
return {"first_name": person.first_name}
The `validate_signature` method call may raise a 400 if the request is invalid. `check_authorization` could raise a 403 exception. `fetch_from_database` would raise a 404 if the row couldn't be fetched. In effect, each line of that function has an implicit early return: if it fails, processing stops there.
Compare and contrast with:
def handle(request):
if validate_signature(request):
if check_authorization(request):
person = fetch_from_database(request.object_id)
if person:
return {"first_name": person.first_name}
else:
return 404
else:
return 403
else:
return 400
Python's exceptions are idiomatic. Embrace them.
- eddd-ddde 4y agoWhat about actually using early returns ? I feel like the implicitness of exceptions is more of a downside than an advantage def handle(request): if !validate_signature(request): return 400 if !check_authorization(request): return 403 person = fetch_from_database(request.object_id) if !person: return 404 return { "first_name":person.first_name }
- kstrauser 4y agoThat's twice the code for the same result. Also, `validate_signature` itself might have lots of similar code, like def validate_signature(request): signature = sig_from_request(request) payload = payload_from_request(request) check_signature(payload, signature) where each of those nested calls follows the same pattern. Yeah, you can do all of that with passing around error objects. A lot of people writing Go do that every day, so obviously it works and it's not overwhelmingly tedious. I highly prefer the Python style of EAFP though.
- deleted 4y ago[deleted]
- the_gipsy 4y agoThe most ergonomic error handling is a Result<Value,Error> with some syntax sugar to get-value-or-return-early-error like rust's ? operator.
- PartiallyTyped 4y agoIOW the most ergonomic error handling is a monad, and I agree.
- int_19h 4y agoAnd exceptions can be considered a syntactic sugar on top of just such a monad. It's just that many believe it's too much sugar to be good for you, particularly the implicit rebind part.
- the_gipsy 4y agoToo much sugar can be bad, for flavor and health.
- eddd-ddde 4y agoThere's this language called zig, and there errors are represented as union values and have this kind of syntax to work with but way more explicit, really like the idea of how they do it.
- PartiallyTyped 4y agoNot exactly. You may catch exceptions in between the computations, whereas with monads you have to unpack and match the error. You can replicate the monad behaviour by catching the exceptions at the top level. I find that this approach is generally cleaner than intermediate exceptions but is more restrictive.