Skip to content

Commit

Permalink
Guard against formatting markdown files while mistletoe is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-the1 committed Jun 12, 2024
1 parent 9ed1fcf commit b622a65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/sqlfmt/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@

from sqlfmt.analyzer import Analyzer
from sqlfmt.cache import Cache, check_cache, clear_cache, load_cache, write_cache
from sqlfmt.exception import SqlfmtEquivalenceError, SqlfmtError, SqlfmtUnicodeError
from sqlfmt.exception import (
SqlfmtEquivalenceError,
SqlfmtError,
SqlfmtImportError,
SqlfmtUnicodeError,
)
from sqlfmt.formatter import QueryFormatter
from sqlfmt.mode import Mode as Mode
from sqlfmt.query import Query
Expand Down Expand Up @@ -61,9 +66,14 @@ def format_markdown_string(source_string: str, mode: Mode) -> str:
if mode.no_markdownfmt:
return source_string

from mistletoe import Document
from mistletoe.block_token import BlockToken, CodeFence
from mistletoe.markdown_renderer import MarkdownRenderer
try:
from mistletoe import Document
from mistletoe.block_token import BlockToken, CodeFence
from mistletoe.markdown_renderer import MarkdownRenderer
except ImportError:
raise SqlfmtImportError(
"Tried to format a Markdown file but markdownfmt extras are not installed."
)

def format_sql_code_blocks(token: BlockToken):
"""Walk through the AST and replace SQL code blocks with its formatted version."""
Expand Down
8 changes: 8 additions & 0 deletions src/sqlfmt/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class SqlfmtEquivalenceError(SqlfmtError):
pass


class SqlfmtImportError(SqlfmtError):
"""
Raised when an extra library is imported while not installed
"""

pass


class SqlfmtControlFlowException(Exception):
"""
Generic exception for exceptions used to manage control
Expand Down

0 comments on commit b622a65

Please sign in to comment.