Skip to content

fix: replace bare except clauses with specific exception types#31

Open
AmSach wants to merge 1 commit into
robromano:masterfrom
AmSach:fix/bare-except-clauses
Open

fix: replace bare except clauses with specific exception types#31
AmSach wants to merge 1 commit into
robromano:masterfrom
AmSach:fix/bare-except-clauses

Conversation

@AmSach

@AmSach AmSach commented Jun 5, 2026

Copy link
Copy Markdown

Found two bare except: clauses in adminrestrict/middleware.py that catch BaseException (including SystemExit and KeyboardInterrupt) and any unrelated exception type, which can hide real bugs and mask programming errors.

Changes

is_valid_ipsocket.inet_pton raises OSError (or subclass) for invalid addresses; ValueError can occur for bad input. Narrowed to (OSError, ValueError).

get_ip_address_for_fqdnsocket.gethostbyname raises socket.gaierror for DNS failures, and UnicodeError/ValueError for invalid domain inputs. Narrowed to (socket.gaierror, UnicodeError, ValueError).

Diff

-        except:
+        except (OSError, ValueError):
             continue

-    except:
+    except (socket.gaierror, UnicodeError, ValueError):
         return None

This makes the error handling PEP 8 compliant (BLE001 / W0702) and prevents accidentally swallowing control-flow exceptions.

— Sent via @AmSach bot

Bare `except:` clauses catch BaseException, including SystemExit and
KeyboardInterrupt, which can hide real bugs and make the code harder to
debug. They also catch any unrelated exception type, masking programming
errors.

This change narrows the exception handlers to the specific exceptions
that the called functions can actually raise:
- `socket.inet_pton` raises OSError (or its subclass) for invalid
  addresses; ValueError can also occur with bad input.
- `socket.gethostbyname` raises socket.gaierror for DNS failures, and
  UnicodeError/ValueError for invalid domain inputs.

This makes error handling more precise and PEP 8 compliant
(BLE001 / W0702).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant