Skip to content

Commit

Permalink
Merge pull request #109 from tenex/lb/improve-unknown-format-error-ha…
Browse files Browse the repository at this point in the history
…ndling

Lb/improve unknown format error handling
  • Loading branch information
joshjordan committed May 14, 2016
2 parents 13877dc + d14945a commit 6312b6a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
15 changes: 3 additions & 12 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
class ApplicationController < ActionController::Base
rescue_from Exception, with: :show_error
rescue_from ActionController::UnknownFormat, with: :handle_unknown_format

def show_error(e)
Rails.logger.error(e)

raise if !request.xhr? || !Rails.env.development?

if Rails.env.development?
render json: { message: e.message, log: e.backtrace.join("\n") },
status: :unprocessable_entity
else
render json: { message: e.message }, status: :unprocessable_entity
end
def handle_unknown_format
render nothing: true, status: 406
end
end
2 changes: 1 addition & 1 deletion app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class MainController < ApplicationController
before_action :redirect_to_https, only: ['home']

def home
render(json: request.env.inspect) && return if params[:debug]
respond_to :html
end

def status
Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/unknown_format_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'spec_helper'

feature 'Unexpected request formats' do
controller do
def index
respond_to do |format|
format.html { render nothing: true }
end
end
end

scenario 'accepted content-type matches exceptations' do
get :index

response.should be_success
end

scenario 'content-type format does not match expectations' do
get :index, format: :json

response.status.should eq 406
end
end

0 comments on commit 6312b6a

Please sign in to comment.