-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(chen2021Fitness): don't hand out invalid JSON when there are not …
…enough data to compute a fit (#42) Some values were NaN or Infinity, which are no valid JSON tokens. Instead throw an error. resolves #41
- Loading branch information
1 parent
f981d5a
commit 1702603
Showing
3 changed files
with
47 additions
and
4 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
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
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/ch/ethz/covspectrum/controller/ExceptionHandler.kt
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,36 @@ | ||
package ch.ethz.covspectrum.controller | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
import org.springframework.web.client.HttpStatusCodeException | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler | ||
|
||
@ControllerAdvice | ||
class ExceptionHandler : ResponseEntityExceptionHandler() { | ||
@ExceptionHandler(Throwable::class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
fun handleUnexpectedException(e: Throwable): ResponseEntity<ErrorResponse> { | ||
return ResponseEntity( | ||
ErrorResponse( | ||
HttpStatus.INTERNAL_SERVER_ERROR.value(), | ||
HttpStatus.INTERNAL_SERVER_ERROR.name, | ||
e.message ?: "An unexpected error occurred" | ||
), | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
) | ||
} | ||
|
||
@ExceptionHandler(HttpStatusCodeException::class) | ||
fun handleBadRequestException(e: HttpStatusCodeException): ResponseEntity<String> { | ||
return ResponseEntity(e.responseBodyAsString, e.statusCode) | ||
} | ||
} | ||
|
||
data class ErrorResponse( | ||
val status: Int, | ||
val error: String, | ||
val message: String | ||
) |