Replies: 4 comments 15 replies
-
I actually agree with you, exceptions make it quite easy to "error-out" deep in the code. On the other hand, languages that enforce strict type handling and have support to easily work with error results, such as Rust, the API of individual functions might look cleaner and more consistent and the caller cannot mess it up. The C/golang approach pleases me the least as it requires boilerplate everywhere and is easy to get wrong. |
Beta Was this translation helpful? Give feedback.
-
I looked at a few web frameworks this morning, they all allow you to either return a Response or raise an Exception. There's no reason why we can't support both. I would suggest returning a response though where you can. The job of the method is to return a response, so we should do that where we're able to, in my opinion. |
Beta Was this translation helpful? Give feedback.
-
This is what I'm proposing for methods in version 5, at this stage: from jsonrpcserver.result import Error, InvalidParams, Result, Success
@method
def fruits(color: str) -> Result:
if color != "yellow":
return InvalidParams("No fruits of that colour")
if some_other_issue:
return Error(-1, "Some other issue")
return Success("banana") The return value "Result" is a union of Success and Error ( The library will convert the Result type into a Response (which adds the original request id). |
Beta Was this translation helpful? Give feedback.
-
I've added JsonRpcError exception for now (see #178) but I'm still unsure about it. We shouldn't raise exceptions. The function allows you to return an Error so we should do that. |
Beta Was this translation helpful? Give feedback.
-
I do not know if it is part of the coming redesign but at some point you @bcb mentioned that you are looking into making ApiError a return object instead of using it as an exception. Just to be clear, I am not trying to say that either design is better than the other, I just think it is an interesting technical discussion. To that end I hope I can provide "anecdotal evidence" for exceptions as error mechanism.
I did some refactoring today on an API and I kept the backend modules as is, without exposing the methods to the rpc server. (the API is some boilerplate handling logic on top of K8s). After the refactoring the new public RPC method became something of the form
Since the methods all throw an ApiError on error, it became trivial to refactor the code using the existing pieces from the old backend. Otherwise I would have to inject error handling logic in each step. A small win perhaps, but I liked it. Looks cleaner than the typical golang/C boilerplate.
I have not thought about this in any depth so it would be great to hear from others what you and other users see as the pros/cons of the two different approaches to error handling? Consistency with other frameworks/libraries in this space? More idiomatic Python style?
Cheers!
Beta Was this translation helpful? Give feedback.
All reactions