no-str-exception-translation

Forbid translating exceptions with str(exc) messages that discard typed context.

Message

Do not translate exceptions by passing str(exc); use a stable message and chain the cause.

Valid examples

try:
    run()
except ValueError as exc:
    raise CommandArgumentError("Invalid resource identifier.") from exc
message = str(value)
Show more
try:
    run()
except ValueError as exc:
    str = lambda value: "fixed"
    raise RuntimeError(str(exc)) from exc

Invalid examples

try:
    run()
except ValueError as exc:
    raise CommandArgumentError(str(exc)) from exc
Show more
try:
    run()
except ValueError as error:
    raise RuntimeError(str(error)) from error
try:
    run()
except ValueError as exc:
    raise RuntimeError(message=str(exc)) from exc
try:
    run()
except ValueError as exc:
    raise RuntimeError(f"{exc}") from exc
try:
    run()
except ValueError as exc:
    raise RuntimeError("{}".format(exc)) from exc
try:
    run()
except ValueError as exc:
    raise RuntimeError("%s" % exc) from exc
try:
    run()
except ValueError as exc:
    raise RuntimeError("%s" % (exc,)) from exc
import builtins

try:
    run()
except ValueError as exc:
    raise RuntimeError(builtins.str(exc)) from exc
import builtins as builtin_values

try:
    run()
except ValueError as exc:
    raise RuntimeError(builtin_values.str(exc)) from exc
from builtins import str as stringify

try:
    run()
except ValueError as exc:
    raise RuntimeError(stringify(exc)) from exc