/
AmqpException.java
161 lines (143 loc) · 6.21 KB
/
AmqpException.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.core.amqp.exception;
import com.azure.core.exception.AzureException;
import com.azure.core.util.CoreUtils;
import java.util.Locale;
/**
* General exception for AMQP related failures.
*
* @see AmqpErrorCondition
* @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-transport-v1.0-os.html#type-error">Amqp
* Error</a>
* @see <a href="https://docs.microsoft.com/azure/event-hubs/event-hubs-messaging-exceptions">Azure Messaging
* Exceptions</a>
*/
public class AmqpException extends AzureException {
private static final long serialVersionUID = -3654294093967132325L;
/**
* Context that caused this AMQP error.
*/
private final AmqpErrorContext errorContext;
/**
* Whether this error is transient and can be retried.
*/
private final boolean isTransient;
/**
* Symbolic value indicating the error condition.
*/
private final AmqpErrorCondition errorCondition;
/**
* Initializes a new instance of the AmqpException class.
*
* @param isTransient A boolean indicating if the exception is a transient error or not. If true, then the
* request can be retried; otherwise not.
* @param message Text containing any supplementary details of the exception.
* @param errorContext The context that caused this AMQP error.
*/
public AmqpException(boolean isTransient, String message, AmqpErrorContext errorContext) {
this(isTransient, null, message, errorContext);
}
/**
* Initializes a new instance of the AmqpException class.
*
* @param isTransient A boolean indicating if the exception is a transient error or not. If true, then the
* request can be retried; otherwise not.
* @param errorCondition The symbolic value indicating the error condition.
* @param message Text containing any supplementary details not indicated by the condition field. This text can
* be logged as an aid to resolving issues.
* @param errorContext The context that caused this AMQP error.
*/
public AmqpException(boolean isTransient, AmqpErrorCondition errorCondition, String message,
AmqpErrorContext errorContext) {
super(message);
this.errorCondition = errorCondition;
this.isTransient = isTransient;
this.errorContext = errorContext;
}
/**
* Initializes a new instance of the AmqpException class.
*
* @param isTransient A boolean indicating if the exception is a transient error or not. If true, then the
* request can be retried; otherwise not.
* @param errorCondition The symbolic value indicating the error condition.
* @param message Text containing any supplementary details not indicated by the condition field. This text can
* be logged as an aid to resolving issues.
* @param cause The Throwable which caused the creation of this AmqpException.
* @param errorContext The context that caused this AMQP error.
*/
public AmqpException(boolean isTransient, AmqpErrorCondition errorCondition, String message, Throwable cause,
AmqpErrorContext errorContext) {
super(message, cause);
this.errorCondition = errorCondition;
this.isTransient = isTransient;
this.errorContext = errorContext;
}
/**
* Initializes a new instance of the AmqpException class.
*
* @param isTransient A boolean indicating if the exception is a transient error or not. If true, then the
* request can be retried; otherwise not.
* @param errorCondition The symbolic value indicating the error condition.
* @param cause The Throwable which caused the creation of this AmqpException.
* @param errorContext The context that caused this AMQP error.
*/
public AmqpException(boolean isTransient, AmqpErrorCondition errorCondition, Throwable cause,
AmqpErrorContext errorContext) {
super(cause.getMessage(), cause);
this.errorCondition = errorCondition;
this.isTransient = isTransient;
this.errorContext = errorContext;
}
/**
* Initializes a new instance of the AmqpException class.
*
* @param isTransient A boolean indicating if the exception is a transient error or not. If true, then the
* request can be retried; otherwise not.
* @param message Text containing any supplementary details not indicated by the condition field. This text can
* be logged as an aid to resolving issues.
* @param cause The Throwable which caused the creation of this AmqpException.
* @param errorContext The context that caused this AMQP error.
*/
public AmqpException(boolean isTransient, String message, Throwable cause, AmqpErrorContext errorContext) {
super(message, cause);
this.errorCondition = null;
this.isTransient = isTransient;
this.errorContext = errorContext;
}
@Override
public String getMessage() {
final String baseMessage = super.getMessage();
if (this.errorContext == null) {
return super.getMessage();
}
return !CoreUtils.isNullOrEmpty(baseMessage)
? String.format(Locale.US, "%s, %s[%s]", baseMessage, "errorContext", errorContext.toString())
: String.format(Locale.US, "%s[%s]", "errorContext", errorContext.toString());
}
/**
* A boolean indicating if the exception is a transient error or not.
*
* @return returns true when user can retry the operation that generated the exception without additional
* intervention.
*/
public boolean isTransient() {
return this.isTransient;
}
/**
* Gets the {@link AmqpErrorCondition} for this exception.
*
* @return The {@link AmqpErrorCondition} for this exception, or {@code null} if nothing was set.
*/
public AmqpErrorCondition getErrorCondition() {
return this.errorCondition;
}
/**
* Gets the context for this exception.
*
* @return The context for this exception.
*/
public AmqpErrorContext getContext() {
return this.errorContext;
}
}