Skip to content

Instantly share code, notes, and snippets.

@andr3medeiros
Last active May 3, 2018 19:56
Show Gist options
  • Save andr3medeiros/58cd97bb14e58c532564667d4882daca to your computer and use it in GitHub Desktop.
Save andr3medeiros/58cd97bb14e58c532564667d4882daca to your computer and use it in GitHub Desktop.
Spring Boot Angular Refresh/Error Handler
package br.com.app.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import br.com.app.constants.Constants;
@Controller
@RequestMapping("${error.path:/error}")
public class AngularErrorController implements ErrorController {
private final Log LOG = LogFactory.getLog(this.getClass());
@Value("${error.path:/error}")
private String errorPath;
@Autowired
private ErrorAttributes errorAttributes;
private ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build();
@Override
public String getErrorPath() {
return this.errorPath;
}
@RequestMapping
public Object error(HttpServletRequest request) {
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
Map<String, Object> attributes = errorAttributes.getErrorAttributes(requestAttributes, false);
HttpStatus status = getStatus(attributes);
if(status.equals(HttpStatus.NOT_FOUND) || unauthorizedHandledByAngular(attributes)) {
if(!request.getServletPath().contains(Constants.API)) {
return new ModelAndView(Constants.INDEX_PAGE);
}
}
if(status.equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
try {
LOG.error(mapper.writeValueAsString(attributes));
} catch (JsonProcessingException e) {
LOG.error(e.getMessage(), e);
}
}
return new ResponseEntity<Object>(attributes, status);
}
private HttpStatus getStatus(Map<String, Object> attributes) {
Integer statusCode = (Integer) attributes.get("status");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
private boolean unauthorizedHandledByAngular(Map<String, Object> attributes) {
HttpStatus status = getStatus(attributes);
return status.equals(HttpStatus.UNAUTHORIZED) && attributes.get("exception") == null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment