A
A
Andriy Stepanyuk2015-09-09 10:47:02
Ruby on Rails
Andriy Stepanyuk, 2015-09-09 10:47:02

Best practices for throwing exceptions in ruby?

Good afternoon, I have such a general (one might say philosophical) question about exceptions in ruby ​​and rails.
There is a lot of everything on the network about how to process and catch them, but when they are not generated (or I’m not looking for it that way).
It is clear that exceptions need to be generated when an exceptional situation occurs, there is no need to tie logic on exceptions, this is understandable.
Question to the public, what situations do you consider exceptional? Links to github of better code? I would like more projects on ruby ​​on rails.
Let's say:
RoR project: a user can specify several companies with which he works, params.require(:user).permit(:email, ..., company_ids: []) is passed through the form, i.e. {"email": "[email protected]", ... ,"company_ids": ["12", "
The User model has a method:
def update_with_companies params_with_companies
...
end
I assume that the situation is when the user tries to add non-existent companies (they are not in the list of companies in the form), or duplicates the company (although js does not allow this in the form, i.e. the user sends, for example, a curl request), or something else then the abnormal is all an exception and the update_with_companies method will throw an exception. if everything is OK, it will simply return an instance of the User class. By the way, what do you think about passing params_with_companies which is essentially params from the controller to the model? Is it necessary to pass the same parameters in another form to the model after processing them in the controller (the fact is that the companies of a particular user are connected to the User model through associations and the parameters are processed inside the update_with_companies method)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Mirilaczvili, 2015-09-09
@2ord

Exceptions are covered in detail in Exceptional Ruby . Even though it was published in early 2011, it is still relevant.
When processing an invalid request from an HTTP client, you must return an HTTP response.
REST response code for invalid data
Any HTTP 40x responses indicate that the HTTP client is not working properly.
In the case of working in Ruby on Rails, exceptions are apparently considered normal practice and therefore there is no getting away from them.
Trying to get a POST to return 400 bad request
When processing POST requests, you should give the user a message about where the error is ("Select companies from the list"). If the error is in the JS logic, then you need to fix the JS code.

J
Jeiwan, 2015-09-09
@Jeiwan

Exceptions should be thrown in case of errors, when the program cannot continue to work. But it's also worth remembering that exceptions in Ruby work slowly, so it's better to use them less often. And for error handling, use the usual conditions (if, else, unless ...).
This check needs to be moved to model validation. Make your own custom method.
There is no need for an exception here. The update_with_companies method should work the same as the standard save method. Perhaps, if we remove the check of companies in validation, the update_with_companies method can be abandoned altogether.
params returns a hash, there is nothing wrong with passing a hash to the model method. Just need to add extra validation for the data as it is being passed from the user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question