lib/contracts/method_handler.rb in contracts-0.16.0 vs lib/contracts/method_handler.rb in contracts-0.16.1

- old
+ new

@@ -123,42 +123,34 @@ # through each method and call it with the arguments. # If we get a failure_exception, we move to the next # function. Otherwise we return the result. # If we run out of functions, we raise the last error, but # convert it to_contract_error. - success = false - i = 0 - result = nil + expected_error = decorated_methods[0].failure_exception + last_error = nil - until success - decorated_method = decorated_methods[i] - i += 1 - begin - success = true - result = decorated_method.call_with(self, *args, &blk) - rescue expected_error => error - success = false - unless decorated_methods[i] - begin - ::Contract.failure_callback(error.data, false) - rescue expected_error => final_error - raise final_error.to_contract_error - end - end - end + decorated_methods.each do |decorated_method| + result = decorated_method.call_with_inner(true, self, *args, &blk) + return result unless result.is_a?(ParamContractError) + last_error = result end - # Return the result of successfully called method - result + begin + if ::Contract.failure_callback(last_error.data, false) + decorated_methods.last.call_with_inner(false, self, *args, &blk) + end + rescue expected_error => final_error + raise final_error.to_contract_error + end end end def validate_decorators! return if decorators.size == 1 fail %{ -Oops, it looks like method '#{name}' has multiple contracts: +Oops, it looks like method '#{method_name}' has multiple contracts: #{decorators.map { |x| x[1][0].inspect }.join("\n")} Did you accidentally put more than one contract on a single function, like so? Contract String => String