Skip to content

Instantly share code, notes, and snippets.

@gilbertolptn
Created May 19, 2021 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilbertolptn/7a5d63bd08411134f2bd7b8acb21027f to your computer and use it in GitHub Desktop.
Save gilbertolptn/7a5d63bd08411134f2bd7b8acb21027f to your computer and use it in GitHub Desktop.
This classs replace default error attributes globaly on Spring MVC applications
package com.example.demo.config;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;
@Component
class CustomErrorAttibutes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest request, ErrorAttributeOptions options) {
final var error = getError(request);
final var errorAttributes = super.getErrorAttributes(request, options);
errorAttributes.put("datahora", errorAttributes.get("timestamp"));
errorAttributes.put("mensagem", errorAttributes.get("error"));
errorAttributes.put("detalhes", error.getMessage());
errorAttributes.remove("status");
errorAttributes.remove("timestamp");
errorAttributes.remove("path");
errorAttributes.remove("message");
errorAttributes.remove("error");
return errorAttributes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment