Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled exception when checking if URL exists #101 #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions mermaid2/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from bs4 import BeautifulSoup

from . import pyjs
from .util import info, libname, url_exists
from .util import info, libname, url_exists, critical


# ------------------------
Expand Down Expand Up @@ -42,6 +42,7 @@ class MarkdownMermaidPlugin(BasePlugin):

('version', PluginType(str, default=JAVASCRIPT_VERSION)),
('javascript', PluginType(str, default=None)),
('allow_inaccessible_url', PluginType(bool, default=False)),
('arguments', PluginType(dict, default={})),
# ('custom_loader', PluginType(bool, default=False))
)
Expand Down Expand Up @@ -140,8 +141,11 @@ def javascript(self) -> str:
# make checks
if not url_exists(javascript,
local_base_dir=self.full_config['docs_dir']):
raise FileNotFoundError("Cannot find Mermaid library: %s" %
critical("Cannot find Mermaid library: %s" %
javascript)
if not self.config['allow_inaccessible_url']:
raise FileNotFoundError("Cannot find Mermaid library: %s" %
javascript)
self._javascript = javascript
return self._javascript

Expand Down
30 changes: 27 additions & 3 deletions mermaid2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ def info(*args) -> str:
args = [MERMAID_LABEL] + [str(arg) for arg in args]
msg = ' '.join(args)
log.info(msg)


def warning(*args) -> str:
"Write information on the console, preceded by the signature label"
args = [MERMAID_LABEL] + [str(arg) for arg in args]
msg = ' '.join(args)
log.warning(msg)

def critical(*args) -> str:
"Write information on the console, preceded by the signature label"
args = [MERMAID_LABEL] + [str(arg) for arg in args]
msg = ' '.join(args)
log.critical(msg)

# -------------------
# Paths and URLs
# -------------------
Expand All @@ -45,8 +57,20 @@ def libname(lib:str) -> str:
def url_exists(url:str, local_base_dir:str='') -> bool:
"Checks that a url exists"
if url.startswith('http'):
request = requests.get(url)
return request.status_code == 200
# requests can fail without HTTP code, list:
# https://docs.python-requests.org/en/latest/_modules/requests/exceptions/
try:
request = requests.get(url)
except Exception as ue:
if ue.__class__.__module__ == "requests.exceptions":
warning("Exception when making a GET request: %s" % ue)
return False
# Returning the rest
else:
raise
else:
return request.status_code == 200

else:
pathname = os.path.join(local_base_dir, url)
return os.path.exists(pathname)