private static Task HandleExceptionsAsync(HttpContext context, Exception ex) { var responseCode = HttpStatusCode.InternalServerError; var errorCode = 500; if (ex is EntityNotFoundException) { responseCode = HttpStatusCode.NotFound; errorCode = 404; } if (ex is TenantMissmatchException) { responseCode = HttpStatusCode.Unauthorized; errorCode = 401; } if (ex is NotEnoughPlayersException) { responseCode = HttpStatusCode.BadRequest; errorCode = 400; } var result = JsonSerializer.Serialize( HandicapResponse <ExceptionHandling, ExceptionHandling> .CreateErrorResponse( new HandicapError { ErrorMessage = $"Message: {ex.Message}", ErrorCode = errorCode }) ); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)responseCode; return(context.Response.WriteAsync(result)); }