github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/ErrorHandlerServlet.java (about)

     1  package com.shashi.servlets;
     2  
     3  import java.io.IOException;
     4  import java.io.PrintWriter;
     5  import java.util.Optional;
     6  
     7  import javax.servlet.RequestDispatcher;
     8  import javax.servlet.ServletException;
     9  import javax.servlet.http.HttpServlet;
    10  import javax.servlet.http.HttpServletRequest;
    11  import javax.servlet.http.HttpServletResponse;
    12  
    13  import com.shashi.beans.TrainException;
    14  import com.shashi.constant.ResponseCode;
    15  
    16  public class ErrorHandlerServlet extends HttpServlet {
    17  
    18  	/**
    19  	 * 
    20  	 */
    21  	private static final long serialVersionUID = 1L;
    22  
    23  	public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    24  		PrintWriter pw = res.getWriter();
    25  		res.setContentType("text/html");
    26  
    27  		// Fetch the exceptions
    28  		Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
    29  		Integer statusCode = (Integer) req.getAttribute("javax.servlet.error.status_code");
    30  		String servletName = (String) req.getAttribute("javax.servlet.error.servlet_name");
    31  		String requestUri = (String) req.getAttribute("javax.servlet.error.request_uri");
    32  		String errorMessage = ResponseCode.INTERNAL_SERVER_ERROR.getMessage();
    33  		String errorCode = ResponseCode.INTERNAL_SERVER_ERROR.name();
    34  
    35  		if (statusCode == null)
    36  			statusCode = 0;
    37  		Optional<ResponseCode> errorCodes = ResponseCode.getMessageByStatusCode(statusCode);
    38  		if (errorCodes.isPresent()) {
    39  			errorMessage = errorCodes.get().getMessage();
    40  			errorCode = errorCodes.get().name();
    41  		}
    42  
    43  		if (throwable != null && throwable instanceof TrainException) {
    44  			TrainException trainException = (TrainException) throwable;
    45  			if (trainException != null) {
    46  				errorMessage = trainException.getMessage();
    47  				statusCode = trainException.getStatusCode();
    48  				errorCode = trainException.getErrorCode();
    49  				trainException.printStackTrace();
    50  			}
    51  		} else if (throwable != null) {
    52  			errorMessage = throwable.getMessage();
    53  			errorCode = throwable.getLocalizedMessage();
    54  		}
    55  
    56  		System.out.println("======ERROR TRIGGERED========");
    57  		System.out.println("Servlet Name: " + servletName);
    58  		System.out.println("Request URI: " + requestUri);
    59  		System.out.println("Status Code: " + statusCode);
    60  		System.out.println("Error Code: " + errorCode);
    61  		System.out.println("Error Message: " + errorMessage);
    62  		System.out.println("=============================");
    63  
    64  		if (statusCode == 401) {
    65  			RequestDispatcher rd = req.getRequestDispatcher("UserLogin.html");
    66  			rd.include(req, res);
    67  			pw.println("<div class='tab'><p1 class='menu'>" + errorMessage + "</p1></div>");
    68  
    69  		} else {
    70  			RequestDispatcher rd = req.getRequestDispatcher("error.html");
    71  			rd.include(req, res);
    72  			pw.println("<div style='margin-top:20%; text-align:center;'>\r\n"
    73  					+ "	<p class=\"menu\" style='color:red'>" + errorCode + "</p><br>\r\n" + "	<p class=\"menu\">"
    74  					+ errorMessage + "</p>\r\n" + "  </div>");
    75  
    76  		}
    77  
    78  	}
    79  
    80  }