• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-data-services / 10281053350

07 Aug 2024 08:41AM UTC coverage: 90.451% (-0.02%) from 90.467%
10281053350

Pull #340

github

web-flow
Merge 381662897 into d92932700
Pull Request #340: test: fix schemathesis query parameters test

124 of 124 new or added lines in 18 files covered. (100.0%)

9 existing lines in 4 files now uncovered.

9084 of 10043 relevant lines covered (90.45%)

1.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.03
/components/renku_data_services/base_api/error_handler.py
1
"""The error handler for the application."""
2✔
2

3
import os
2✔
4
import sys
2✔
5
import traceback
2✔
6
from collections.abc import Mapping, Set
2✔
7
from sqlite3 import Error as SqliteError
2✔
8
from typing import Any, Optional, Protocol, TypeVar, Union
2✔
9

10
from asyncpg import exceptions as postgres_exceptions
2✔
11
from pydantic import ValidationError as PydanticValidationError
2✔
12
from sanic import HTTPResponse, Request, SanicException, json
2✔
13
from sanic.errorpages import BaseRenderer, TextRenderer
2✔
14
from sanic.handlers import ErrorHandler
2✔
15
from sanic_ext.exceptions import ValidationError
2✔
16
from sqlalchemy.exc import SQLAlchemyError
2✔
17

18
from renku_data_services import errors
2✔
19

20

21
class BaseError(Protocol):
2✔
22
    """Protocol for the error type of an apispec module."""
2✔
23

24
    code: int
2✔
25
    message: str
2✔
26
    detail: Optional[str]
2✔
27
    quiet: bool
2✔
28

29

30
class BaseErrorResponse(Protocol):
2✔
31
    """Porotocol for the error response class of an apispec module."""
2✔
32

33
    error: BaseError
2✔
34

35
    def dict(
2✔
36
        self,
37
        *,
38
        include: Optional[Union[Set[Union[int, str]], Mapping[Union[int, str], Any]]] = None,
39
        exclude: Optional[Union[Set[Union[int, str]], Mapping[Union[int, str], Any]]] = None,
40
        by_alias: bool = False,
41
        skip_defaults: Optional[bool] = None,
42
        exclude_unset: bool = False,
43
        exclude_defaults: bool = False,
44
        exclude_none: bool = False,
45
    ) -> dict[str, Any]:
46
        """Turn the response to dict."""
47
        ...
×
48

49

50
BError = TypeVar("BError", bound=BaseError)
2✔
51
BErrorResponse = TypeVar("BErrorResponse", bound=BaseErrorResponse)
2✔
52

53

54
class ApiSpec(Protocol[BErrorResponse, BError]):
2✔
55
    """Protocol for an apispec with error data."""
2✔
56

57
    ErrorResponse: BErrorResponse
2✔
58
    Error: BError
2✔
59

60

61
class CustomErrorHandler(ErrorHandler):
2✔
62
    """Central error handling."""
2✔
63

64
    def __init__(self, api_spec: ApiSpec, base: type[BaseRenderer] = TextRenderer) -> None:
2✔
65
        self.api_spec = api_spec
2✔
66
        super().__init__(base)
2✔
67

68
    def default(self, request: Request, exception: Exception) -> HTTPResponse:
2✔
69
        """Overrides the default error handler."""
70
        formatted_exception = errors.BaseError()
2✔
71
        match exception:
2✔
72
            case errors.BaseError():
2✔
73
                formatted_exception = exception
2✔
74
            case ValidationError():
2✔
75
                extra_exception = None if exception.extra is None else exception.extra["exception"]
2✔
76
                match extra_exception:
2✔
77
                    case TypeError():
2✔
78
                        formatted_exception = errors.ValidationError(
2✔
79
                            message="The validation failed because the provided input has the wrong type"
80
                        )
81
                    case PydanticValidationError():
2✔
82
                        parts = [
2✔
83
                            ".".join(str(i) for i in field["loc"]) + ": " + field["msg"]
84
                            for field in extra_exception.errors()
85
                        ]
86
                        message = f"There are errors in the following fields, {', '.join(parts)}"
2✔
87
                        formatted_exception = errors.ValidationError(message=message)
2✔
88
            case SanicException():
2✔
89
                message = exception.message
2✔
90
                if message == "" or message is None:
2✔
91
                    message = ", ".join([str(i) for i in exception.args])
×
92
                formatted_exception = errors.BaseError(
2✔
93
                    message=message,
94
                    status_code=exception.status_code,
95
                    code=1000 + exception.status_code,
96
                    quiet=exception.quiet or False,
97
                )
98
            case SqliteError():
2✔
99
                formatted_exception = errors.BaseError(
×
100
                    message=f"Database error occurred: {exception.sqlite_errorname}",
101
                    detail=f"Error code: {exception.sqlite_errorcode}",
102
                )
103
            case postgres_exceptions.PostgresError():
2✔
104
                formatted_exception = errors.BaseError(
×
105
                    message=f"Database error occurred: {exception.msg}", detail=f"Error code: {exception.pgcode}"
106
                )
107
            case SQLAlchemyError():
2✔
108
                message = ", ".join([str(i) for i in exception.args])
1✔
109
                if "CharacterNotInRepertoireError" in message:
1✔
110
                    # NOTE: This message is usually triggered if a string field for the database contains
111
                    # NULL - i.e \u0000 or other invalid characters that are not UTF-8 compatible
112
                    formatted_exception = errors.ValidationError(
×
113
                        message="The payload contains characters that are incompatible with the database",
114
                        detail=message,
115
                    )
116
                elif "value out of int32 range" in message:
1✔
UNCOV
117
                    formatted_exception = errors.ValidationError(
×
118
                        message="The payload contains integers with values that are "
119
                        "too large or small for the database",
120
                        detail=message,
121
                    )
122
                else:
123
                    formatted_exception = errors.BaseError(message=f"Database error occurred: {message}")
1✔
124
            case PydanticValidationError():
2✔
125
                parts = [".".join(str(i) for i in field["loc"]) + ": " + field["msg"] for field in exception.errors()]
2✔
126
                message = f"There are errors in the following fields, {', '.join(parts)}"
2✔
127
                formatted_exception = errors.ValidationError(message=message)
2✔
128
            case OverflowError():
1✔
129
                formatted_exception = errors.ValidationError(
×
130
                    message="The provided input is too large to be stored in the database"
131
                )
132
        self.log(request, formatted_exception)
2✔
133
        if formatted_exception.status_code == 500 and "PYTEST_CURRENT_TEST" in os.environ:
2✔
134
            # TODO: Figure out how to do logging properly in here, I could not get the sanic logs to show up from here
135
            # at all when running schemathesis. So 500 errors are hard to debug but print statements do show up.
136
            # The above log statement does not show up in the logs that pytest shows after a test is done.
137
            sys.stderr.write(f"A 500 error was raised because of {type(exception)} on request {request}\n")
1✔
138
            traceback.print_exception(exception)
1✔
139
        return json(
2✔
140
            self.api_spec.ErrorResponse(
141
                error=self.api_spec.Error(
142
                    code=formatted_exception.code,
143
                    message=formatted_exception.message,
144
                    detail=formatted_exception.detail,
145
                )
146
            ).model_dump(exclude_none=True),
147
            status=formatted_exception.status_code,
148
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc