How to rescue all result unhandled exceptions?
Use RescuesResultUnhandledExceptions plugin
danger
RescuesResultUnhandledExceptions plugin intentionally rescues only StandardErrors, not Exceptions.
info
RescuesResultUnhandledExceptions plugin is especially useful for the Fault Tolerant production environments.
caution
RescuesResultUnhandledExceptions plugin is NOT included in the Standard config by default.
It is up to the end-users to decide whether it is needed for thier applications or not.
Option 1: Modify config to rescue results in all services
ConvenientService::Dependencies.require_rescues_result_unhandled_exceptions
module ApplicationService
module Config
include ConvenientService::Concern
included do |service_class|
service_class.class_exec do
include ConvenientService::Standard::Config
middlewares :result, scope: :class do
use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware
end
end
end
end
end
class Service
include ApplicationService::Config
def result
raise StandardError, "exception message"
end
end
Option 2: Modify service to rescue results in a single service
ConvenientService::Dependencies.require_rescues_result_unhandled_exceptions
module ApplicationService
module Config
include ConvenientService::Concern
included do |service_class|
service_class.class_exec do
include ConvenientService::Standard::Config
end
end
end
end
class Service
include ApplicationService::Config
middlewares :result, scope: :class do
use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware
end
def result
raise StandardError, "exception message"
end
end
Result with exception becomes an error
info
result = Service.result
result.success?
puts result.message
Result has access to the original exception object
info
result.data[:exception]
is Ruby's StandardError object that inherits from Exception.
result.data[:exception]
result.data[:exception].class
result.data[:exception].message
result.data[:exception].backtrace.to_a.take(10)
result.data[:exception].cause