| 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 |
| 20 |
|
| 21 |
# An AMQP error condition. |
| 22 |
# |
| 23 |
# An error sent across an AMQP connection has a name, description and optional extra info. |
| 24 |
# The {Connectin}, {Session} and {Link} endpoint classes all have a #condition method to |
| 25 |
# check for errors. |
| 26 |
# |
| 27 |
# {Condition} can also be raised as an exception. |
| 28 |
# |
| 29 |
class Condition < ProtonError |
| 30 |
|
| 31 |
attr_reader :name, :description, :info |
| 32 |
|
| 33 |
def initialize(name, description = nil, info = nil) |
| 34 |
@name = name |
| 35 |
@description = description |
| 36 |
@info = info |
| 37 |
super(to_s) |
| 38 |
end |
| 39 |
|
| 40 |
def to_s() "#{@name}: #{@description}"; end |
| 41 |
|
| 42 |
def inspect() "#{self.class.name}(#{@name.inspect}, #{@description.inspect}, #{@info.inspect})"; end |
| 43 |
|
| 44 |
def ==(other) |
| 45 |
((other.is_a? Condition) && |
| 46 |
(other.name == self.name) && |
| 47 |
(other.description == self.description) && |
| 48 |
(other.info == self.info)) |
| 49 |
end |
| 50 |
|
| 51 |
# Convert an object to a condition. |
| 52 |
# @param obj the object to turn into a condition |
| 53 |
# @param default_name name to use if obj does not imply a name |
| 54 |
# @return [Condition] Conversion depends on the type of obj |
| 55 |
# - Condition: return obj |
| 56 |
# - Exception: return Condition(obj.class.name, obj.to_s) |
| 57 |
# - String-like: return String.try_convert(obj) |
| 58 |
# - nil: return nil |
| 59 |
# @raise ::ArgumentError if obj is not convertible to {Condition} |
| 60 |
def self.convert(obj, default_name="error") |
| 61 |
case obj |
| 62 |
when nil then nil |
| 63 |
when Condition then obj |
| 64 |
when Exception then Condition.new(obj.class.name, obj.to_s) |
| 65 |
when SWIG::TYPE_p_pn_condition_t |
| 66 |
if Cproton.pn_condition_is_set(obj) |
| 67 |
Condition.new(Cproton.pn_condition_get_name(obj), |
| 68 |
Cproton.pn_condition_get_description(obj), |
| 69 |
Codec::Data.to_object(Cproton.pn_condition_info(obj))) |
| 70 |
end |
| 71 |
else |
| 72 |
raise ::ArgumentError, "can't convert #{obj.class.name} to #{self.class.name}" unless obj.respond_to? :to_str |
| 73 |
Condition.new(default_name, obj.to_str) |
| 74 |
end |
| 75 |
end |
| 76 |
|
| 77 |
private |
| 78 |
|
| 79 |
def self.assign(impl, cond) |
| 80 |
Cproton.pn_condition_clear(impl) |
| 81 |
if cond |
| 82 |
cond = self.convert(cond) |
| 83 |
Cproton.pn_condition_set_name(impl, cond.name) if cond.name |
| 84 |
Cproton.pn_condition_set_description(impl, cond.description) if cond.description |
| 85 |
Codec::Data.from_object(Cproton.pn_condition_info(impl), cond.info) if cond.info |
| 86 |
end |
| 87 |
end |
| 88 |
end |
| 89 |
end |