How to return rail error messages

I study rails and confuse 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 line app.errors.full_messages.to_jsonfails because I just made it from what I saw. How to return a message about what caused a method failure?

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

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

+4
source share
2 answers

, ( ) . Spaceship:: Tunes:: Application, , , .

, , - create method.

, , .

​​ ?

(, 400 (Bad Request)).

+2

-

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

api, , HTTP, . , , , . , , .

, -

rescue => e

-

rescue SyntaxError => e

, . question,

0

Source: https://habr.com/ru/post/1658110/


All Articles