Skip to content

Commit

Permalink
Handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vertism committed Sep 24, 2024
1 parent 80534ef commit fee4011
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def not_found
raise ActiveRecord::RecordNotFound
end
end
4 changes: 2 additions & 2 deletions app/controllers/correspondence_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CorrespondenceController < ApplicationController
rescue_from Redis::CannotConnectError do
render file: "public/500-redis-down.html", status: :internal_server_error, layout: false
render "errors/internal_error", status: :internal_server_error, layout: false
end

def start; end
Expand Down Expand Up @@ -42,7 +42,7 @@ def search
def authenticate
@correspondence = Correspondence.where(uuid: params[:uuid]).first
if @correspondence.nil?
render file: Rails.root.join("public/404.html"), status: :not_found, layout: false
not_found and return
else
@correspondence.authenticate!
CorrespondenceMailer.new_correspondence(@correspondence).deliver_later
Expand Down
15 changes: 11 additions & 4 deletions spec/controllers/correspondence_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@

context "when uuid not in database" do
it "responds Not found" do
get :authenticate, params: { uuid: "ffffffff-eeee-dddd-cccc-bbbbbbbbbbb" }
expect(response).to have_http_status(:not_found)
expect {
get :authenticate, params: { uuid: "ffffffff-eeee-dddd-cccc-bbbbbbbbbbb" }
}.to raise_error(ActiveRecord::RecordNotFound)
end
end

Expand All @@ -117,12 +118,18 @@
before { correspondence.authenticated_at = authenticated_time }

it "does not update the authenticated at date" do
get :authenticate, params: { uuid: "3cc98e93-d11c-42ad-832d-f40113d3ec27" }
expect {
get :authenticate, params: { uuid: "3cc98e93-d11c-42ad-832d-f40113d3ec27" }
}.to raise_error(ActiveRecord::RecordNotFound)

expect(correspondence.authenticated_at).to eq authenticated_time
end

it "does not resend the email" do
get :authenticate, params: { uuid: "3cc98e93-d11c-42ad-832d-f40113d3ec27" }
expect {
get :authenticate, params: { uuid: "3cc98e93-d11c-42ad-832d-f40113d3ec27" }
}.to raise_error(ActiveRecord::RecordNotFound)

expect(CorrespondenceMailer).not_to receive(:new_correspondence)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/errors_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec_helper"
require "rails_helper"

RSpec.describe ErrorsController, type: :controller do
context "when not found" do
Expand Down

0 comments on commit fee4011

Please sign in to comment.