Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/utopia/exceptions/application_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2026, by Samuel Williams.

module Utopia
module Exceptions
# Exceptions raised by application code which can be safely handled and reported.
APPLICATION_ERRORS = [StandardError, ScriptError].freeze
end
end
7 changes: 4 additions & 3 deletions lib/utopia/exceptions/handler.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2014-2025, by Samuel Williams.
# Copyright, 2014-2026, by Samuel Williams.
# Copyright, 2025, by Olle Jonsson.

require "console"

require_relative "../middleware"
require_relative "../request"
require_relative "../response"
require_relative "application_errors"

module Utopia
module Exceptions
Expand Down Expand Up @@ -37,7 +38,7 @@ def freeze
def call(request)
begin
return @delegate.call(request)
rescue Exception => exception
rescue *APPLICATION_ERRORS => exception
Console.warn(self, "An error occurred while processing the request.", error: exception)

begin
Expand All @@ -52,7 +53,7 @@ def call(request)
error_response.status = 500

return error_response
rescue Exception => exception
rescue *APPLICATION_ERRORS => exception
# If redirection fails, we also finish with a fatal error:
Console.error(self, "An error occurred while invoking the error handler.", error: exception)
return Response[500, {"content-type" => "text/plain"}, ["An error occurred while processing the request."]]
Expand Down
6 changes: 3 additions & 3 deletions lib/utopia/exceptions/mailer.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2016-2025, by Samuel Williams.
# Copyright, 2016-2026, by Samuel Williams.

require "net/smtp"
require "mail"

require_relative "../middleware"
require_relative "../request"
require_relative "handler"
require_relative "application_errors"

module Utopia
module Exceptions
Expand Down Expand Up @@ -59,7 +59,7 @@ def freeze
def call(request)
begin
return @delegate.call(request)
rescue => exception
rescue *APPLICATION_ERRORS => exception
request.exception = exception
send_notification exception, request

Expand Down
10 changes: 9 additions & 1 deletion test/utopia/exceptions/.handler/controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2015-2023, by Samuel Williams.
# Copyright, 2015-2026, by Samuel Williams.

prepend Actions

Expand All @@ -12,6 +12,14 @@ class TharSheBlows < StandardError
raise TharSheBlows.new("Arrrh!")
end

on "syntax-error" do
raise SyntaxError.new("Invalid application syntax!")
end

on "interrupt" do
raise Interrupt.new("Application interrupted!")
end

# The ExceptionHandler middleware will redirect here when an exception occurs. If this also fails, things get ugly.
on "exception" do |request|
if request.query_parameters["fatal"]
Expand Down
13 changes: 12 additions & 1 deletion test/utopia/exceptions/handler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2015-2025, by Samuel Williams.
# Copyright, 2015-2026, by Samuel Williams.

require "sus/fixtures/protocol/http/middleware_context"
require "utopia/application"
Expand Down Expand Up @@ -35,4 +35,15 @@
expect(last_response.status).to be == 500
expect(last_response.read).to be(:include?, "Error: Arrrh!")
end

it "handles application syntax errors" do
client.get "/syntax-error"

expect(last_response.status).to be == 500
expect(last_response.read).to be(:include?, "Invalid application syntax!")
end

it "does not handle process exceptions" do
expect{client.get "/interrupt"}.to raise_exception(Interrupt, message: be =~ /Application interrupted/)
end
end
15 changes: 14 additions & 1 deletion test/utopia/exceptions/mailer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2016-2025, by Samuel Williams.
# Copyright, 2016-2026, by Samuel Williams.

require "sus/fixtures/protocol/http/middleware_context"
require "utopia/application"
Expand Down Expand Up @@ -43,6 +43,19 @@ def before
expect(last_mail.to_s).to be(:include?, "TharSheBlows")
end

it "reports application syntax errors" do
expect{client.get "/syntax-error"}.to raise_exception(SyntaxError, message: be =~ /Invalid application syntax/)

last_mail = Mail::TestMailer.deliveries.last
expect(last_mail.to_s).to be(:include?, "SyntaxError")
end

it "does not report process exceptions" do
expect{client.get "/interrupt"}.to raise_exception(Interrupt, message: be =~ /Application interrupted/)

expect(Mail::TestMailer.deliveries).to be(:empty?)
end

it "extracts rewindable request bodies" do
request = Utopia::Request["POST", "/", {}, ["Hello", " World!"]]
request.body.read
Expand Down
Loading