All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.social.evernote.api.EvernoteExceptionUtils Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package org.springframework.social.evernote.api;

import com.evernote.edam.error.EDAMNotFoundException;
import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;

/**
 * @author Tadaya Tsuyukubo
 */
public class EvernoteExceptionUtils {

	/**
	 * convert to {@link EvernoteException}
	 *
	 * @param ex original exception
	 * @return converted exception
	 */
	public static EvernoteException convert(Exception ex) {
		StringBuilder sb = new StringBuilder();
		sb.append(ex.getMessage());
		if (ex instanceof EDAMUserException) {
			sb.append(", ErrorCode [");
			sb.append(((EDAMUserException) ex).getErrorCode());
			sb.append("], Parameter [");
			sb.append(((EDAMUserException) ex).getParameter());
			sb.append("]");
		}
		if (ex instanceof EDAMSystemException) {
			sb.append(", ErrorCode [");
			sb.append(((EDAMSystemException) ex).getErrorCode());
			sb.append("], RateLimitDuration [");
			sb.append(((EDAMSystemException) ex).getRateLimitDuration());
			sb.append("]");
		}
		if (ex instanceof EDAMNotFoundException) {
			sb.append(", Identifier [");
			sb.append(((EDAMNotFoundException) ex).getIdentifier());
			sb.append("], Key [");
			sb.append(((EDAMNotFoundException) ex).getKey());
			sb.append("]");
		}
		return new EvernoteException(sb.toString(), ex);
	}

	/**
	 * Execute the given operation and wrap the thrown exception to {@link EvernoteException}.
	 *
	 * @param operation a callback operation
	 * @param        operation return type
	 * @return return from callback operation
	 * @throws EvernoteException EvernoteException wrapping a thrown original exception
	 */
	public static  T wrap(Operation operation) throws EvernoteException {
		try {
			return operation.execute();
		} catch (Exception ex) {
			throw convert(ex);
		}
	}

	/**
	 * Callback interface for {@link EvernoteExceptionUtils#wrap(org.springframework.social.evernote.api.EvernoteExceptionUtils.Operation)}.
	 *
	 * @param 
	 */
	public static interface Operation {
		T execute() throws Exception;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy