Skip to content

Commit

Permalink
Merge pull request #28 from The-Socialites/errorHandling
Browse files Browse the repository at this point in the history
custom error handler
  • Loading branch information
Umi007 committed Jun 26, 2024
2 parents 17013c9 + 02aabf8 commit fc3aae6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.makersacademy.acebook.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CustomErrorController implements ErrorController {

@RequestMapping("/error")
public String handleError(HttpServletRequest request, Model model) {
Object status = request.getAttribute("javax.servlet.error.status_code");
Object message = request.getAttribute("javax.servlet.error.message");

model.addAttribute("status", status);
model.addAttribute("message", message);

return "error";
}
}
21 changes: 21 additions & 0 deletions src/main/resources/templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Error</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
<div class="container mx-auto p-8">
<h1 class="text-4xl font-bold mb-8 text-center">Error Page</h1>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4">
<strong class="font-bold">Error:</strong>
<span th:text="${status}"></span>
</div>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4">
<strong class="font-bold">Message:</strong>
<span th:text="${message}"></span>
</div>
</div>
</body>
</html>

0 comments on commit fc3aae6

Please sign in to comment.