[Java] Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3

1 minute read

Overview

–Some constructors and methods of the DefaultErrorAttributes class have been deprecated since Spring Boot 2.3 (not unusable)
–Error information can be obtained from DefaultErrorAttributes by using the ErrorAttributeOptions class introduced in Spring Boot 2.3.

Error information that can be obtained with DefaultErrorAttributes

DefaultErrorAttributes \ (Spring Boot 2 \ .3 \ .0 \ .RELEASE API ) etc., the attributes of the error information that can be acquired are described.

–Timestamp: Time when the error was extracted
–status: Status code
–error: Reason for error
–exception: Root exception class name
–message: Exception message
–errors: Multiple ObjectErrors (binding-errors) set in BindingResult
–trace: Exception stack trace
–path: URL path when the exception occurred

Sample code for Spring Boot 2.2

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class ErrorAttributesGetter22 {

  /**
   *Extract error information.
   *
   * @param req request information
   * @return error information
   */
  public static Map<String, Object> getErrorAttributes(HttpServletRequest req) {
    //Get detailed error information with the DefaultErrorAttributes class
    ServletWebRequest swr = new ServletWebRequest(req);
    DefaultErrorAttributes dea = new DefaultErrorAttributes(true);
    return dea.getErrorAttributes(swr, true);
  }
}

Sample code for Spring Boot 2.3

By using the ErrorAttributeOptions class, it is now possible to select whether to acquire some attributes (whether to leave them as error information).

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class ErrorAttributesGetter23 {

  /**
   *Extract error information.
   *
   * @param req request information
   * @return error information
   */
  public static Map<String, Object> getErrorAttributes(HttpServletRequest req) {
    //Get detailed error information with the DefaultErrorAttributes class
    ServletWebRequest swr = new ServletWebRequest(req);
    DefaultErrorAttributes dea = new DefaultErrorAttributes();
    ErrorAttributeOptions eao = ErrorAttributeOptions.of(
      ErrorAttributeOptions.Include.BINDING_ERRORS,
      ErrorAttributeOptions.Include.EXCEPTION,
      ErrorAttributeOptions.Include.MESSAGE,
      ErrorAttributeOptions.Include.STACK_TRACE);
    return dea.getErrorAttributes(swr, eao);
  }
}

Check the source code of DefaultErrorAttributes class of Spring Boot 2.3

Source code of the part deleted from the error information for the item that is not the acquisition target.

spring-boot/DefaultErrorAttributes.java at v2.3.1.RELEASE · spring-projects/spring-boot · GitHub

@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
	Map<String, Object> errorAttributes = getErrorAttributes(request, options.isIncluded(Include.STACK_TRACE));
	if (this.includeException != null) {
		options = options.including(Include.EXCEPTION);
	}
	if (!options.isIncluded(Include.EXCEPTION)) {
		errorAttributes.remove("exception");
	}
	if (!options.isIncluded(Include.STACK_TRACE)) {
		errorAttributes.remove("trace");
	}
	if (!options.isIncluded(Include.MESSAGE) && errorAttributes.get("message") != null) {
		errorAttributes.put("message", "");
	}
	if (!options.isIncluded(Include.BINDING_ERRORS)) {
		errorAttributes.remove("errors");
	}
	return errorAttributes;
}

Reference material