-
Notifications
You must be signed in to change notification settings - Fork 215
/
ConnectionFailure.java
147 lines (133 loc) · 5.51 KB
/
ConnectionFailure.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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.internal;
import java.io.IOException;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import akka.actor.ActorRef;
import akka.actor.Status;
/**
* Messaging internal error message for when a Failure was detected on a Connection.
*/
public interface ConnectionFailure extends WithOrigin {
/**
* Creates a new ConnectionFailure of not yet known differentiation between {@link ConnectivityStatus#FAILED} and
* {@link ConnectivityStatus#MISCONFIGURED}.
*
* @param origin the origin ActorRef
* @param cause the cause of the Failure
* @param description an optional description
* @return the created ConnectionFailure
*/
static ConnectionFailure of(@Nullable final ActorRef origin, @Nullable final Throwable cause,
@Nullable final String description) {
return new ImmutableConnectionFailure(origin, getRealCause(cause), description, null);
}
/**
* Creates a new ConnectionFailure which was most likely cause by an internal problem.
*
* @param origin the origin ActorRef
* @param cause the cause of the Failure
* @param description an optional description
* @return the created ConnectionFailure
*/
static ConnectionFailure internal(@Nullable final ActorRef origin, @Nullable final Throwable cause,
@Nullable final String description) {
return new ImmutableConnectionFailure(origin, getRealCause(cause), description, ConnectivityStatus.FAILED);
}
/**
* Creates a new ConnectionFailure which was most likely caused by an issue outside of the service.
* This could be for example a misconfiguration of the connection by a user or a temporary downtime of the broker
* or anything else that is not in our responsibility.
*
* @param origin the origin ActorRef
* @param cause the cause of the Failure
* @param description an optional description
* @return the created ConnectionFailure
*/
static ConnectionFailure userRelated(@Nullable final ActorRef origin,
@Nullable final Throwable cause,
@Nullable final String description) {
return new ImmutableConnectionFailure(origin, getRealCause(cause), description,
ConnectivityStatus.MISCONFIGURED);
}
/**
* Determines a nicely formatted failure description string based on the based in optional {@code cause}, an
* optional {@code description} and the {@code time}.
*
* @param time the optional time to include in the description.
* @param cause the optional cause to extract {@code message} and (if it was a {@code DittoRuntimeException}
* {@code description} from.
* @param description an optional additional description to include in the created failure description.
* @return the created nicely formatted failure description.
*/
static String determineFailureDescription(@Nullable final Instant time,
@Nullable final Throwable cause,
@Nullable final String description) {
String responseStr = "";
if (cause != null) {
if (description != null) {
responseStr = description + " - cause ";
}
responseStr += String.format("<%s>: %s", cause.getClass().getSimpleName(), cause.getMessage());
if (cause instanceof DittoRuntimeException dittoRuntimeException) {
if (!responseStr.endsWith(".")) {
responseStr += ".";
}
responseStr += dittoRuntimeException.getDescription().map(d -> " " + d).orElse("");
}
} else {
responseStr = Objects.requireNonNullElse(description, "unknown failure");
}
if (!responseStr.endsWith(".")) {
responseStr += ".";
}
if (null != time) {
responseStr += " At " + time;
}
return responseStr;
}
/**
* @return the description of the failure.
*/
String getFailureDescription();
/**
* @return the optionally already certainly known connectivity status.
*/
Optional<ConnectivityStatus> getStatus();
/**
* @return the Failure containing the cause.
*/
Status.Failure getFailure();
@Nullable
private static Throwable getRealCause(@Nullable final Throwable cause) {
final Throwable realCause;
if (cause instanceof CompletionException) {
realCause = cause.getCause();
} else if (cause instanceof ExecutionException) {
realCause = cause.getCause();
} else if (cause != null && IOException.class.equals(cause.getClass()) && cause.getMessage() == null &&
cause.getCause() != null) {
realCause = cause.getCause();
} else {
realCause = cause;
}
return realCause;
}
}