From ada74756f460d9b25e26c7da6f6904c5c08f1ea1 Mon Sep 17 00:00:00 2001 From: Liam Bowen Date: Fri, 13 May 2016 22:58:08 -0400 Subject: [PATCH 1/3] specify that we should only respond to :html here --- app/controllers/main_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index 4704340c..e9f4b657 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -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 From c7a9bb5ba2ec771b6b90f341c91ce1c4dae66ffb Mon Sep 17 00:00:00 2001 From: Liam Bowen Date: Fri, 13 May 2016 22:58:20 -0400 Subject: [PATCH 2/3] change error handling in ApplicationController get rid of bizarre error handling, and replace it with better stuff. --- app/controllers/application_controller.rb | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 88781029..4b974750 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 From d14945aecda150a446152e824c195c28ebd1bfec Mon Sep 17 00:00:00 2001 From: Josh Jordan Date: Sat, 14 May 2016 14:26:42 -0700 Subject: [PATCH 3/3] Tests for unknownformat exception handler --- spec/controllers/unknown_format_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 spec/controllers/unknown_format_spec.rb diff --git a/spec/controllers/unknown_format_spec.rb b/spec/controllers/unknown_format_spec.rb new file mode 100644 index 00000000..79483dcc --- /dev/null +++ b/spec/controllers/unknown_format_spec.rb @@ -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