Skip to content

Commit

Permalink
ruff: Enable flake8-blind-except ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
ranta committed Nov 26, 2024
1 parent 8e16bf5 commit 8c44858
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config/health_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def check_status(self) -> None:
self.add_error(ServiceUnavailable("Unable to connect to Redis: Timeout."), error)
except exceptions.ConnectionError as error:
self.add_error(ServiceUnavailable("Unable to connect to Redis: Connection Error"), error)
except BaseException as error:
except BaseException as error: # noqa: BLE001
self.add_error(ServiceUnavailable("Unknown error"), error)
else:
logger.debug("Connection established. Redis sentinel is healthy.")
6 changes: 3 additions & 3 deletions config/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class GraphQLSentryMiddleware:
def resolve(self, next_: GraphQLFieldResolver, root: Any, info: GQLInfo, **kwargs: Any) -> Any:
try:
return next_(root, info, **kwargs)
except Exception as err:
except Exception as err: # noqa: BLE001
SentryLogger.log_exception(err, "Error in GraphQL query")
return err

Expand All @@ -80,7 +80,7 @@ class GraphQLErrorLoggingMiddleware:
def resolve(self, next_: GraphQLFieldResolver, root: Any, info: GQLInfo, **kwargs: Any) -> Any:
try:
return next_(root, info, **kwargs)
except Exception as err:
except Exception as err: # noqa: BLE001
logger.info(traceback.format_exc())
return err

Expand Down Expand Up @@ -127,7 +127,7 @@ def query_logger(self, request: WSGIRequest) -> Generator[None]:
body=request.body,
duration_ms=total_duration_ms,
)
except Exception as error:
except Exception as error: # noqa: BLE001
SentryLogger.log_exception(error, "Error in QueryLoggingMiddleware")

