Skip to content

Commit

Permalink
Better error messages (#435)
Browse files Browse the repository at this point in the history
* Added URL to the message of HTTPError exceptions when loading an ontology
  • Loading branch information
jesper-friis authored Aug 18, 2022
1 parent bcfcbfd commit 4f6cd89
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
35 changes: 27 additions & 8 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pathlib import Path
from collections import defaultdict
from collections.abc import Iterable
from urllib.request import HTTPError

import rdflib
from rdflib.util import guess_format
Expand Down Expand Up @@ -670,14 +671,32 @@ def getmtime(path):

self.loaded = False
with open(output, "rb") as handle:
return super().load(
only_local=True,
fileobj=handle,
reload=reload,
reload_if_newer=reload_if_newer,
format="rdfxml",
**kwargs,
)
try:
return super().load(
only_local=True,
fileobj=handle,
reload=reload,
reload_if_newer=reload_if_newer,
format="rdfxml",
**kwargs,
)
except HTTPError as exc: # Add url to HTTPError message
raise HTTPError(
url=exc.url,
code=exc.code,
msg=f"{exc.url}: {exc.msg}",
hdrs=exc.hdrs,
fp=exc.fp,
).with_traceback(exc.__traceback__)

except HTTPError as exc: # Add url to HTTPError message
raise HTTPError(
url=exc.url,
code=exc.code,
msg=f"{exc.url}: {exc.msg}",
hdrs=exc.hdrs,
fp=exc.fp,
).with_traceback(exc.__traceback__)

def save(
self,
Expand Down
11 changes: 9 additions & 2 deletions ontopy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from rdflib import Graph, URIRef
from rdflib.util import guess_format
from rdflib.plugin import PluginException

import owlready2

Expand All @@ -26,9 +27,10 @@

# Format mappings: file extension -> rdflib format name
FMAP = {
"": "turtle",
"ttl": "turtle",
"n3": "ntriples",
"nt": "ntriples",
"ttl": "turtle",
"owl": "xml",
"rdfxml": "xml",
}
Expand Down Expand Up @@ -579,7 +581,12 @@ def recur(graph, outext):
)

graph = Graph()
graph.parse(input_ontology, format=fmt)
try:
graph.parse(input_ontology, format=fmt)
except PluginException as exc: # Add input_ontology to exception msg
raise PluginException(
f'Cannot load "{input_ontology}": {exc.msg}'
).with_traceback(exc.__traceback__)
graph.serialize(destination=output_ontology, format=output_format)
recur(graph, outext)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@


def test_load(repo_dir: "Path", testonto: "Ontology") -> None:
import pytest

from ontopy import get_ontology
from ontopy.ontology import HTTPError

# Check that the defaults works
emmo = get_ontology("emmo").load() # ttl format
Expand All @@ -32,6 +35,12 @@ def test_load(repo_dir: "Path", testonto: "Ontology") -> None:
).load()
assert onto.Electrolyte.prefLabel.first() == "Electrolyte"

with pytest.raises(
HTTPError,
match="HTTP Error 404: https://emmo.info/non-existing/ontology: Not Found",
):
get_ontology("http://emmo.info/non-existing/ontology#").load()


def test_load_rdfs() -> None:
from ontopy import get_ontology
Expand Down

0 comments on commit 4f6cd89

Please sign in to comment.