package eu.dnetlib.r2d2.thrift;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class ExceptionTranslationAspect {
	@AfterThrowing(pointcut = "scholarlynk()", throwing = "ex")
	public void throwing(final Throwable ex) throws InvalidOperation {
		StringWriter buffer = new StringWriter();
		buffer.append(ex.getMessage());

		buffer.append(": ");
		
		buffer.append(ex.getClass().getName());

		buffer.append(":          ");

		ex.printStackTrace(new PrintWriter(buffer));

		throw new InvalidOperation(InvalidOperationReason.UNHANDLED_EXCEPTION, "unhandled exception", buffer.toString());
	}

	@Pointcut("execution(* eu.dnetlib.r2d2.ScholarlynkImpl.*(..))")
	public void scholarlynk() {
	}
}
