package info.bukova.isspst.ui;

import info.bukova.isspst.StringUtils;
import info.bukova.isspst.services.ModuleNotActiveException;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Executions;

public class ErrorVM {
	
	private String message;
	private final Logger logger = LoggerFactory.getLogger(ErrorVM.class);
	
	@Init
	public void init() {
		HttpServletRequest request = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
		Throwable ex = (Throwable) request.getAttribute("javax.servlet.error.exception");
		Throwable root = ex;
		
		if (root != null) {
			while (root.getCause() != null) {
				root = root.getCause();
			}
			
			if (root instanceof ModuleNotActiveException) {
				message = StringUtils.localize("ModuleNotActive");
			} else {
				logger.error("Unhandled exception", ex);
				message = ex.toString();
			}
		}
	}
	
	public String getMessage() {
		return message;
	}

}
