Last active
December 19, 2015 22:39
-
-
Save richhollis/6029217 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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