简体   繁体   中英

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?

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM