INT-1564 added exception translation logic to TwitterOperationException for better display to the end user, added tests
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.twitter.core;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class TwitterOperationExceptionTests {
|
||||
|
||||
@Test
|
||||
public void test401(){
|
||||
// will result in exception since a,b,c,d are invalid credentials
|
||||
Twitter4jTemplate template = new Twitter4jTemplate("a", "b", "c", "d");
|
||||
try {
|
||||
template.getProfileId();
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof TwitterOperationException);
|
||||
assertEquals(401, ((TwitterOperationException)e).getTwitterStatusCode());
|
||||
assertTrue(e.getMessage().contains("401:Authentication credentials were missing or incorrect"));
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testWithNull(){
|
||||
try {
|
||||
throw new TwitterOperationException();
|
||||
} catch (Exception e) {
|
||||
TwitterOperationException tex = (TwitterOperationException) e;
|
||||
assertFalse(StringUtils.hasText(tex.getMessage()));
|
||||
assertEquals(-1, tex.getTwitterStatusCode());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testWithTwitterException(){
|
||||
try {
|
||||
throw new TwitterOperationException(new TwitterException("foo"));
|
||||
} catch (Exception e) {
|
||||
TwitterOperationException tex = (TwitterOperationException) e;
|
||||
assertTrue(StringUtils.hasText(tex.getMessage()));
|
||||
assertTrue(tex.getMessage().contains("foo"));
|
||||
assertEquals(-1, tex.getTwitterStatusCode());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testWithDescription(){
|
||||
try {
|
||||
throw new TwitterOperationException("foo");
|
||||
} catch (Exception e) {
|
||||
TwitterOperationException tex = (TwitterOperationException) e;
|
||||
assertTrue(StringUtils.hasText(tex.getMessage()));
|
||||
assertTrue(tex.getMessage().contains("foo"));
|
||||
assertEquals(-1, tex.getTwitterStatusCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user