Skip to content

Commit 1702603

Browse files
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
1 parent f981d5a commit 1702603

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

ext_models/chen2021Fitness/model.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def fit(
1919
# adapt the data to the required format
2020
x = t
2121
x = sm.add_constant(x)
22-
y = np.column_stack((k, n-k))
23-
22+
y = np.column_stack((k, n - k))
23+
2424
# estimate the model
2525
model = sm.GLM(y, x, family=sm.families.Binomial(link=sm.families.links.logit())).fit(disp=0)
2626

@@ -32,6 +32,10 @@ def fit(
3232

3333
# take the MLE of the parameters as the functions of the MLE of beta0, beta
3434
beta0, beta1 = model.params
35+
36+
if beta1 == 0:
37+
raise ValueError("Cannot compute fit: beta1 is zero. Did you supply enough data points?")
38+
3539
t0, a, fd, fc = -beta0 / beta1, \
3640
beta1, \
3741
np.exp(beta1 * generation_time) - 1, \

ext_models/chen2021Fitness/server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77

88
@app.route("/", methods=["POST"])
9-
def without_prediction() -> Dict:
10-
return process(request.json)
9+
def without_prediction():
10+
try:
11+
return process(request.json)
12+
except Exception as e:
13+
return {"error": str(e)}, 500
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ch.ethz.covspectrum.controller
2+
3+
import org.springframework.http.HttpStatus
4+
import org.springframework.http.ResponseEntity
5+
import org.springframework.web.bind.annotation.ControllerAdvice
6+
import org.springframework.web.bind.annotation.ExceptionHandler
7+
import org.springframework.web.bind.annotation.ResponseStatus
8+
import org.springframework.web.client.HttpStatusCodeException
9+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
10+
11+
@ControllerAdvice
12+
class ExceptionHandler : ResponseEntityExceptionHandler() {
13+
@ExceptionHandler(Throwable::class)
14+
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
15+
fun handleUnexpectedException(e: Throwable): ResponseEntity<ErrorResponse> {
16+
return ResponseEntity(
17+
ErrorResponse(
18+
HttpStatus.INTERNAL_SERVER_ERROR.value(),
19+
HttpStatus.INTERNAL_SERVER_ERROR.name,
20+
e.message ?: "An unexpected error occurred"
21+
),
22+
HttpStatus.INTERNAL_SERVER_ERROR
23+
)
24+
}
25+
26+
@ExceptionHandler(HttpStatusCodeException::class)
27+
fun handleBadRequestException(e: HttpStatusCodeException): ResponseEntity<String> {
28+
return ResponseEntity(e.responseBodyAsString, e.statusCode)
29+
}
30+
}
31+
32+
data class ErrorResponse(
33+
val status: Int,
34+
val error: String,
35+
val message: String
36+
)

0 commit comments

Comments
 (0)