Skip to content

Commit

Permalink
custom error page
Browse files Browse the repository at this point in the history
  • Loading branch information
psalios committed Apr 7, 2018
1 parent 5639df0 commit ab41988
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mp236.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;

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

import com.mp236.entities.Error;

@RestController
public class CustomErrorController implements ErrorController {
private static final String PATH = "/error";

@Autowired
private ErrorAttributes errorAttributes;

@RequestMapping(value = PATH)
Error error(HttpServletRequest request, HttpServletResponse response) {
return new Error(response.getStatus(), getErrorAttributes(request));
}

@Override
public String getErrorPath() {
return PATH;
}

private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return errorAttributes.getErrorAttributes(requestAttributes, false);
}
}
50 changes: 50 additions & 0 deletions peaks/src/main/java/com/mp236/entities/Error.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mp236.entities;

import java.util.Map;

public class Error {

private Integer status;
private String error;
private String message;
private String timestamp;

public Error(int status, Map<String, Object> errorAttributes) {
this.status = status;
this.error = (String) errorAttributes.get("error");
this.message = (String) errorAttributes.get("message");
this.timestamp = errorAttributes.get("timestamp").toString();
}

public Integer getStatus() {
return status;
}

public void setStatus(Integer status) {
this.status = status;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}

0 comments on commit ab41988

Please sign in to comment.