-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from SAKPaaS/bugfix/occupancy-range
Added Validators to occupancy reports
- Loading branch information
Showing
6 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
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,17 @@ | ||
package de.sakpaas.backend.dto; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
@Data | ||
public class Error400Dto { | ||
|
||
private Date timestamp; | ||
private final int status = 400; | ||
private final String error = "Bad Request"; | ||
private String message; | ||
private String path; | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/de/sakpaas/backend/util/GlobalExceptionHandler.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,42 @@ | ||
package de.sakpaas.backend.util; | ||
|
||
import de.sakpaas.backend.dto.Error400Dto; | ||
import org.springframework.context.support.DefaultMessageSourceResolvable; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
/** | ||
* This ErrorHandler handles @Valid errors. | ||
*/ | ||
@Override | ||
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception, | ||
HttpHeaders headers, | ||
HttpStatus status, WebRequest request) { | ||
Error400Dto error = new Error400Dto(); | ||
error.setTimestamp(new Date()); | ||
error.setPath(((ServletWebRequest) request).getRequest().getServletPath()); | ||
|
||
// Iterate over all errors and get the messages | ||
String message = exception.getBindingResult() | ||
.getFieldErrors() | ||
.stream() | ||
.map(DefaultMessageSourceResolvable::getDefaultMessage) | ||
.collect(Collectors.joining(", ")); | ||
error.setMessage(message); | ||
|
||
return new ResponseEntity<>(error, headers, status); | ||
} | ||
} |
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
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
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
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