Created
May 19, 2021 12:11
-
-
Save gilbertolptn/7a5d63bd08411134f2bd7b8acb21027f to your computer and use it in GitHub Desktop.
This classs replace default error attributes globaly on Spring MVC applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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