Skip to content

Instantly share code, notes, and snippets.

@richhollis
Last active December 19, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richhollis/6029217 to your computer and use it in GitHub Desktop.
Save richhollis/6029217 to your computer and use it in GitHub Desktop.
# controllers/api/api_controller.rb
class Api::ApiController < ApplicationController
respond_to :json
before_filter :ensure_user
rescue_from ::ApiException do |exception|
logger.info "ApiException #{exception.inspect}"
render :json => { :errors => { :error => exception.message} }.to_json, :status => 400
end
private
def ensure_user
unless current_user
render :json => { :errors => { :error => 'Not authenticated'} }.to_json, :status => 401
false
end
end
end
# controllers/api/v1/some_controller.rb
class Api::V1::SomeController < Api::ApiController
def index
# any errors here would be caught in the parent controller
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment