test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.util.Optional;
6
7import javax.servlet.RequestDispatcher;
8import javax.servlet.ServletException;
9import javax.servlet.http.HttpServlet;
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12
13import com.bittercode.constant.ResponseCode;
14import com.bittercode.model.StoreException;
15import com.bittercode.model.UserRole;
16import com.bittercode.util.StoreUtil;
17
18public class ErrorHandlerServlet extends HttpServlet {
19
20public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
21PrintWriter pw = res.getWriter();
22res.setContentType("text/html");
23
24// Fetch the exceptions
25Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
26Integer statusCode = (Integer) req.getAttribute("javax.servlet.error.status_code");
27String servletName = (String) req.getAttribute("javax.servlet.error.servlet_name");
28String requestUri = (String) req.getAttribute("javax.servlet.error.request_uri");
29String errorMessage = ResponseCode.INTERNAL_SERVER_ERROR.getMessage();
30String errorCode = ResponseCode.INTERNAL_SERVER_ERROR.name();
31
32if (statusCode == null)
33statusCode = 0;
34Optional<ResponseCode> errorCodes = ResponseCode.getMessageByStatusCode(statusCode);
35if (errorCodes.isPresent()) {
36errorMessage = errorCodes.get().getMessage();
37errorCode = errorCodes.get().name();
38}
39
40if (throwable != null && throwable instanceof StoreException) {
41StoreException storeException = (StoreException) throwable;
42if (storeException != null) {
43errorMessage = storeException.getMessage();
44statusCode = storeException.getStatusCode();
45errorCode = storeException.getErrorCode();
46storeException.printStackTrace();
47}
48}
49
50System.out.println("======ERROR TRIGGERED========");
51System.out.println("Servlet Name: " + servletName);
52System.out.println("Request URI: " + requestUri);
53System.out.println("Status Code: " + statusCode);
54System.out.println("Error Code: " + errorCode);
55System.out.println("Error Message: " + errorMessage);
56System.out.println("=============================");
57
58if (StoreUtil.isLoggedIn(UserRole.CUSTOMER, req.getSession())) {
59RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
60rd.include(req, res);
61StoreUtil.setActiveTab(pw, "home");
62showErrorMessage(pw, errorCode, errorMessage);
63
64} else if (StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
65RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
66rd.include(req, res);
67StoreUtil.setActiveTab(pw, "home");
68showErrorMessage(pw, errorCode, errorMessage);
69
70} else {
71RequestDispatcher rd = req.getRequestDispatcher("index.html");
72rd.include(req, res);
73pw.println("<script>"
74+ "document.getElementById('topmid').innerHTML='';"
75+ "document.getElementById('happy').innerHTML='';"
76+ "</script>");
77showErrorMessage(pw, errorCode, errorMessage);
78}
79
80}
81
82private void showErrorMessage(PrintWriter pw, String errorCode, String errorMessage) {
83pw.println("<div class='container my-5'>"
84+ "<div class=\"alert alert-success\" role=\"alert\" style='max-width:450px; text-align:center; margin:auto;'>\r\n"
85+ " <h4 class=\"alert-heading\">"
86+ errorCode
87+ "</h4>\r\n"
88+ " <hr>\r\n"
89+ " <p class=\"mb-0\">"
90+ errorMessage
91+ "</p>\r\n"
92+ "</div>"
93+ "</div>");
94
95}
96
97}
98