Respond to all formats with html

This commit is contained in:
David Cook
2023-08-10 15:44:31 +10:00
parent c191158a97
commit f64a24a5bb
3 changed files with 38 additions and 1 deletions

View File

@@ -9,7 +9,7 @@ class ErrorsController < ApplicationController
event.add_metadata(:request, request.env)
end
render status: :not_found
render "not_found", status: :not_found, formats: :html
end
def internal_server_error

View File

@@ -0,0 +1,21 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Errors', type: :request do
include ExceptionHelper
shared_examples "returning a HTTP 404" do |path|
it path do
rails_respond_without_detailed_exceptions do
get path
end
expect(response).to have_http_status(:not_found)
end
end
it_behaves_like "returning a HTTP 404", "/nonexistent/path"
it_behaves_like "returning a HTTP 404", "/nonexistent/path.jpg"
it_behaves_like "returning a HTTP 404", "/nonexistent/path.xml"
end

View File

@@ -0,0 +1,16 @@
# frozen_string_literal: true
# https://medium.com/@edgar/rspec-how-to-have-rails-request-specs-handling-errors-like-production-a8c21ed0308a
module ExceptionHelper
def rails_respond_without_detailed_exceptions
env_config = Rails.application.env_config
original_show_exceptions = env_config['action_dispatch.show_exceptions']
original_show_detailed_exceptions = env_config['action_dispatch.show_detailed_exceptions']
env_config['action_dispatch.show_exceptions'] = true
env_config['action_dispatch.show_detailed_exceptions'] = false
yield
ensure
env_config['action_dispatch.show_exceptions'] = original_show_exceptions
env_config['action_dispatch.show_detailed_exceptions'] = original_show_detailed_exceptions
end
end