| 1 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 2 |
# or more contributor license agreements. See the NOTICE file |
| 3 |
# distributed with this work for additional information |
| 4 |
# regarding copyright ownership. The ASF licenses this file |
| 5 |
# to you under the Apache License, Version 2.0 (the |
| 6 |
# "License"); you may not use this file except in compliance |
| 7 |
# with the License. You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, |
| 12 |
# software distributed under the License is distributed on an |
| 13 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 |
# KIND, either express or implied. See the License for the |
| 15 |
# specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
|
| 19 |
module Qpid::Proton::Util |
| 20 |
|
| 21 |
# Provides mixin functionality for dealing with exception conditions. |
| 22 |
# |
| 23 |
# @private |
| 24 |
module ErrorHandler |
| 25 |
|
| 26 |
def self.included(other) other.extend(self); end |
| 27 |
|
| 28 |
def can_raise_error(method_names, options = {}) |
| 29 |
error_class = options[:error_class] |
| 30 |
below = options[:below] || 0 |
| 31 |
# coerce the names to be an array |
| 32 |
Array(method_names).each do |method_name| |
| 33 |
raise "missing method #{method_name.inspect}" unless method_defined?(method_name) || private_method_defined?(method_name) |
| 34 |
create_exception_handler_wrapper(method_name, error_class, below) |
| 35 |
end |
| 36 |
end |
| 37 |
|
| 38 |
def create_exception_handler_wrapper(method_name, error_class = nil, below = 0) |
| 39 |
original_method_name = method_name.to_s |
| 40 |
wrapped_method_name = "_excwrap_#{original_method_name}" |
| 41 |
alias_method wrapped_method_name, original_method_name |
| 42 |
define_method original_method_name do |*args, &block| |
| 43 |
# need to get a reference to the method object itself since |
| 44 |
# calls to Class.send interfere with Messenger.send |
| 45 |
method = self.method(wrapped_method_name.to_sym) |
| 46 |
rc = method.call(*args, &block) |
| 47 |
check_for_error(rc, error_class) if rc < below |
| 48 |
return rc |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
# Raises an Proton-specific error if a return code is non-zero. |
| 53 |
# |
| 54 |
# Expects the class to provide an +error+ method. |
| 55 |
def check_for_error(code, error_class = nil) |
| 56 |
|
| 57 |
raise ::ArgumentError.new("Invalid error code: #{code}") if code.nil? |
| 58 |
|
| 59 |
return code if code > 0 |
| 60 |
|
| 61 |
case(code) |
| 62 |
|
| 63 |
when Qpid::Proton::Error::NONE |
| 64 |
return |
| 65 |
|
| 66 |
when Qpid::Proton::Error::EOS |
| 67 |
raise Qpid::Proton::EOSError.new(self.error) |
| 68 |
|
| 69 |
when Qpid::Proton::Error::ERROR |
| 70 |
raise Qpid::Proton::ProtonError.new(self.error) |
| 71 |
|
| 72 |
when Qpid::Proton::Error::OVERFLOW |
| 73 |
raise Qpid::Proton::OverflowError.new(self.error) |
| 74 |
|
| 75 |
when Qpid::Proton::Error::UNDERFLOW |
| 76 |
raise Qpid::Proton::UnderflowError.new(self.error) |
| 77 |
|
| 78 |
when Qpid::Proton::Error::ARGUMENT |
| 79 |
raise Qpid::Proton::ArgumentError.new(self.error) |
| 80 |
|
| 81 |
when Qpid::Proton::Error::STATE |
| 82 |
raise Qpid::Proton::StateError.new(self.error) |
| 83 |
|
| 84 |
when Qpid::Proton::Error::TIMEOUT |
| 85 |
raise Qpid::Proton::TimeoutError.new(self.error) |
| 86 |
|
| 87 |
when Qpid::Proton::Error::INPROGRESS |
| 88 |
return |
| 89 |
|
| 90 |
when Qpid::Proton::Error::INTERRUPTED |
| 91 |
raise Qpid::Proton::InterruptedError.new(self.error) |
| 92 |
|
| 93 |
when Qpid::Proton::Error::INPROGRESS |
| 94 |
raise Qpid::Proton::InProgressError.new(self.error) |
| 95 |
|
| 96 |
else |
| 97 |
|
| 98 |
raise ::ArgumentError.new("Unknown error code: #{code}") |
| 99 |
|
| 100 |
end |
| 101 |
|
| 102 |
end |
| 103 |
|
| 104 |
end |
| 105 |
|
| 106 |
end |