1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
# frozen_string_literal: true
# @private
# Base class for Contract errors
#
# If default failure callback is used it stores failure data
class ContractBaseError < ArgumentError
attr_reader :data
def initialize(message, data)
super(message)
@data = data
end
# Used to convert to simple ContractError from other contract errors
def to_contract_error
self
end
end
# Default contract error
#
# If default failure callback is used, users normally see only these contract errors
class ContractError < ContractBaseError
end
class ParamContractError < ContractError
end
class ReturnContractError < ContractError
end
# @private
# Special contract error used internally to detect pattern failure during pattern matching
class PatternMatchingError < ContractBaseError
# Used to convert to ContractError from PatternMatchingError
def to_contract_error
ContractError.new(to_s, data)
end
end
# Base invariant violation error
class InvariantError < StandardError
def to_contract_error
self
end
end
module Contracts
# Error issued when user haven't included Contracts in original class but used Contract definition in singleton class
#
# Provides useful description for user of the gem and an example of correct usage.
class ContractsNotIncluded < TypeError
DEFAULT_MESSAGE = %{In order to use contracts in singleton class, please include Contracts module in original class
Example:
```ruby
class Example
include Contracts # this line is required
class << self
# you can use `Contract` definition here now
end
end
```}
attr_reader :message
alias_method :to_s, :message
def initialize(message = DEFAULT_MESSAGE)
super
@message = message
end
end
end
|