package net.sourceforge.fenixedu.presentationTier.config;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.fenixedu.domain.exceptions.DomainException;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;

public class FenixDomainExceptionHandler extends ExceptionHandler {

	public ActionForward execute(Exception ex, ExceptionConfig exceptionConfig, ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
			throws ServletException {

		ActionForward forward = mapping.getInputForward();

		if (ex instanceof DomainException) {
			DomainException domainException = (DomainException) ex; 
			String property = domainException.getKey();
			ActionError error = new ActionError(domainException.getKey(), domainException.getArgs());
			
			super.storeException(request, property, error, forward, exceptionConfig.getScope());
		}
		return forward;
	}

}
