1 package com.atlassian.vcache;
2
3 import com.atlassian.annotations.PublicApi;
4
5 import static java.util.Objects.requireNonNull;
6
7 /**
8 * Represents a failure occurred with an {@link ExternalCache}.
9 *
10 * @since 1.0
11 */
12 @PublicApi
13 public class ExternalCacheException extends VCacheException {
14 /**
15 * The reasons for failure. Note that even though a failure may be reported, the operation may have still been
16 * performed on the external cache.
17 */
18 public enum Reason {
19 /**
20 * Operation timed out.
21 */
22 TIMEOUT,
23 /**
24 * Operation failed due to a network error.
25 */
26 NETWORK_FAILURE,
27 /**
28 * Operation failed due a {@link Marshaller} failure.
29 */
30 MARSHALLER_FAILURE,
31 /**
32 * Operation failed due to the supplied Function not returning the expected number of values.
33 */
34 FUNCTION_INCORRECT_RESULT,
35 /**
36 * Creation of cache failed.
37 */
38 CREATION_FAILURE,
39 /**
40 * Operation failed due to a transaction sync failure.
41 */
42 TRANSACTION_FAILURE,
43 /**
44 * Operation failed due to an unclassified failure.
45 */
46 UNCLASSIFIED_FAILURE
47 }
48
49 private final Reason reason;
50
51 /**
52 * Creates an instance for the specified reason.
53 *
54 * @param reason the reason for the failure
55 */
56 public ExternalCacheException(Reason reason) {
57 super("Failed due to " + reason.name());
58 this.reason = requireNonNull(reason);
59 }
60
61 /**
62 * Creates an instance for the specified reason and cause.
63 *
64 * @param reason the reason for the failure
65 * @param cause the cause of the failure
66 */
67 public ExternalCacheException(Reason reason, Throwable cause) {
68 super("Failed due to " + reason.name(), cause);
69 this.reason = requireNonNull(reason);
70 }
71
72 /**
73 * Returns the reason for failure.
74 *
75 * @return the reason for failure.
76 */
77 public Reason getReason() {
78 return reason;
79 }
80 }