New NotAnActivityError error

pull/14/head
Thomas Sileo 2018-07-22 11:18:27 +02:00
rodzic 66d086f95f
commit 855aba00f5
3 zmienionych plików z 18 dodań i 3 usunięć

Wyświetl plik

@ -37,7 +37,11 @@ AS_PUBLIC = "https://www.w3.org/ns/activitystreams#Public"
COLLECTION_CTX = [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{"Hashtag": "as:Hashtag", "sensitive": "as:sensitive"},
{
"Hashtag": "as:Hashtag",
"sensitive": "as:sensitive",
"toot": "http://joinmastodon.org/ns#",
},
]
# Will be used to keep track of all the defined activities

Wyświetl plik

@ -14,6 +14,7 @@ from .__version__ import __version__
from .collection import parse_collection
from .errors import ActivityGoneError
from .errors import ActivityNotFoundError
from .errors import NotAnActivityError
from .errors import ActivityUnavailableError
from .urlutils import URLLookupFailedError
from .urlutils import check_url as check_url
@ -75,6 +76,9 @@ class Backend(abc.ABC):
pass # pragma: no cover
def fetch_iri(self, iri: str, **kwargs) -> "ap.ObjectType": # pragma: no cover
if not iri.startswith('http'):
raise NotAnActivityError(f"{iri} is not a valid IRI")
try:
self.check_url(iri)
except URLLookupFailedError:
@ -111,9 +115,9 @@ class Backend(abc.ABC):
try:
out = resp.json()
except json.JSONDecodeError:
except (json.JSONDecodeError, ValueError):
# TODO(tsileo): a special error type?
raise ActivityUnavailableError(f"{iri} is not JSON")
raise NotAnActivityError(f"{iri} is not JSON")
return out

Wyświetl plik

@ -83,3 +83,10 @@ class ActivityUnavailableError(ServerError):
"""Raises when fetching a remote activity times out."""
status_code = 503
class NotAnActivityError(ServerError):
"""Raised when no JSON can be decoded.
Most likely raised when stumbling upon a OStatus notice or failed lookup.
"""