How to return error messages in rails
error-handling, fastlane, ruby-on-rails
Solution
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).
Problem
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?