Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider having the renderer decide on success/error with status code. #61

Open
beauby opened this issue Sep 19, 2017 · 1 comment
Open

Comments

@beauby
Copy link
Member

beauby commented Sep 19, 2017

Instead of having render jsonapi: foo and render jsonapi_errors: errors, simply have render jsonapi: foo and render jsonapi: errors, status: 400.

@nuheluxulu
Copy link

Thanks for this comment.
Solved it as follows:

#controllers/api/v1/api_controller.rb

class ApiController < ApplicationController
  include ExceptionHandler
end

#controllers/api/v1/concerns/ExceptionHandler.rb

module ExceptionHandler
  # provides the more graceful `included` method
  extend ActiveSupport::Concern
  included do
    rescue_from Exception do |exception|
      case exception
      when ActiveRecord::RecordNotFound
        render jsonapi_errors: {message: exception.message}, status: :not_found
      when ActiveRecord::RecordInvalid
        render jsonapi_errors: {message: exception.message}, status: :unprocessable_entity
      # else
        # figure out how to render_500
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants