|
1 | 1 | // Copyright (c) Microsoft Corporation. All Rights reserved |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | | -pub enum ErrorKind { |
5 | | - AmqpReceiverAlreadyAttached, |
6 | | - TransportImplementationError { |
7 | | - source: Box<dyn std::error::Error + Send + Sync>, |
8 | | - }, |
| 4 | +pub use crate::sender::error::AmqpSenderError; |
| 5 | +use crate::{AmqpOrderedMap, AmqpSymbol, AmqpValue}; |
| 6 | + |
| 7 | +/// Type of AMQP error. |
| 8 | +pub enum AmqpErrorKind { |
| 9 | + /// Remote peer closed the link |
| 10 | + ClosedByRemote(Option<AmqpDescribedError>), |
| 11 | + |
| 12 | + /// Remote peer detached |
| 13 | + DetachedByRemote(Option<AmqpDescribedError>), |
| 14 | + |
| 15 | + /// The connection was dropped. |
| 16 | + ConnectionDropped(Box<dyn std::error::Error + Send + Sync>), |
| 17 | + |
| 18 | + /// Link State error. |
| 19 | + LinkStateError(Box<dyn std::error::Error + Send + Sync>), |
| 20 | + |
| 21 | + FramingError(Box<dyn std::error::Error + Send + Sync>), |
| 22 | + IdleTimeoutElapsed(Box<dyn std::error::Error + Send + Sync>), |
| 23 | + |
| 24 | + /// Transfer Limit Exceeded |
| 25 | + TransferLimitExceeded(Box<dyn std::error::Error + Send + Sync>), |
| 26 | + |
| 27 | + /// Management Status code |
| 28 | + ManagementStatusCode(azure_core::StatusCode, Option<String>), |
| 29 | + |
| 30 | + DetachError(Box<dyn std::error::Error + Send + Sync>), |
| 31 | + SenderError(AmqpSenderError), |
| 32 | + TransportImplementationError(Box<dyn std::error::Error + Send + Sync>), |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Clone)] |
| 36 | +pub struct AmqpDescribedError { |
| 37 | + condition: AmqpSymbol, |
| 38 | + description: Option<String>, |
| 39 | + info: AmqpOrderedMap<AmqpSymbol, AmqpValue>, |
| 40 | +} |
| 41 | + |
| 42 | +impl AmqpDescribedError { |
| 43 | + pub fn new( |
| 44 | + condition: AmqpSymbol, |
| 45 | + description: Option<String>, |
| 46 | + info: AmqpOrderedMap<AmqpSymbol, AmqpValue>, |
| 47 | + ) -> Self { |
| 48 | + Self { |
| 49 | + condition, |
| 50 | + description, |
| 51 | + info, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + pub fn condition(&self) -> &AmqpSymbol { |
| 56 | + &self.condition |
| 57 | + } |
| 58 | + pub fn description(&self) -> Option<&String> { |
| 59 | + self.description.as_ref() |
| 60 | + } |
| 61 | + pub fn info(&self) -> &AmqpOrderedMap<AmqpSymbol, AmqpValue> { |
| 62 | + &self.info |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// An AMQP error from the AMQP stack. |
| 67 | +pub struct AmqpError { |
| 68 | + /// Type of error. |
| 69 | + kind: AmqpErrorKind, |
9 | 70 | } |
10 | 71 |
|
11 | | -pub struct Error { |
12 | | - kind: ErrorKind, |
| 72 | +impl AmqpError { |
| 73 | + pub fn kind(&self) -> &AmqpErrorKind { |
| 74 | + &self.kind |
| 75 | + } |
13 | 76 | } |
14 | 77 |
|
15 | | -impl Error { |
16 | | - pub fn new(kind: ErrorKind) -> Self { |
| 78 | +impl From<AmqpErrorKind> for AmqpError { |
| 79 | + fn from(kind: AmqpErrorKind) -> Self { |
17 | 80 | Self { kind } |
18 | 81 | } |
19 | 82 | } |
20 | 83 |
|
21 | | -impl std::error::Error for Error { |
| 84 | +impl std::error::Error for AmqpError { |
22 | 85 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
23 | 86 | match &self.kind { |
24 | | - ErrorKind::TransportImplementationError { source } => source.source(), |
25 | | - _ => None, |
| 87 | + AmqpErrorKind::TransportImplementationError(s) |
| 88 | + | AmqpErrorKind::DetachError(s) |
| 89 | + | AmqpErrorKind::LinkStateError(s) |
| 90 | + | AmqpErrorKind::ConnectionDropped(s) => Some(s.as_ref()), |
| 91 | + AmqpErrorKind::SenderError(e) => e.source(), |
| 92 | + AmqpErrorKind::ManagementStatusCode(_, _) => None, |
| 93 | + AmqpErrorKind::ClosedByRemote(_) | AmqpErrorKind::DetachedByRemote(_) => None, |
| 94 | + AmqpErrorKind::TransferLimitExceeded(e) => Some(e.as_ref()), |
| 95 | + AmqpErrorKind::FramingError(e) => Some(e.as_ref()), |
| 96 | + AmqpErrorKind::IdleTimeoutElapsed(e) => Some(e.as_ref()), |
26 | 97 | } |
27 | 98 | } |
28 | 99 | } |
29 | 100 |
|
30 | | -impl std::fmt::Display for Error { |
| 101 | +impl std::fmt::Display for AmqpError { |
31 | 102 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
32 | 103 | match &self.kind { |
33 | | - ErrorKind::AmqpReceiverAlreadyAttached => { |
34 | | - f.write_str("AMQP Receiver is already attached") |
| 104 | + AmqpErrorKind::ManagementStatusCode(status_code, d) => { |
| 105 | + if let Some(d) = d { |
| 106 | + write!( |
| 107 | + f, |
| 108 | + "Management API returned status code: {} ({})", |
| 109 | + status_code, d |
| 110 | + ) |
| 111 | + } else { |
| 112 | + write!(f, "Management API returned status code: {}", status_code,) |
| 113 | + } |
| 114 | + } |
| 115 | + AmqpErrorKind::DetachedByRemote(err) => { |
| 116 | + write!(f, "Remote detached with error: {:?}", err) |
| 117 | + } |
| 118 | + AmqpErrorKind::ClosedByRemote(err) => { |
| 119 | + write!(f, "Remote closed with error: {:?}", err) |
| 120 | + } |
| 121 | + AmqpErrorKind::DetachError(err) => { |
| 122 | + write!(f, "AMQP Detach Error: {} ", err) |
| 123 | + } |
| 124 | + AmqpErrorKind::TransportImplementationError(s) => { |
| 125 | + write!(f, "Transport Implementation Error: {}", s) |
| 126 | + } |
| 127 | + AmqpErrorKind::ConnectionDropped(s) => { |
| 128 | + write!(f, "Connection dropped: {}", s) |
| 129 | + } |
| 130 | + AmqpErrorKind::FramingError(s) => { |
| 131 | + write!(f, "Connection Framing error: {}", s) |
| 132 | + } |
| 133 | + AmqpErrorKind::IdleTimeoutElapsed(s) => { |
| 134 | + write!(f, "Connection Idle Timeout elapsed: {}", s) |
| 135 | + } |
| 136 | + AmqpErrorKind::SenderError(err) => { |
| 137 | + write!(f, "AMQP Sender Error: {} ", err) |
| 138 | + } |
| 139 | + AmqpErrorKind::LinkStateError(err) => { |
| 140 | + write!(f, "AMQP Link State Error: {} ", err) |
35 | 141 | } |
36 | | - ErrorKind::TransportImplementationError { source } => { |
37 | | - write!(f, "Transport Implementation Error: {:?}", source) |
| 142 | + AmqpErrorKind::TransferLimitExceeded(e) => { |
| 143 | + write!(f, "AMQP Transfer Limit Exceeded: {e}") |
38 | 144 | } |
39 | 145 | } |
40 | 146 | } |
41 | 147 | } |
42 | 148 |
|
43 | | -impl std::fmt::Debug for Error { |
| 149 | +impl std::fmt::Debug for AmqpError { |
44 | 150 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
45 | | - write!(f, "AMQP Transport Error: {}", self) |
| 151 | + write!(f, "AmqpError: {}", self)?; |
| 152 | + Ok(()) |
46 | 153 | } |
47 | 154 | } |
48 | 155 |
|
49 | | -impl From<Error> for azure_core::Error { |
50 | | - fn from(e: Error) -> Self { |
51 | | - Self::new(azure_core::error::ErrorKind::Other, Box::new(e)) |
| 156 | +impl From<AmqpError> for azure_core::Error { |
| 157 | + fn from(e: AmqpError) -> Self { |
| 158 | + Self::new(azure_core::error::ErrorKind::Amqp, Box::new(e)) |
52 | 159 | } |
53 | 160 | } |
54 | 161 |
|
55 | | -impl From<ErrorKind> for azure_core::Error { |
56 | | - fn from(e: ErrorKind) -> Self { |
57 | | - Self::new( |
58 | | - azure_core::error::ErrorKind::Other, |
59 | | - Box::new(Error { kind: e }), |
60 | | - ) |
| 162 | +impl From<AmqpErrorKind> for azure_core::Error { |
| 163 | + fn from(e: AmqpErrorKind) -> Self { |
| 164 | + AmqpError::from(e).into() |
61 | 165 | } |
62 | 166 | } |
0 commit comments