6

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?

Frankie
  • 11,508
  • 5
  • 53
  • 60
  • 1
    Never ever do a basic `begin`/`rescue` without rescuing the StandardError class. (https://robots.thoughtbot.com/rescue-standarderror-not-exception) – MrYoshiji Oct 18 '16 at 16:31

2 Answers2

3

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

rohanagarwal
  • 771
  • 9
  • 30
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • @AndreyDeineko Thanks, I was looking through that documentation as well and didn't see any information either. I was hoping I was missing something because I want to return the specific reason back to the user. – Frankie Oct 18 '16 at 15:28
  • @Frankie I think you can try failing the creation on purpose and see, what it returns, this way you'd know what to return to user. Of course, it is not the best option, but since you have checked the docs not much left to do here.. – Andrey Deineko Oct 18 '16 at 15:28
  • @AndreyDeineko how do I return just whatever it returns? `rescue \ render json: app.to_json, status: 200` ? Or do you mean by looking in the server logs once it fails? – Frankie Oct 18 '16 at 15:32
  • @Frankie test it in console, so that you see the exception output. First thing you have to find out is whether it at all returns any validation errors/exceptions. Then you'll decide on what to do with it. Basically you'd probably do something like `...create!... rescue SomeSpecificErrorYouFoundOutWhileTesting => e; render json: error.message, status 400; Rails.logger.warn "Error occurred: #{error.class: error.messge}"` – Andrey Deineko Oct 18 '16 at 15:36
  • @Frankie it says in docs, that `name cant be bigger, than 255 chars` - try creating an object passing bigger string as `name`. Also you can scan `app.methods` array for anything like `errors` or something. – Andrey Deineko Oct 18 '16 at 15:43
1

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

Community
  • 1
  • 1
C dot StrifeVII
  • 1,885
  • 1
  • 16
  • 21