diff --git a/springBootBlog/src/main/java/com/yen/mdblog/config/CustomErrorConfig.java b/springBootBlog/src/main/java/com/yen/mdblog/config/CustomErrorConfig.java new file mode 100644 index 000000000..83a69e469 --- /dev/null +++ b/springBootBlog/src/main/java/com/yen/mdblog/config/CustomErrorConfig.java @@ -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 getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { + + Map errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace); + // Add custom error attributes if needed + return errorAttributes; + } + }; + + } + +} diff --git a/springBootBlog/src/main/java/com/yen/mdblog/controller/CustomErrorController.java b/springBootBlog/src/main/java/com/yen/mdblog/controller/CustomErrorController.java new file mode 100644 index 000000000..9e0f06ea7 --- /dev/null +++ b/springBootBlog/src/main/java/com/yen/mdblog/controller/CustomErrorController.java @@ -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") + } + +} diff --git a/springBootBlog/src/main/resources/application.properties b/springBootBlog/src/main/resources/application.properties index 2c52f99ea..d9297233e 100644 --- a/springBootBlog/src/main/resources/application.properties +++ b/springBootBlog/src/main/resources/application.properties @@ -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 \ No newline at end of file +# google login + +# custom error html +server.error.path=/error \ No newline at end of file diff --git a/springBootBlog/src/main/resources/templates/404.html b/springBootBlog/src/main/resources/templates/404.html new file mode 100644 index 000000000..e3efda487 --- /dev/null +++ b/springBootBlog/src/main/resources/templates/404.html @@ -0,0 +1,14 @@ + + + + + + + Error + + +

404 Error

+

An error occurred. Please try again later.

+Back + + diff --git a/springBootBlog/src/main/resources/templates/501.html b/springBootBlog/src/main/resources/templates/501.html new file mode 100644 index 000000000..7656d011d --- /dev/null +++ b/springBootBlog/src/main/resources/templates/501.html @@ -0,0 +1,14 @@ + + + + + + + Error + + +

501 Error

+

An error occurred. Please try again later.

+Back + + diff --git a/springBootBlog/src/main/resources/templates/error.html b/springBootBlog/src/main/resources/templates/error.html new file mode 100644 index 000000000..7866351ed --- /dev/null +++ b/springBootBlog/src/main/resources/templates/error.html @@ -0,0 +1,15 @@ + + + + + + + Error + + +

Error

+

An error occurred. Please try again later.

+Back + + +