class ObjectFramework::ErrorRegistry
Maps exception classes to safe, transport-independent public descriptions. Configure it before Application#start finalizes registrations.
Constants
- CATEGORIES
- DEFAULTS
Public Class Methods
Source
# File lib/object_framework/errors.rb, line 35 def initialize @registrations = {} DEFAULTS.each { |type, (category, code, title)| register(type, category:, code:, title:) } end
Public Instance Methods
Source
# File lib/object_framework/errors.rb, line 44 def register(type, category:, code:, title:, details: ->(_) { {} }) raise ConfigurationError, "Error registry is finalized" if @registrations.frozen? raise ConfigurationError, "Expected an exception class" unless type.is_a?(Class) && type <= StandardError raise ConfigurationError, "Duplicate error registration: #{type}" if @registrations.key?(type) raise ConfigurationError, "Unknown error category: #{category}" unless CATEGORIES.include?(category) raise ConfigurationError, "A stable code and title are required" if code.to_s.empty? || title.to_s.empty? @registrations[type] = [category, code.to_s.dup.freeze, title.to_s.dup.freeze, details].freeze self end
Registers a StandardError subclass with a stable code and title. category must belong to CATEGORIES. The optional details callable must return only explicitly safe public values. Duplicate types are rejected. Returns self.
Source
# File lib/object_framework/errors.rb, line 63 def resolve(exception) type = exception.class.ancestors.find { |ancestor| @registrations.key?(ancestor) } return ErrorDescription.new(category: :internal, code: "internal_error", title: "Internal server error", details: {}) unless type category, code, title, details = @registrations.fetch(type) ErrorDescription.new(category:, code:, title:, details: Values.detach(details.call(exception))) end
Returns an ErrorDescription for the closest registered ancestor. Unregistered exceptions receive a generic internal-error description.
Source
# File lib/object_framework/errors.rb, line 54 def validate! codes = @registrations.values.map { |registration| registration[1] } raise ConfigurationError, "Ambiguous public error codes" unless codes.uniq.size == codes.size @registrations.freeze self end