1 Commits
Author SHA1 Message Date
dsql 733306574f fix: normalize construction-channel fetch errors; reject wrong-type channel objects at setup (dpylogger-7/8)
initialize() previously let a bad construction channel id leak raw
discord.errors.NotFound/Forbidden/HTTPException past the documented
ValueError-only setup contract, and let an already-resolved non-TextChannel
object (Thread/VoiceChannel/ForumChannel) pass silently, later misrouting or
blackholing every send. Wrap the construction-channel fetch in the same
try/except as the settings path and type-check the resolved-object channel
path, mirroring the existing int-path guard.

Widened the normalization tuple to also catch discord.ClientException
(covers InvalidData), fixing dpylogger-9 opportunistically since it's the
same pattern.

Bump to v0.1.5.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-07-02 17:19:03 -04:00
3 changed files with 37 additions and 17 deletions
+11 -9
View File
@@ -10,13 +10,13 @@ live from `bot.settings` so it can change at runtime via a command.
`requirements.txt`: `requirements.txt`:
``` ```
dpy_logger @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_logger.git@v0.1.4 dpy_logger @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_logger.git@v0.1.5
``` ```
Direct: Direct:
```bash ```bash
pip install "dpy_logger @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_logger.git@v0.1.4" pip install "dpy_logger @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_logger.git@v0.1.5"
``` ```
Requires `discord.py` (pulled transitively). Requires `discord.py` (pulled transitively).
@@ -72,13 +72,15 @@ await bot.log.debug("noisy", log_to_file=False) # -> Discord only
## Errors ## Errors
Resolution failures (unresolvable guild/channel, bad config, a non-text channel) raise Resolution failures (unresolvable guild/channel, bad config, a non-text channel — whether
`ValueError` from `initialize()` a misconfigured logger should fail loudly at setup. passed as an id or as an already-resolved object) raise `ValueError` from `initialize()`
Underlying `discord` exceptions (`NotFound` / `Forbidden` / `HTTPException`) from a misconfigured logger should fail loudly at setup. Underlying `discord` exceptions
`fetch_guild`/`fetch_channel` are normalized to that `ValueError` so callers see one (`NotFound` / `Forbidden` / `HTTPException` / `InvalidData` and other `ClientException`
error type. On a **per-call** send, neither resolution nor send failures propagate: they subclasses) from `fetch_guild`/`fetch_channel` are normalized to that `ValueError` on every
fall back to the stdlib logger so a transient Discord failure (or a per-call `guild=` resolution path (construction channel, per-guild settings lookup) so callers see one error
that doesn't resolve) never breaks the caller's command. type. On a **per-call** send, neither resolution nor send failures propagate: they fall
back to the stdlib logger so a transient Discord failure (or a per-call `guild=` that
doesn't resolve) never breaks the caller's command.
## Construction contract ## Construction contract
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "dpy_logger" name = "dpy_logger"
version = "0.1.4" version = "0.1.5"
description = "Leveled Discord channel logger for discord.py — config-free, injectable, installable." description = "Leveled Discord channel logger for discord.py — config-free, injectable, installable."
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [
+24 -6
View File
@@ -40,11 +40,15 @@ happens before the discord send, so the record survives even if discord fails.
extending: this base has no feed/announcement method by design. a project extending: this base has no feed/announcement method by design. a project
that wants one subclasses DPYLogger and adds it, reusing _resolve. that wants one subclasses DPYLogger and adds it, reusing _resolve.
errors: resolution failures (unresolvable guild/channel, bad config) raise errors: resolution failures (unresolvable guild/channel, bad config, a
non-text channel passed as either an id or an already-resolved object) raise
ValueError from `initialize()` — a misconfigured logger should fail loudly at ValueError from `initialize()` — a misconfigured logger should fail loudly at
setup. on a per-call send, resolution AND send failures do NOT propagate: they setup. underlying discord exceptions (NotFound/Forbidden/HTTPException/
fall back to the stdlib logger so a transient discord failure (or a per-call InvalidData and other ClientException subclasses) are normalized to that
`guild=` that doesn't resolve) never breaks the caller's command. ValueError on every resolution path. on a per-call send, resolution AND send
failures do NOT propagate: they fall back to the stdlib logger so a transient
discord failure (or a per-call `guild=` that doesn't resolve) never breaks the
caller's command.
""" """
import logging import logging
@@ -119,6 +123,12 @@ class DPYLogger:
self.guild = self.channel.guild self.guild = self.channel.guild
if self.guild is None and self.channel is not None: if self.guild is None and self.channel is not None:
raise ValueError("[dpy_logger] channel provided without a resolvable guild") raise ValueError("[dpy_logger] channel provided without a resolvable guild")
# a resolved wrong-type object (Thread/VoiceChannel/ForumChannel) would also pass
# setup silently — _get_channel only recognizes TextChannel/int, so anything else
# falls through to the bot.settings lookup at send time and misroutes or blackholes
# the sink. fail loud here, mirroring the int-path check in _get_channel.
if self.channel is not None and not isinstance(self.channel, (discord.TextChannel, int)):
raise ValueError(f"[dpy_logger] channel {self.channel!r} is not a text channel")
async def _get_guild(self, guild): async def _get_guild(self, guild):
"""resolve a guild from id-or-object, raising if unresolvable""" """resolve a guild from id-or-object, raising if unresolvable"""
@@ -147,7 +157,14 @@ class DPYLogger:
if isinstance(self.channel, discord.TextChannel): if isinstance(self.channel, discord.TextChannel):
return self.channel return self.channel
if isinstance(self.channel, int): if isinstance(self.channel, int):
# fetch_channel raises NotFound/Forbidden/HTTPException/InvalidData (a
# ClientException, not an HTTPException); normalize all of those to the
# lib's ValueError so a bad construction channel id fails loud with one
# error type, matching the settings path below
try:
channel = await guild.fetch_channel(self.channel) channel = await guild.fetch_channel(self.channel)
except (discord.HTTPException, discord.ClientException) as error:
raise ValueError(f"[dpy_logger] could not fetch channel {self.channel}: {error}") from error
if not isinstance(channel, discord.TextChannel): if not isinstance(channel, discord.TextChannel):
# fetch_channel can return a Voice/Category/Forum channel; fail loud # fetch_channel can return a Voice/Category/Forum channel; fail loud
# at setup like the settings path, not later via an AttributeError on .send # at setup like the settings path, not later via an AttributeError on .send
@@ -160,8 +177,9 @@ class DPYLogger:
raise ValueError(f"[dpy_logger] no log channel configured for guild {guild.id}") raise ValueError(f"[dpy_logger] no log channel configured for guild {guild.id}")
try: try:
channel = await guild.fetch_channel(channel_id) channel = await guild.fetch_channel(channel_id)
except discord.HTTPException as error: except (discord.HTTPException, discord.ClientException) as error:
# fetch_channel raises NotFound/Forbidden/HTTPException; normalize to the # fetch_channel raises NotFound/Forbidden/HTTPException/InvalidData (a
# ClientException, not an HTTPException); normalize all of those to the
# lib's ValueError so a bad configured id fails loud with one error type # lib's ValueError so a bad configured id fails loud with one error type
raise ValueError(f"[dpy_logger] could not fetch channel {channel_id}: {error}") from error raise ValueError(f"[dpy_logger] could not fetch channel {channel_id}: {error}") from error
if not isinstance(channel, discord.TextChannel): if not isinstance(channel, discord.TextChannel):