Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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