Skip to content

Commit

Permalink
Merge pull request #739 from trade-tariff/HOTT-4630-error-controller
Browse files Browse the repository at this point in the history
HOTT-4630: Extend DutyCalculator ErrorsController
  • Loading branch information
alexdesi committed Dec 22, 2023
2 parents 94e23de + 309d354 commit fa709ec
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 119 deletions.
70 changes: 64 additions & 6 deletions app/controllers/errors_controller.rb
Expand Up @@ -3,25 +3,83 @@ class ErrorsController < ApplicationController

helper_method :user_session

def bad_request
message = "The request you made is not valid.<br>
Please contact support for assistance or try a different request.".html_safe

respond_to do |format|
format.html { render 'error', status: :bad_request, locals: { header: 'Bad request', message: } }
format.json { render json: { error: 'Bad request' }, status: :bad_request }
format.all { render status: :bad_request, plain: 'Bad request' }
end
end

def not_found
message = 'You may have mistyped the address or the page may have moved.'

respond_to do |format|
format.html { render status: :not_found }
format.html { render 'error', status: :not_found, locals: { header: 'The page you were looking for does not exist.', message: } }
format.json { render json: { error: 'Resource not found' }, status: :not_found }
format.all { render status: :not_found, body: nil }
format.all { render status: :not_found, plain: 'Resource not found' }
end
end

def internal_server_error
def method_not_allowed
message = "We're sorry, but this request method is not supported.<br>
Please contact support for assistance or try a different request.".html_safe

respond_to do |format|
format.html { render status: :internal_server_error }
format.json { render json: { error: 'Internal server error' }, status: :internal_server_error }
format.html { render 'error', status: :method_not_allowed, locals: { header: 'Method not allowed', message: } }
format.json { render json: { error: 'Method not allowed' }, status: :method_not_allowed }
format.all { render status: :method_not_allowed, plain: 'Bad request' }
end
end

def not_acceptable
message = "Unfortunately, we cannot fulfill your request as it is not in a format we can accept.<br>
Please contact support for assistance or try a different request.".html_safe

respond_to do |format|
format.html { render 'error', status: :not_acceptable, locals: { header: 'Not acceptable', message: } }
format.json { render json: { error: 'Not acceptable' }, status: :not_acceptable }
format.all { render status: :not_acceptable, plain: 'Not accepdtable' }
end
end

def unprocessable_entity
message = "Maybe you tried to change something you didn't have access to."

respond_to do |format|
format.html { render status: :unprocessable_entity }
format.html do
render 'error', status: :unprocessable_entity,
locals: { header: 'The change you wanted was rejected.', message: }
end
format.json { render json: { error: 'Unprocessable entity' }, status: :unprocessable_entity }
format.all { render status: :unprocessable_entity, plain: 'Unprocessable entity' }
end
end

def internal_server_error
message = 'We could not proceed with calculating your duty.'

respond_to do |format|
format.html do
render 'error', status: :internal_server_error,
locals: { header: 'Sorry, there is a problem with the service', message: }
end
format.json { render json: { error: 'Internal server error' }, status: :internal_server_error }
format.all { render status: :internal_server_error, plain: 'Internal server error' }
end
end

def not_implemented
message = 'We\'re sorry, but the requested action is not supported by our server at this time.<br>
Please contact support for assistance or try a different request.'.html_safe

respond_to do |format|
format.html { render 'error', status: :not_implemented, locals: { header: 'Not implemented', message: } }
format.json { render json: { error: 'Not implemented' }, status: :not_implemented }
format.all { render status: :not_implemented, plain: 'Not Implemented' }
end
end
end
12 changes: 12 additions & 0 deletions app/views/errors/error.html.erb
@@ -0,0 +1,12 @@
<main role="main" class="govuk-main-wrapper" id="main-content">
<div class="govuk-grid-row">
<div class="govuk-grid-column-full-width">
<h1 class="govuk-heading-xl"><%= header %></h1>
<p class="govuk-body"><%= message %></p>

<% if user_session.commodity_code.present? %>
<p class="govuk-body">Click here to <%= link_to('start again', import_date_path(referred_service: user_session.referred_service, commodity_code: user_session.commodity_code), class: 'govuk-link') %>.</p>
<% end %>
</div>
</div>
</main>
9 changes: 0 additions & 9 deletions app/views/errors/internal_server_error.html.erb

This file was deleted.

9 changes: 0 additions & 9 deletions app/views/errors/not_found.html.erb

This file was deleted.

9 changes: 0 additions & 9 deletions app/views/errors/unprocessable_entity.html.erb

This file was deleted.

5 changes: 3 additions & 2 deletions config/environments/test.rb
Expand Up @@ -20,11 +20,12 @@
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=#{1.hour.to_i}"
"Cache-Control" => "public, max-age=#{1.hour.to_i}",
}

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.consider_all_requests_local = false
config.action_dispatch.show_exceptions = true
config.action_controller.perform_caching = false
config.cache_store = :null_store

Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
root to: proc { [404, {'Content-Type' => 'text/html'}, ['Not found.']] }
root to: proc { [404, { 'Content-Type' => 'text/html' }, ['Not found.']] }

get 'healthcheckz', to: 'healthcheck#checkz'

Expand Down Expand Up @@ -64,7 +64,11 @@
get 'prefill', to: 'steps/prefill_user_session#show'
end

get '400', to: 'errors#bad_request', via: :all
get '404', to: 'errors#not_found', via: :all
get '405', to: 'errors#method_not_allowed', via: :all
get '406', to: 'errors#not_acceptable', via: :all
get '422', to: 'errors#unprocessable_entity', via: :all
get '500', to: 'errors#internal_server_error', via: :all
get '501', to: 'errors#not_implemented', via: :all
end
22 changes: 0 additions & 22 deletions spec/controllers/errors_controller_spec.rb

This file was deleted.

28 changes: 0 additions & 28 deletions spec/controllers/steps/errors_controller_spec.rb

This file was deleted.

92 changes: 92 additions & 0 deletions spec/requests/errors_controller_spec.rb
@@ -0,0 +1,92 @@
RSpec.describe ErrorsController, type: :request do
include ApplicationHelper

subject(:rendered) { make_request && response }

let(:json_response) { JSON.parse(rendered.body) }
let(:body) { rendered.body }

shared_examples 'a json error response' do |status_code, message|
it { is_expected.to have_http_status status_code }
it { is_expected.to have_attributes media_type: 'application/json' }
it { expect(json_response).to include 'error' => message }
end

describe 'GET /400.json' do
let(:make_request) { get '/400.json' }

it_behaves_like 'a json error response', 400, 'Bad request'
end

describe 'GET /404.json' do
let(:make_request) { get '/404.json' }

it_behaves_like 'a json error response', 404, 'Resource not found'
end

describe 'GET /405.json' do
let(:make_request) { get '/405.json' }

it_behaves_like 'a json error response', 405, 'Method not allowed'
end

describe 'GET /406.json' do
let(:make_request) { get '/406.json' }

it_behaves_like 'a json error response', 406, 'Not acceptable'
end

describe 'GET /422.json' do
let(:make_request) { get '/422.json' }

it_behaves_like 'a json error response', 422, 'Unprocessable entity'
end

describe 'GET /500.json' do
let(:make_request) { get '/500.json' }

it_behaves_like 'a json error response', 500, 'Internal server error'
end

describe 'GET /501.json' do
let(:make_request) { get '/501.json' }

it_behaves_like 'a json error response', 501, 'Not implemented'
end

describe 'GET /400' do
let(:make_request) { get '/400' }

it { expect(body).to include 'Bad request' }
end

describe 'GET /404' do
let(:make_request) { get '/404' }

it { expect(body).to include 'The page you were looking for does not exist.' }
end

describe 'GET /406' do
let(:make_request) { get '/406' }

it { expect(body).to include 'Not acceptable' }
end

describe 'GET /422' do
let(:make_request) { get '/422' }

it { expect(body).to include 'The change you wanted was rejected.' }
end

describe 'GET /500' do
let(:make_request) { get '/500' }

it { expect(body).to include 'Sorry, there is a problem with the service' }
end

describe 'GET #501' do
let(:make_request) { get '/501' }

it { expect(body).to include 'Not implemented' }
end
end
13 changes: 13 additions & 0 deletions spec/views/errors/error_spec.rb
@@ -0,0 +1,13 @@
RSpec.describe 'errors/error', type: :view do
let(:user_session) { build(:user_session, :with_commodity_information) }

it 'renders a link to the import_date_path' do
render template: 'errors/error',
locals: { user_session:, header: 'Error header', message: 'Error message' }

expected_path = import_date_path(referred_service: user_session.referred_service,
commodity_code: user_session.commodity_code)

expect(rendered).to include(expected_path)
end
end
11 changes: 0 additions & 11 deletions spec/views/errors/internal_server_error_spec.rb

This file was deleted.

11 changes: 0 additions & 11 deletions spec/views/errors/not_found_spec.rb

This file was deleted.

11 changes: 0 additions & 11 deletions spec/views/errors/unprocessable_entity_spec.rb

This file was deleted.

0 comments on commit fa709ec

Please sign in to comment.