diff --git a/ext_models/chen2021Fitness/model.py b/ext_models/chen2021Fitness/model.py index 243b59b..f87c0ab 100644 --- a/ext_models/chen2021Fitness/model.py +++ b/ext_models/chen2021Fitness/model.py @@ -19,8 +19,8 @@ def fit( # adapt the data to the required format x = t x = sm.add_constant(x) - y = np.column_stack((k, n-k)) - + y = np.column_stack((k, n - k)) + # estimate the model model = sm.GLM(y, x, family=sm.families.Binomial(link=sm.families.links.logit())).fit(disp=0) @@ -32,6 +32,10 @@ def fit( # take the MLE of the parameters as the functions of the MLE of beta0, beta beta0, beta1 = model.params + + if beta1 == 0: + raise ValueError("Cannot compute fit: beta1 is zero. Did you supply enough data points?") + t0, a, fd, fc = -beta0 / beta1, \ beta1, \ np.exp(beta1 * generation_time) - 1, \ diff --git a/ext_models/chen2021Fitness/server.py b/ext_models/chen2021Fitness/server.py index 5615583..ccd956b 100644 --- a/ext_models/chen2021Fitness/server.py +++ b/ext_models/chen2021Fitness/server.py @@ -6,5 +6,8 @@ @app.route("/", methods=["POST"]) -def without_prediction() -> Dict: - return process(request.json) +def without_prediction(): + try: + return process(request.json) + except Exception as e: + return {"error": str(e)}, 500 diff --git a/src/main/kotlin/ch/ethz/covspectrum/controller/ExceptionHandler.kt b/src/main/kotlin/ch/ethz/covspectrum/controller/ExceptionHandler.kt new file mode 100644 index 0000000..ca57172 --- /dev/null +++ b/src/main/kotlin/ch/ethz/covspectrum/controller/ExceptionHandler.kt @@ -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 { + 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 { + return ResponseEntity(e.responseBodyAsString, e.statusCode) + } +} + +data class ErrorResponse( + val status: Int, + val error: String, + val message: String +)