Improved error handling on social plugin (#6818)

* fix(social): CairoSVG OSError handling in social plugin

Related issue: #6817

Co-authored-by: Guts <1596222+Guts@users.noreply.github.com>

* feat(docs): Add troubleshooting guide for CairoSVG crash

---------

Co-authored-by: Kamil Krzyśków <kamilzary@gmail.com>
Co-authored-by: Guts <1596222+Guts@users.noreply.github.com>
Co-authored-by: Martin Donath <martin.donath@squidfunk.com>
This commit is contained in:
Julien
2024-03-31 11:00:27 +02:00
committed by GitHub
parent abfac1a93e
commit a2cb35d4c5
6 changed files with 369 additions and 16 deletions

View File

@@ -49,14 +49,25 @@ from mkdocs.plugins import BasePlugin
from mkdocs.utils import copy_file
from shutil import copyfile
from tempfile import NamedTemporaryFile
try:
from cairosvg import svg2png
from PIL import Image, ImageDraw, ImageFont
except ImportError:
pass
from .config import SocialConfig
try:
from PIL import Image, ImageDraw, ImageFont
except ImportError as e:
import_errors = {repr(e)}
else:
import_errors = set()
cairosvg_error: str = ""
try:
from cairosvg import svg2png
except ImportError as e:
import_errors.add(repr(e))
except OSError as e:
cairosvg_error = str(e)
# -----------------------------------------------------------------------------
# Classes
@@ -76,10 +87,18 @@ class SocialPlugin(BasePlugin[SocialConfig]):
return
# Check dependencies
if "Image" not in globals():
if import_errors:
raise PluginError(
"Required dependencies of \"social\" plugin not found. "
"Install with: pip install \"mkdocs-material[imaging]\""
"Required dependencies of \"social\" plugin not found:\n"
+ str("\n".join(map(lambda x: "- " + x, import_errors)))
+ "\n\n--> Install with: pip install \"mkdocs-material[imaging]\""
)
if cairosvg_error:
raise PluginError(
"\"cairosvg\" Python module is installed, but it crashed with:\n"
+ cairosvg_error
+ "\n\n--> Check out the troubleshooting guide: https://t.ly/MfX6u"
)
# Move color options