private bool TryHandleCoreException(Exception maybeCoreException, out Exception e) { if (maybeCoreException is NotImplementedException notImplemented) { e = new DartyAPIException(StatusCodes.Status501NotImplemented, notImplemented.Message); } else if (maybeCoreException is GameNotFoundException gameNotFound) { e = new DartyAPIException(StatusCodes.Status404NotFound, gameNotFound.Message); } else if (maybeCoreException is InvalidPlayerException invalidPlayer) { e = new DartyAPIException(StatusCodes.Status400BadRequest, invalidPlayer.Message); } else if (maybeCoreException is GameOverException gameOver) { e = new DartyAPIException(StatusCodes.Status409Conflict, gameOver.Message); } else if (maybeCoreException is InvalidDartMultiplierException invalidMultiplier) { e = new DartyAPIException(StatusCodes.Status400BadRequest, invalidMultiplier.Message); } else if (maybeCoreException is InvalidDartValueException invalidValue) { e = new DartyAPIException(StatusCodes.Status400BadRequest, invalidValue.Message); } else { e = maybeCoreException; // was not handle-able return(false); } // was handled return(true); }
private IActionResult HandleException(Exception e) { if (TryHandleCoreException(e, out Exception newException)) { DartyAPIException handledException = newException as DartyAPIException; _logger.LogWarning($"Known error was handled. StatusCode: {handledException.Error.Code}, Message: {handledException.Error.Message}"); // handle known errors return(new ObjectResult(handledException.Error) { StatusCode = handledException.Code }); } else { _logger.LogError($"Unknown error. Message: {e.Message}"); // 500 on unknown errors return(new InternalServerErrorObjectResult()); } }