package mac.winter.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.unbescape.html.HtmlEscape;

@Controller
public class ErrorController {

	@GetMapping("/error")
	public String error(HttpServletRequest request, Model model) {
		Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
		StringBuilder sb = new StringBuilder();
		sb.append("<ul>");
		while (throwable != null) {
			sb.append("<li>").append(HtmlEscape.escapeHtml5(throwable.getMessage())).append("</li>");
			throwable = throwable.getCause();
		}
		sb.append("</ul>");

		model.addAttribute("errorCode", "Error " + request.getAttribute("javax.servlet.error.status_code"));
		model.addAttribute("errorMessage", sb.toString());
		return "error";
	}

	@GetMapping("/403")
	public String forbidden() {
		return "403";
	}
}
