-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update game controller for (future) usage without sockets and Lurkars…
- Loading branch information
Showing
4 changed files
with
342 additions
and
20 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
46 changes: 46 additions & 0 deletions
46
...n/java/de/champonthis/ghs/server/controller/ControllerResponseEntityExceptionHandler.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,46 @@ | ||
/** | ||
* | ||
*/ | ||
package de.champonthis.ghs.server.controller; | ||
|
||
import java.time.Instant; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
|
||
/** | ||
* | ||
*/ | ||
@ControllerAdvice | ||
public class ControllerResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
private Gson gson = new Gson(); | ||
|
||
@ExceptionHandler(value = { ResponseStatusException.class }) | ||
protected ResponseEntity<Object> handleResponseEntityStatusException(ResponseStatusException exception, | ||
WebRequest request) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
JsonObject errorResponse = new JsonObject(); | ||
errorResponse.addProperty("timestamp", Instant.now().toString()); | ||
errorResponse.addProperty("status", exception.getStatus().value()); | ||
errorResponse.addProperty("error", exception.getStatus().getReasonPhrase()); | ||
if (StringUtils.hasText(exception.getReason())) { | ||
errorResponse.addProperty("reason", exception.getReason()); | ||
} | ||
|
||
return handleExceptionInternal(exception, gson.toJson(errorResponse), headers, exception.getStatus(), request); | ||
} | ||
|
||
} |
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
Oops, something went wrong.