Skip to content

Commit

Permalink
add error html, error config, controller, setting
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 14, 2023
1 parent 8787e6f commit 02d8623
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.yen.mdblog.config;

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Configuration
public class CustomErrorConfig {

@Bean
public DefaultErrorAttributes customErrorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {

Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
// Add custom error attributes if needed
return errorAttributes;
}
};

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.yen.mdblog.controller;

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

public class CustomErrorController implements ErrorController{

@Override
public String getErrorPath() {
return "/error";
}

@RequestMapping("/error")
public String handleError(){

// Provide logic to determine the error status and redirect accordingly
// For example, you can use the HttpServletResponse.getStatus() method
return "error"; // Thymeleaf template name (e.g., "error.html")
}

}
5 changes: 4 additions & 1 deletion springBootBlog/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ logging.level.org.springframework.security=TRACE
# github login
spring.security.oauth2.client.registration.github.clientId=8c3b3cfa5ba107d7ad25
spring.security.oauth2.client.registration.github.clientSecret=
# google login
# google login

# custom error html
server.error.path=/error
14 changes: 14 additions & 0 deletions springBootBlog/src/main/resources/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- error.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
</head>
<body>
<h1>404 Error</h1>
<p>An error occurred. Please try again later.</p>
<a href="/posts/all">Back</a>
</body>
</html>
14 changes: 14 additions & 0 deletions springBootBlog/src/main/resources/templates/501.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- error.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
</head>
<body>
<h1>501 Error</h1>
<p>An error occurred. Please try again later.</p>
<a href="/posts/all">Back</a>
</body>
</html>
15 changes: 15 additions & 0 deletions springBootBlog/src/main/resources/templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- error.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
</head>
<body>
<h1>Error</h1>
<p>An error occurred. Please try again later.</p>
<a href="/posts/all">Back</a>

</body>
</html>

0 comments on commit 02d8623

Please sign in to comment.