Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return error messages in rails

I'm learning rails and confused about some basics. Here is my API method:

  def itunes_app_create
    begin
      app = Spaceship::Tunes::Application.create!(name: params[:itunes_app_name],
                                  primary_language: "English",
                                           version: params[:itunes_app_version],
                                               sku: params[:itunes_app_sku],
                                         bundle_id: params[:itunes_app_bundle_id],
                                           team_id: params[:itunes_team_id])
      render json: app.to_json, status: 200
    rescue
      render json: app.errors.full_messages.to_json, status: 200
    end
  end

My app.errors.full_messages.to_json line fails because well, I just made that up from something I saw. How do I return a message of what caused the method to fail?

Not sure if it matters, app is not part of my model. I just need to call this from my client app and then send back the result.

As a side question, what status should I return with the error in this case?

like image 517
Frankie Avatar asked Oct 18 '16 15:10

Frankie


People also ask

How to restore the method error_messages_for in rails?

The method error_messages_for was deprecated on Rails 2.3.8. So, I looked for a way to restore the feature. I found two solutions: Solution 1: Create the partial shared/_error_messages.html.erb In the case, you just need render the partial in your form. Example: module ErrorMessagesHelper # Render error messages for the given objects.

What is a flash message in rails?

A flash message is a way to communicate information with the users of your Rails application so they can know what happens as a result of their actions. You set these flash messages in your controllers, then you render them in your views. Your users can then act accordingly. Let’s learn exactly how this works!

What is the most common mistake experienced by Rails developers?

It might be interesting for you to know that expecting render and redirect to break the flow of the method and exit it immediately is one of the most common mistake experienced by some Rails developers at the beginning of their career. throw :halt (rails?) As Avdi wrote and his blogpost Rack is also internally using throw :halt.

How to render error messages for the given objects?

Solution 1: Create the partial shared/_error_messages.html.erb In the case, you just need render the partial in your form. Example: module ErrorMessagesHelper # Render error messages for the given objects.


2 Answers

You should return the errors which occurred (if any) while creating the object. Your object is an instance of Spaceship::Tunes::Application, so you should be searching for whether this class defines any instance methods which return validation errors.

I am not familiar with this class, but after a quick look into it's documentation I do not see that it even has any validations in create method.

So you can just return a custom error message if by any chance the object creation failed.

As to

what status should I return with the error in this case?

Take a look into the list of status codes and pick suitable one (I think 400 (Bad Request) would do).

like image 104
Andrey Deineko Avatar answered Oct 22 '22 02:10

Andrey Deineko


You could do something like this

def itunes_app_create
    begin
      app = Spaceship::Tunes::Application.create!(name: params[:itunes_app_name],
                                  primary_language: "English",
                                           version: params[:itunes_app_version],
                                               sku: params[:itunes_app_sku],
                                         bundle_id: params[:itunes_app_bundle_id],
                                           team_id: params[:itunes_team_id])
      render json: app.to_json, status: 200
    rescue => e
      render json: {error: e, status: 500}.to_json
    end
  end

But if you are building out a full api you might want to come up with your own error codes and a convention on when you will use which of the http errors codes that Andrey so politely included. Also its not a good practice to just blindly catch all error types because it is behavior concealing and it will generalize the behavior of your app in difference scenarios. In my example since you are return the error message it give you a little bit a visibility.

any time you are urged to write something like this

rescue => e

write something like this instead

rescue SyntaxError => e

Be very deliberate with your error handling, see this question for why

like image 30
C dot StrifeVII Avatar answered Oct 22 '22 01:10

C dot StrifeVII