def log( # noqa: PLR0917
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ select = [
"ANN", # flake8-annotations
"ARG", # flake8-unused-arguments
"B", # flake8-bugbear
#"BLE", # flake8-blind-except # TODO: Enable this and fix (7) issues
"BLE", # flake8-blind-except
"C4", # flake8-comprehensions
"COM", # flake8-commas
"D200", # pydocstyle: One-line docstring should fit on one line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,7 @@ def test__reservation_unit__first_reservable_time__remove_not_reservable(graphql
# 8) Fetch reservation units for response
try:
response.assert_query_count(8) # Locally only 8 queries are made
except BaseException:
except BaseException: # noqa: BLE001
response.assert_query_count(10) # In CI, sometimes 10 queries are made, but we don't know why. This is fine.


Expand Down
4 changes: 2 additions & 2 deletions tilavarauspalvelu/admin/application_round/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def export_applications_to_csv(self, request: WSGIRequest, pk: int) -> FileRespo
try:
exporter = ApplicationRoundApplicationsCSVExporter(application_round_id=pk)
response = exporter.to_file_response()
except Exception as error:
except Exception as error: # noqa: BLE001
self.message_user(request, f"Error while exporting applications: {error}", level=messages.ERROR)
SentryLogger.log_exception(error, "Error while exporting ApplicationRound applications")
return None
Expand All @@ -120,7 +120,7 @@ def export_results_to_csv(self, request: WSGIRequest, pk: int) -> FileResponse |
try:
exporter = ApplicationRoundResultCSVExporter(application_round_id=pk)
response = exporter.to_file_response()
except Exception as error:
except Exception as error: # noqa: BLE001
self.message_user(request, f"Error while exporting results: {error}", level=messages.ERROR)
SentryLogger.log_exception(error, "Error while exporting ApplicationRound results")
return None
Expand Down
2 changes: 1 addition & 1 deletion tilavarauspalvelu/admin/email_template/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def email_tester_admin_view(request: WSGIRequest, email_type: str) -> HttpRespon

try:
email_type = EmailType(email_type)
except Exception:
except ValueError:
return HttpResponse(f"Invalid email type: {email_type}")

tester_form_class = select_tester_form(email_type=email_type)
Expand Down
2 changes: 1 addition & 1 deletion tilavarauspalvelu/admin/email_template/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def email_templates_admin_list_view(request: WSGIRequest, **kwargs: Any) -> Tabl
def email_type_admin_view(request: WSGIRequest, email_type: str) -> HttpResponse:
try:
email_type = EmailType(email_type)
except Exception:
except ValueError:
return HttpResponse("Invalid email type")

as_html = request.GET.get("text", "false").lower() not in {"true", "1", "t"}
Expand Down
2 changes: 1 addition & 1 deletion tilavarauspalvelu/admin/request_log/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def export_results_to_csv(self, request: WSGIRequest, queryset: RequestLogQueryS
try:
exporter = SQLLogCSVExporter(queryset=queryset)
response = exporter.to_file_response()
except Exception as error:
except Exception as error: # noqa: BLE001
self.message_user(request, f"Error while exporting results: {error}", level=messages.ERROR)
SentryLogger.log_exception(error, "Error while exporting SQL log results")
return None
Expand Down
2 changes: 1 addition & 1 deletion tilavarauspalvelu/admin/reservation_unit/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def export_to_csv(self, request: WSGIRequest, queryset: ReservationUnitQuerySet)
try:
exporter = ReservationUnitExporter(queryset=queryset)
response = exporter.to_file_response()
except Exception as error:
except Exception as error: # noqa: BLE001
self.message_user(request, f"Error while exporting results: {error}", level=messages.ERROR)
SentryLogger.log_exception(error, "Error while exporting ReservationUnits")
return None
Expand Down
2 changes: 1 addition & 1 deletion tilavarauspalvelu/admin/unit/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def update_from_tprek(self, request: WSGIRequest, queryset: QuerySet[Unit]) -> N
tprek_unit_importer = TprekUnitImporter()
try:
tprek_unit_importer.update_unit_from_tprek(units, force_update=True)
except Exception as err:
except Exception as err: # noqa: BLE001
details = f"Tried to import units from TPREK: '{units.values_list('pk', flat=True)}'"
SentryLogger.log_exception(err, details=details)
self.message_user(request, f"Error while importing units from TPREK: {err}", level=messages.ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _prefill_reservation_from_profile(self, data: dict[str, Any]) -> None:
prefill_info = HelsinkiProfileClient.get_reservation_prefill_info(user=user, session=request.session)
except ExternalServiceError:
return
except Exception as error:
except Exception as error: # noqa: BLE001
msg = "Unexpected error reading profile data"
SentryLogger.log_exception(error, details=msg, user=user.pk)
return
Expand Down
6 changes: 3 additions & 3 deletions tilavarauspalvelu/api/rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def reservation_ical(request: WSGIRequest, pk: int) -> FileResponse | JsonRespon

try:
ical = reservation.actions.to_ical()
except Exception as exc:
except Exception as exc: # noqa: BLE001
return JsonResponse(data={"detail": str(exc)}, status=500)

buffer = io.BytesIO()
Expand Down Expand Up @@ -105,7 +105,7 @@ def terms_of_use_pdf(request: WSGIRequest) -> FileResponse | JsonResponse:

try:
pdf = render_to_pdf("terms_of_use/booking_terms.jinja", **context)
except Exception as error:
except Exception as error: # noqa: BLE001
data = {"detail": f"PDF could not be rendered: {error}"}
return JsonResponse(data=data, status=500)

Expand Down Expand Up @@ -133,7 +133,7 @@ def readiness_check(request: WSGIRequest) -> JsonResponse:
connection.ensure_connection()
apps.check_apps_ready()
apps.check_models_ready()
except Exception as error:
except Exception as error: # noqa: BLE001
return JsonResponse({"status": "NOT_READY", "error": str(error)}, status=503)

return JsonResponse({"status": "READY"}, status=200)
2 changes: 1 addition & 1 deletion tilavarauspalvelu/integrations/email/attachements.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def get_reservation_ical_attachment(reservation: Reservation) -> EmailAttachment | None:
try:
ical = reservation.actions.to_ical()
except Exception as exc:
except Exception as exc: # noqa: BLE001
SentryLogger.log_exception(exc, "Failed to generate iCal attachment for reservation email.")
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def _fetch_image(image_url: str, path: Path) -> None:
try:
response = requests.get(image_url, timeout=8)
response.raise_for_status()
except Exception as e:
except Exception as e: # noqa: BLE001
msg = f"Could not download image from '{image_url}': {e}"
print(msg) # noqa: T201, RUF100
return
Expand Down
2 changes: 1 addition & 1 deletion tilavarauspalvelu/utils/helauth/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def refresh_token(cls, *, user: AnyUser, session: SessionMapping) -> str | None:
if "error" in error_data and error_data["error"] == "invalid_grant":
session["keycloak_refresh_token_expired"] = True

except Exception:
except Exception: # noqa: BLE001
msg = f"Unable to refresh keycloak token for user {int(user.pk)}: {error.response.text}"
SentryLogger.log_exception(error, details=msg)

Expand Down
2 changes: 1 addition & 1 deletion tilavarauspalvelu/utils/helauth/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def from_string(cls, token: str) -> IDToken | None:

try:
payload = get_jwt_payload(token)
except Exception:
except Exception: # noqa: BLE001
return None

return cls(
Expand Down

0 comments on commit 8c44858

Please sign in to comment.