1 package com.atlassian.marketplace.client;
2
3 import com.atlassian.marketplace.client.encoding.SchemaViolation;
4 import com.atlassian.marketplace.client.model.ErrorDetail;
5
6 import com.google.common.base.Joiner;
7 import com.google.common.collect.ImmutableList;
8
9 /**
10 * Base class for all exceptions thrown by {@link MarketplaceClient}.
11 */
12 @SuppressWarnings("serial")
13 public class MpacException extends Exception
14 {
15 public MpacException()
16 {
17 super();
18 }
19
20 public MpacException(String message)
21 {
22 super(message);
23 }
24
25 public MpacException(Throwable cause)
26 {
27 super(cause);
28 }
29
30 public MpacException(String message, Throwable cause)
31 {
32 super(message, cause);
33 }
34
35 /**
36 * Indicates that the client was unable to make an HTTP connection to the server, or that
37 * a request timed out before a response was received.
38 */
39 public static class ConnectionFailure extends MpacException
40 {
41 public ConnectionFailure(Throwable cause)
42 {
43 super(cause);
44 }
45 }
46
47 /**
48 * Indicates that the server returned an HTTP error status code.
49 */
50 public static class ServerError extends MpacException
51 {
52 private final int status;
53 private final ImmutableList<ErrorDetail> errorDetails;
54
55 public ServerError(int status)
56 {
57 super("error " + status);
58 this.status = status;
59 this.errorDetails = ImmutableList.of();
60 }
61
62 public ServerError(int status, String message)
63 {
64 super(message);
65 this.status = status;
66 this.errorDetails = ImmutableList.of();
67 }
68
69 public ServerError(int status, Iterable<ErrorDetail> errorDetails)
70 {
71 super(Joiner.on(", ").join(errorDetails));
72 this.status = status;
73 this.errorDetails = ImmutableList.copyOf(errorDetails);
74 }
75
76 /**
77 * The HTTP status code from the response.
78 */
79 public int getStatus()
80 {
81 return status;
82 }
83
84 /**
85 * Error details included in the response, if any.
86 */
87 public Iterable<ErrorDetail> getErrorDetails()
88 {
89 return errorDetails;
90 }
91 }
92
93 /**
94 * Indicates that the server returned a response that could not be parsed. This should not
95 * normally happen, and may indicate either a bug in the client library or a server-side bug.
96 */
97 public static class InvalidResponseError extends MpacException
98 {
99 private ImmutableList<SchemaViolation> schemaViolations;
100
101 public InvalidResponseError(Iterable<SchemaViolation> schemaViolations)
102 {
103 this.schemaViolations = ImmutableList.copyOf(schemaViolations);
104 }
105
106 public InvalidResponseError(String message)
107 {
108 super(message);
109 this.schemaViolations = ImmutableList.of();
110 }
111
112 public InvalidResponseError(String message, Throwable cause)
113 {
114 super(message, cause);
115 this.schemaViolations = ImmutableList.of();
116 }
117
118 public Iterable<SchemaViolation> getSchemaViolations()
119 {
120 return schemaViolations;
121 }
122
123 public String getMessage()
124 {
125 if (super.getMessage() == null)
126 {
127 return Joiner.on(", ").join(schemaViolations);
128 }
129 else
130 {
131 return super.getMessage();
132 }
133 }
134 }
135
136 /**
137 * Indicates that an {@code update} method was called for an object that did not come from
138 * the Marketplace server.
139 * <p>
140 * This could happen if, for example, you construct an
141 * {@link com.atlassian.marketplace.client.model.Addon} instance {@code a0}, pass it to
142 * {@link com.atlassian.marketplace.client.api.Addons#createAddon} to create the add-on,
143 * and then try to use {@code a0} again with {@link com.atlassian.marketplace.client.api.Addons#updateAddon}
144 * to update some property of the add-on; that will not work because the {@code a0} instance
145 * is still in a not-yet-created state and does not have a resource URI. The correct thing
146 * to do in that case would be to use the {@code Addon} instance that is <i>returned from</i>
147 * {@code createAddon}, which represents the persistent state of the resource after it has
148 * been created. (You could also query the add-on's current state again at any time with
149 * {@link com.atlassian.marketplace.client.api.Addons#getByKey}, and use the resulting
150 * {@code Addon} instance for your update request.)
151 *
152 * @since 2.0.0
153 */
154 public static class CannotUpdateNonServerSideEntity extends MpacException
155 {
156 public CannotUpdateNonServerSideEntity()
157 {
158 }
159 }
160 }