Exception: Treaty::Exceptions::Unexpected
- Defined in:
- lib/treaty/exceptions/unexpected.rb
Overview
Raised for unexpected errors that don't fit other exception types
Purpose
Serves as a catch-all for unexpected errors that occur during Treaty processing but don't fall into specific exception categories. Helps maintain consistent error handling across the gem.
Usage
Used for rare or unexpected edge cases:
def process_data(data)
# ... processing logic
rescue => e
# Log the unexpected error for investigation
Rails.logger.error("Unexpected Treaty error: #{e.}")
raise Treaty::Exceptions::Unexpected,
"An unexpected error occurred during processing: #{e.}"
end
Integration
Can be rescued by application controllers as a safety net:
rescue_from Treaty::Exceptions::Unexpected, with: :render_unexpected_error
def render_unexpected_error(exception)
# Log for investigation
Rails.logger.error("Unexpected Treaty exception: #{exception.}")
Rails.logger.error(exception.backtrace.join("\n"))
render json: { error: "An unexpected error occurred" },
status: :internal_server_error
end
When to Use
- Edge cases not covered by specific exceptions
- Defensive programming for "should never happen" scenarios
- Wrapping third-party library errors
- Temporary exception during development before creating specific type
When Not to Use
If the error fits an existing exception type, use that instead:
- Validation errors → Treaty::Exceptions::Validation
- Execution errors → Treaty::Exceptions::Execution
- Configuration errors → specific exception type
Investigation
When this exception occurs in production:
- Review logs for full context
- Analyze stack trace
- Consider creating a specific exception type if pattern emerges
- Add tests to prevent recurrence