Errors

Every error svgwriter raises on purpose lives in svgwriter.errors (svg.errors.ThemeError, …). Each subclasses the built-in exception a caller would expect, so except ValueError keeps working.

Every error svgwriter raises on purpose, in one place: svg.errors.<Name>.

Each subclasses the built-in exception a caller would expect (ValueError for bad values, KeyError for failed lookups, AttributeError and TypeError for misspelled names), so except ValueError keeps working.

Examples

>>> import svgwriter as svg
>>> try:
...     svg.themes.get("drak")
... except svg.errors.UnknownNameError as error:
...     print(type(error).__module__, error)
svgwriter.errors unknown theme 'drak'; did you mean 'dark'?
exception DiagramToolError

Bases: RuntimeError

A diagram tool (Graphviz, Mermaid, PlantUML, D2, LaTeX) is missing or rejected the input.

exception FFmpegError

Bases: RuntimeError

ffmpeg ran but failed; the message carries the exit status and the tail of its error output.

exception FFmpegNotFoundError

Bases: RuntimeError

ffmpeg is not installed or not on PATH.

exception OfficeExtraError

Bases: ImportError

The library for an Office format is not installed (pip install "pysvgwriter[office]").

exception PathSyntaxError(message: str, position: int)

Bases: ValueError

Malformed path data; records where in the text parsing failed.

Parameters:
  • message (str) – What went wrong.

  • position (int) – Character offset of the error; also stored as the position attribute.

Examples

>>> import svgwriter as svg
>>> try:
...     svg.PathData("L1 1")
... except svg.errors.PathSyntaxError as error:
...     print(error)
path data must start with a moveto at offset 0
exception ProjectError

Bases: ValueError

A project file is missing or invalid, or its entry cannot be loaded; the message names the file.

Examples

>>> import svgwriter as svg
>>> issubclass(svg.errors.ProjectError, ValueError)
True
exception SVGParseError

Bases: ValueError

Malformed SVG input or, when strict, unsupported content.

Examples

>>> import svgwriter as svg
>>> try:
...     svg.parse("<svg")
... except svg.errors.SVGParseError:
...     print("malformed")
malformed
exception SceneError

Bases: ValueError

A source defines no scene, or no scene with a requested name.

Examples

>>> import svgwriter as svg
>>> issubclass(svg.errors.SceneError, ValueError)
True
exception ThemeError

Bases: ValueError

A theme that cannot be resolved: an unknown role or reference, a cycle, or a malformed value.

Examples

>>> import svgwriter as svg
>>> try:
...     svg.theme.Palette({"a": "{palette.b}"})
... except svg.errors.ThemeError as error:
...     print(error)
palette.a: unknown reference {palette.b}
exception UnknownAttributeError(owner: str, name: str, members: type | None = None)

Bases: AttributeError

An attribute that is neither a Python member nor a known SVG attribute.

The “did you mean” suggestion is computed only when the message is shown, so getattr(element, name, default) stays cheap.

Parameters:
  • owner (str) – The class name of the element.

  • name (str) – The attribute that was requested.

  • members (type, optional) – The class whose public members are suggested too. Defaults to None.

Examples

>>> import svgwriter as svg
>>> try:
...     svg.rect(0, 0, 1, 1).widht
... except AttributeError as error:
...     print("did you mean 'width'?" in str(error))
True
>>> try:
...     svg.text("A").fade_out()
... except AttributeError as error:
...     print(str(error).split(": ", 1)[1])
fade_out is an animation; use element.animate.fade_out(...)
exception UnknownKeywordError(owner: str, name: str, members: type | None = None)

Bases: UnknownAttributeError, TypeError

An unknown keyword argument (likely a typo); custom attributes go in attrs={...}.

Parameters:
  • owner (str) – The class name of the element.

  • name (str) – The keyword that was given.

Examples

>>> import svgwriter as svg
>>> try:
...     svg.rect(0, 0, 1, 1, widht=3)
... except TypeError as error:
...     print(type(error).__name__)
UnknownKeywordError
exception UnknownNameError

Bases: KeyError, ValueError

A name that no registry entry has; the message suggests the closest names.

It is both a KeyError (a failed lookup) and a ValueError (a bad argument value), so either except catches it.

Examples

>>> import svgwriter as svg
>>> isinstance(svg.errors.UnknownNameError("x"), (KeyError, ValueError))
True