Skip to content

Commit b7fcbf4

Browse files
authoredMar 3, 2017
Merge pull request #56 from SparkPost/feature/better_exceptions_issue53
Added POJO object to exceptions thrown from transport to address issu…
2 parents 1bafc72 + 8088e1b commit b7fcbf4

12 files changed

+225
-22
lines changed
 

‎apps/sparkpost-samples-app/src/main/java/com/sparkpost/error/samples/BadApiKeyErrorSample.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.apache.log4j.Logger;
88

99
import com.sparkpost.Client;
10-
import com.sparkpost.exception.SparkPostAuthorizationFailedException;
10+
import com.sparkpost.exception.SparkPostAccessForbiddenException;
1111
import com.sparkpost.exception.SparkPostException;
1212
import com.sparkpost.resources.ResourceSendingDomains;
1313
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
@@ -36,9 +36,9 @@ private void runApp() throws SparkPostException, IOException {
3636
try {
3737
ResourceSendingDomains.list(connection);
3838

39-
throw new IllegalStateException("Error: Expected SparkPostAuthorizationFailedException");
40-
} catch (SparkPostAuthorizationFailedException e) {
41-
System.out.println("GOOD: Sucecssfuly got a SparkPostAuthorizationFailedException");
39+
throw new IllegalStateException("Error: Expected SparkPostAccessForbiddenException");
40+
} catch (SparkPostAccessForbiddenException e) {
41+
System.out.println("GOOD: Sucecssfuly got a SparkPostAccessForbiddenException");
4242
}
4343

4444
}

‎apps/sparkpost-samples-app/src/main/java/com/sparkpost/error/samples/SendEmailErrorSample.java

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.sparkpost.model.RecipientAttributes;
1818
import com.sparkpost.model.TemplateContentAttributes;
1919
import com.sparkpost.model.TransmissionWithRecipientArray;
20+
import com.sparkpost.model.responses.ServerErrorResponse;
2021
import com.sparkpost.resources.ResourceTransmissions;
2122
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
2223
import com.sparkpost.transport.RestConnection;
@@ -88,6 +89,11 @@ private void sendEmail(String from, String[] recipients) throws SparkPostExcepti
8889
System.out.println("GOOD: we got the expected SparkPostErrorServerResponseException");
8990
System.out.println(e.getMessage());
9091

92+
if (e.getServerErrorResponses() != null) {
93+
for (ServerErrorResponse error : e.getServerErrorResponses().getErrors()) {
94+
System.out.println("ERROR: " + error);
95+
}
96+
}
9197
}
9298

