Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Created February 27, 2024 10:19
Show Gist options
  • Save aoudiamoncef/83fa9cce52fb296a4ba5cafc367f4c01 to your computer and use it in GitHub Desktop.
Save aoudiamoncef/83fa9cce52fb296a4ba5cafc367f4c01 to your computer and use it in GitHub Desktop.
Spring Boot Error Server Error Configuration

Spring Boot Error Server Configuration

This README outlines the configuration options for error handling on the server side in Spring Boot applications.

Default Error Configuration:

  • Include Binding Errors: NEVER
    • Description: Specifies whether binding errors, such as validation errors in form submissions, should be included in error responses.
    • Possible Values: ALWAYS, NEVER, ON_PARAM
  • Include Exception: false
    • Description: Specifies whether detailed exception information should be included in error responses.
    • Possible Values: true, false
  • Include Message: NEVER
    • Description: Specifies whether error messages should be included in error responses.
    • Possible Values: ALWAYS, NEVER, ON_PARAM
  • Include Stack Trace: NEVER
    • Description: Specifies whether stack traces should be included in error responses.
    • Possible Values: ALWAYS, NEVER, ON_PARAM
  • Error Controller Path: /error
    • Description: Defines the path of the error controller, which handles error requests.
    • Possible Values: Any valid URL path.
  • Whitelabel Error Page Enabled: true
    • Description: Specifies whether to enable the default Whitelabel Error Page displayed in browsers in case of a server error.
    • Possible Values: true, false

Configuration for Development Profiles (local, test, dev):

  • Include Binding Errors: ALWAYS
  • Include Exception: true
  • Include Message: ALWAYS
  • Include Stack Trace: ALWAYS

Configuration for QA and Staging Profiles:

  • Include Binding Errors: ON_PARAM
  • Include Exception: true
  • Include Message: ON_PARAM
  • Include Stack Trace: ON_PARAM

Configuration for Production Profile:

  • Whitelabel Error Page Enabled: false

These configurations allow fine-grained control over error handling behavior in different environments, ensuring appropriate error responses based on the application's requirements and deployment stages.

Examples using cURL:

Request:

curl -X GET http://localhost:8080/throw

Response:

{
  "timestamp": "2024-02-27T10:10:03.310+00:00",
  "status": 500,
  "error": "Internal Server Error",
  "path": "/throw"
}

Example with spring.profiles.active=staging:

Request:

curl -X GET http://localhost:8080/throw?trace=true

Response:

{
  "timestamp": "2024-02-27T10:16:25.951+00:00",
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.RuntimeException",
  "trace": "java.lang.RuntimeException: This endpoint always throws an error.\n\tat com.maoudia.ErrorResource.throwError(ErrorResource.java:18)\n\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:580)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:261)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:189)\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:917)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:829)\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340)\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391)\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.base/java.lang.Thread.run(Thread.java:1583)\n",
  "path": "/throw"
}

Important

Enable each profile (e.g., local, test, dev, qa, staging, prod) in your Spring Boot application to see the respective error handling behavior.

References:

server:
port: 8080
error:
include-binding-errors: NEVER
include-exception: OFF
include-message: NEVER
include-stacktrace: NEVER
path: /error
whitelabel:
enabled: ON
spring:
application:
name: app
---
spring.config.activate.on-profile: local, test, dev
server:
error:
include-binding-errors: ALWAYS
include-exception: ON
include-message: ALWAYS
include-stacktrace: ALWAYS
---
spring.config.activate.on-profile: qa, staging
server:
error:
include-binding-errors: ON_PARAM
include-exception: ON
include-message: ON_PARAM
include-stacktrace: ON_PARAM
---
spring.config.activate.on-profile: prod
server:
error:
whitelabel:
enabled: OFF
package com.maoudia;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ErrorResource {
/**
* Endpoint to intentionally throw an error.
*
* @return ResponseEntity containing an error message
*/
@GetMapping("/throw")
public ResponseEntity<String> throwError() {
// Simulate an error by throwing a RuntimeException
throw new RuntimeException("This endpoint always throws an error.");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maoudia</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maoudia-app</name>
<description>MAOUDIA APP</description>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment