Handle nodeinfo validation errors (#439)

This commit is contained in:
Michael Manfre 2023-01-18 12:57:28 -05:00 committed by GitHub
parent 0d115bac15
commit e5485b1430
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 7 deletions

View file

@ -16,26 +16,30 @@ class ActorMismatchError(ActivityPubError):
"""
def capture_message(message: str):
def capture_message(message: str, level: str | None = None, scope=None, **scope_args):
"""
Sends the informational message to Sentry if it's configured
"""
if settings.SETUP.SENTRY_DSN and settings.SETUP.SENTRY_CAPTURE_MESSAGES:
from sentry_sdk import capture_message
capture_message(message)
capture_message(message, level, scope, **scope_args)
elif settings.DEBUG:
if scope or scope_args:
message += f"; {scope=}, {scope_args=}"
print(message)
def capture_exception(exception: BaseException):
def capture_exception(
exception: BaseException, level: str | None = None, scope=None, **scope_args
):
"""
Sends the exception to Sentry if it's configured
"""
if settings.SETUP.SENTRY_DSN:
from sentry_sdk import capture_exception
capture_exception(exception)
capture_exception(exception, level, scope, **scope_args)
elif settings.DEBUG:
traceback.print_exc()

View file

@ -3,6 +3,7 @@ import ssl
from typing import Optional
import httpx
import pydantic
import urlman
from asgiref.sync import sync_to_async
from django.conf import settings
@ -202,9 +203,13 @@ class Domain(StatorModel):
try:
info = NodeInfo(**response.json())
except json.JSONDecodeError as ex:
except (json.JSONDecodeError, pydantic.ValidationError) as ex:
capture_message(
f"Client error decoding nodeinfo: domain={self.domain}, error={str(ex)}"
f"Client error decoding nodeinfo: {str(ex)}",
extra={
"domain": self.domain,
"nodeinfo20_url": nodeinfo20_url,
},
)
return None
return info

View file

@ -14,7 +14,7 @@ class NodeInfoSoftware(BaseModel):
class NodeInfoUsage(BaseModel):
users: dict[str, int] | None
users: dict[str, int | None] | None
local_posts: int = Field(default=0, alias="localPosts")