9399
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
package com.sparkpost.error.samples;
3+
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.apache.log4j.Level;
11+
import org.apache.log4j.Logger;
12+
13+
import com.sparkpost.Client;
14+
import com.sparkpost.exception.SparkPostErrorServerResponseException;
15+
import com.sparkpost.exception.SparkPostException;
16+
import com.sparkpost.model.AddressAttributes;
17+
import com.sparkpost.model.RecipientAttributes;
18+
import com.sparkpost.model.TemplateContentAttributes;
19+
import com.sparkpost.model.TransmissionWithRecipientArray;
20+
import com.sparkpost.model.responses.ServerErrorResponse;
21+
import com.sparkpost.resources.ResourceTransmissions;
22+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
23+
import com.sparkpost.transport.RestConnection;
24+
25+
public class SendInvalidFromAddress extends SparkPostBaseApp {
26+
27+
static final Logger logger = Logger.getLogger(SendInvalidFromAddress.class);
28+
29+
private Client client;
30+
31+
public static void main(String[] args) throws SparkPostException, IOException {
32+
Logger.getRootLogger().setLevel(Level.DEBUG);
33+
34+
SendInvalidFromAddress sample = new SendInvalidFromAddress();
35+
sample.runApp();
36+
}
37+
38+
private void runApp() throws SparkPostException, IOException {
39+
this.client = this.newConfiguredClient();
40+
41+
// Loads an email to send from the file system
42+
String fromAddress = "invalid@sparkpost.com";
43+
String[] recipients = getTestRecipients();
44+
45+
sendEmail(fromAddress, recipients);
46+
47+
}
48+
49+
private void sendEmail(String from, String[] recipients) throws SparkPostException {
50+
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
51+
52+
// Populate Recipients
53+
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
54+
for (String recipient : recipients) {
55+
RecipientAttributes recipientAttribs = new RecipientAttributes();
56+
recipientAttribs.setAddress(new AddressAttributes(recipient));
57+
recipientArray.add(recipientAttribs);
58+
}
59+
transmission.setRecipientArray(recipientArray);
60+
61+
// Populate Substitution Data
62+
Map<String, Object> substitutionData = new HashMap<String, Object>();
63+
substitutionData.put("yourContent", "You can add substitution data too.");
64+
transmission.setSubstitutionData(substitutionData);
65+
66+
// Populate Email Body
67+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
68+
contentAttributes.setFrom(new AddressAttributes(from));
69+
contentAttributes.setSubject("☰ Your subject content here. {{yourContent}}");
70+
contentAttributes.setText("Your Text content here. {{yourContent}}");
71+
contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{yourContent}}</p>");
72+
transmission.setContentAttributes(contentAttributes);
73+
74+
transmission.setContentAttributes(contentAttributes);
75+
76+
// Send the Email
77+
try {
78+
RestConnection connection = new RestConnection(this.client, getEndPoint());
79+
ResourceTransmissions.create(connection, 0, transmission);
80+
81+
throw new IllegalStateException("Error: Expected Exception for invalid to address");
82+
} catch (SparkPostErrorServerResponseException e) {
83+
System.out.println("GOOD: we got the expected exception");
84+
System.out.println(e.getMessage());
85+
86+
if (e.getServerErrorResponses() != null) {
87+
for (ServerErrorResponse error : e.getServerErrorResponses().getErrors()) {
88+
System.out.println("ERROR: " + error);
89+
}
90+
}
91+
}
92+
}
93+
94+
}

‎libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostAccessForbiddenException.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

22
package com.sparkpost.exception;
33

4+
import com.sparkpost.model.responses.ServerErrorResponses;
5+
46
public class SparkPostAccessForbiddenException extends SparkPostException {
57

68
private static final long serialVersionUID = 4127644290615861545L;
79

8-
private static final String MESSAGE = "API key has no required permission to perform this action.";
10+
private static final String MESSAGE = "API key does not have the required permission to perform this action.";
911

1012
private final String serverMessage;
1113

@@ -19,6 +21,11 @@ public SparkPostAccessForbiddenException(String serverMessage) {
1921
this.serverMessage = serverMessage;
2022
}
2123

24+
public SparkPostAccessForbiddenException(String serverMessage, ServerErrorResponses errorResponses) {
25+
super(MESSAGE, errorResponses);
26+
this.serverMessage = serverMessage;
27+
}
28+
2229
public String getServerMessage() {
2330
return this.serverMessage;
2431
}

‎libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostAuthorizationFailedException.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
package com.sparkpost.exception;
33

4+
import com.sparkpost.model.responses.ServerErrorResponses;
5+
46
public class SparkPostAuthorizationFailedException extends SparkPostException {
57

68
private static final long serialVersionUID = 3695537080454452130L;
@@ -19,6 +21,11 @@ public SparkPostAuthorizationFailedException(String serverMessage) {
1921
this.serverMessage = serverMessage;
2022
}
2123

24+
public SparkPostAuthorizationFailedException(String serverMessage, ServerErrorResponses errorResponses) {
25+
super(MESSAGE, errorResponses);
26+
this.serverMessage = serverMessage;
27+
}
28+
2229
public String getServerMessage() {
2330
return this.serverMessage;
2431
}

‎libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostErrorServerResponseException.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package com.sparkpost.exception;
33

4+
import com.sparkpost.model.responses.ServerErrorResponses;
45
import com.yepher.jsondoc.annotations.Description;
56

67
import lombok.Getter;
@@ -18,4 +19,9 @@ public SparkPostErrorServerResponseException(String message, int responseCode) {
1819
this.responseCode = responseCode;
1920
}
2021

22+
public SparkPostErrorServerResponseException(String message, int responseCode, ServerErrorResponses errorResponses) {
23+
super(message, errorResponses);
24+
this.responseCode = responseCode;
25+
}
26+
2127
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,49 @@
11

22
package com.sparkpost.exception;
33

4+
import com.sparkpost.model.responses.ServerErrorResponses;
5+
46
public class SparkPostException extends Exception {
57

68
private static final long serialVersionUID = 4017633480414265142L;
79

10+
private final ServerErrorResponses errorResponses;
11+
812
public SparkPostException() {
13+
this.errorResponses = null;
914
}
1015

1116
public SparkPostException(String message) {
1217
super(message);
18+
this.errorResponses = null;
19+
}
20+
21+
public SparkPostException(String message, ServerErrorResponses errorResponses) {
22+
super(message);
23+
this.errorResponses = errorResponses;
1324
}
1425

1526
public SparkPostException(Throwable cause) {
1627
super(cause);
28+
this.errorResponses = null;
29+
}
30+
31+
public SparkPostException(Throwable cause, ServerErrorResponses errorResponses) {
32+
super(cause);
33+
this.errorResponses = errorResponses;
1734
}
1835

1936
public SparkPostException(String message, Throwable cause) {
2037
super(message, cause);
38+
this.errorResponses = null;
39+
}
40+
41+
public SparkPostException(String message, Throwable cause, ServerErrorResponses errorResponses) {
42+
super(message, cause);
43+
this.errorResponses = errorResponses;
44+
}
45+
46+
public ServerErrorResponses getServerErrorResponses() {
47+
return this.errorResponses;
2148
}
2249
}

‎libs/sparkpost-lib/src/main/java/com/sparkpost/model/Base.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Base {
1515

1616
/**
1717
* Generate JSON for this request
18-
*
18+
*
1919
* @return json of object
2020
*/
2121
public String toJson() {
@@ -40,9 +40,12 @@ public String toJson(boolean prettyPrint) {
4040

4141
/**
4242
* Generate JSON from this object for required type.
43-
* @param tClass - target Class.
43+
*
44+
* @param tClass
45+
* - target Class.
4446
* @return json of object.
4547
*/
48+
@SuppressWarnings("rawtypes")
4649
public String toJson(Class tClass) {
4750
return GSON_BUILDER.setPrettyPrinting().create().toJson(this, tClass);
4851
}

‎libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookDescription.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package com.sparkpost.model;
33

4-
import java.util.Arrays;
54
import java.util.List;
65

76
import com.google.gson.annotations.SerializedName;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
package com.sparkpost.model.responses;
3+
4+
import com.sparkpost.model.Base;
5+
import com.yepher.jsondoc.annotations.Description;
6+
7+
import lombok.Data;
8+
import lombok.EqualsAndHashCode;
9+
10+
@Data
11+
@EqualsAndHashCode(callSuper = true)
12+
public class ServerErrorResponse extends Base {
13+
14+
@Description(value = "Error message from server", sample = {""})
15+
private String message;
16+
17+
@Description(value = "Error descrption from server", sample = {""})
18+
private String description;
19+
20+
@Description(value = "Error code from server", sample = {""})
21+
private String code;
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
package com.sparkpost.model.responses;
3+
4+
import java.util.List;
5+
6+
import com.yepher.jsondoc.annotations.Description;
7+
8+
import lombok.Data;
9+
import lombok.EqualsAndHashCode;
10+
11+
@Data
12+
@EqualsAndHashCode(callSuper = true)
13+
public class ServerErrorResponses extends Response {
14+
15+
@Description(value = "An array errors from the server.", sample = {""})
16+
List<ServerErrorResponse> errors;
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.