forked from psalios/nmr-analysis
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
peaks/src/main/java/com/mp236/controllers/CustomErrorController.java
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
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); | ||
} | ||
} |
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
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; | ||
} | ||
} |