INT-1564 added exception translation logic to TwitterOperationException for better display to the end user, added tests

This commit is contained in:
Oleg Zhurakousky
2010-11-08 22:32:32 -05:00
parent 0c4cfa3d6b
commit 9bf9358e5a
3 changed files with 135 additions and 14 deletions

View File

@@ -67,7 +67,7 @@ public class Twitter4jTemplate implements TwitterOperations{
return twitter.getScreenName();
}
catch (Exception e) {
throw new TwitterOperationException("Failed to obtain profile id ", e);
throw new TwitterOperationException("Failed to obtain Profile ID. ", e);
}
}
@@ -79,7 +79,7 @@ public class Twitter4jTemplate implements TwitterOperations{
return this.buildTweetsFromTwitterResponses(directMessages);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to receive Direct Messages ", e);
throw new TwitterOperationException("Failed to receive Direct Messages. ", e);
}
}
@Override
@@ -89,7 +89,8 @@ public class Twitter4jTemplate implements TwitterOperations{
return this.buildTweetsFromTwitterResponses(directMessages);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to receive Direct Messages ", e);
throw new TwitterOperationException("Failed to receive Direct Messages since the last message with ID: "
+ sinceId + ".", e);
}
}
@Override
@@ -99,7 +100,7 @@ public class Twitter4jTemplate implements TwitterOperations{
return this.buildTweetsFromTwitterResponses(mentions);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to receive Mention statuses ", e);
throw new TwitterOperationException("Failed to receive Mention statuses. ", e);
}
}
@Override
@@ -109,7 +110,8 @@ public class Twitter4jTemplate implements TwitterOperations{
return this.buildTweetsFromTwitterResponses(mentions);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to receive Mention statuses ", e);
throw new TwitterOperationException("Failed to receive Mention statuses since the last status with ID: "
+ sinceId + ".", e);
}
}
@Override
@@ -119,7 +121,7 @@ public class Twitter4jTemplate implements TwitterOperations{
return this.buildTweetsFromTwitterResponses(timelines);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to receive Timeline statuses ", e);
throw new TwitterOperationException("Failed to receive Timeline statuses. ", e);
}
}
@Override
@@ -129,7 +131,8 @@ public class Twitter4jTemplate implements TwitterOperations{
return this.buildTweetsFromTwitterResponses(timelines);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to receive Timeline statuses ", e);
throw new TwitterOperationException("Failed to receive Timeline statuses since the last status with ID: "
+ sinceId + ".", e);
}
}
@Override
@@ -140,7 +143,7 @@ public class Twitter4jTemplate implements TwitterOperations{
twitter.sendDirectMessage(userName, text);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to send Direct Message ", e);
throw new TwitterOperationException("Failed to send Direct Message to user: " + userName + ".", e);
}
}
@Override
@@ -151,7 +154,7 @@ public class Twitter4jTemplate implements TwitterOperations{
twitter.sendDirectMessage(userId, text);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to send Direct Message ", e);
throw new TwitterOperationException("Failed to send Direct Message to user with id: " + userId + ".", e);
}
}
@@ -166,7 +169,7 @@ public class Twitter4jTemplate implements TwitterOperations{
twitter.updateStatus(status);
}
catch (Exception e) {
throw new TwitterOperationException("Failed to send Status update ", e);
throw new TwitterOperationException("Failed to send Status update. ", e);
}
}

View File

@@ -15,6 +15,10 @@
*/
package org.springframework.integration.twitter.core;
import org.springframework.util.StringUtils;
import twitter4j.TwitterException;
/**
* @author Oleg Zhurakousky
* @since 2.0
@@ -23,25 +27,27 @@ package org.springframework.integration.twitter.core;
@SuppressWarnings("serial")
public class TwitterOperationException extends RuntimeException {
private int twitterStatusCode = -1;
/**
*
*/
public TwitterOperationException() {
super();
this(null, null);
}
/**
* @param description
*/
public TwitterOperationException(String description) {
super(description);
this(description, null);
}
/**
* @param throwable
*/
public TwitterOperationException(Throwable throwable) {
super(throwable);
this(null, throwable);
}
/**
@@ -49,7 +55,39 @@ public class TwitterOperationException extends RuntimeException {
* @param throwable
*/
public TwitterOperationException(String description, Throwable throwable) {
super(description, throwable);
super(formatDescription(description, throwable), throwable);
if (throwable instanceof TwitterException){
this.twitterStatusCode = ((TwitterException)throwable).getStatusCode();
}
}
public int getTwitterStatusCode() {
return twitterStatusCode;
}
private static String formatDescription(String description, Throwable throwable){
StringBuffer buffer = new StringBuffer();
if (StringUtils.hasText(description)){
buffer.append(description + " ");
}
if (throwable != null && throwable instanceof TwitterException){
TwitterException te = (TwitterException) throwable;
String message = te.getMessage();
if (StringUtils.hasText(message)){
buffer.append("Detailed Error: ");
if (message.contains("{")){
buffer.append(message.substring(0, message.indexOf("{")));
}
else {
buffer.append(message);
}
}
buffer.append("For more information about this exception please visit the following Twitter website: " +
"http://apiwiki.twitter.com/w/page/22554652/HTTP-Response-Codes-and-Errors");
}
return buffer.toString();
}
}