How to use the blacksheep.server.normalization.NormalizationError function in blacksheep

To help you get started, we’ve selected a few blacksheep examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RobertoPrevato / BlackSheep / blacksheep / server / normalization.py View on Github external
def _check_union(parameter, annotation, method):
    """Checks if the given annotation is Optional[] - in such case unwraps it and returns its value

    An exception is thrown if other kinds of Union[] are used, since they are not supported by method normalization.
    In such case, the user of the library should read the desired value from the request object.
    """

    if hasattr(annotation, '__origin__') and annotation.__origin__ is Union:
        # support only Union[None, Type] - that is equivalent of Optional[Type]
        if type(None) not in annotation.__args__ or len(annotation.__args__) > 2:
            raise NormalizationError(f'Unsupported parameter type "{parameter.name}" for method "{method.__name__}"; '
                                     f'only Optional types are supported for automatic binding. '
                                     f'Read the desired value from the request itself.')

        for possible_type in annotation.__args__:
            if type(None) is possible_type:
                continue
            return True, possible_type
    return False, annotation
github RobertoPrevato / BlackSheep / blacksheep / server / normalization.py View on Github external
class UnsupportedSignatureError(NormalizationError):

    def __init__(self, method):
        super().__init__(f'Cannot normalize method `{method.__qualname__}` because its signature contains '
                         f'*args or *kwargs parameters. If you use a decorator, please use `functools.@wraps` '
                         f'with your wrapper, to fix this error.')


class MultipleFromBodyBinders(NormalizationError):

    def __init__(self, method, first_match, new_match):
        super().__init__(f'Cannot use more than one `FromBody` binder for the same method ({method.__qualname__}). '
                         f'The first match was: {first_match}, a second one {new_match}.')


class AmbiguousMethodSignatureError(NormalizationError):

    def __init__(self, method):
        super().__init__(f'Cannot normalize method `{method.__qualname__}` due to its ambiguous signature. '
                         f'Please specify exact binders for its arguments.')


class RouteBinderMismatch(NormalizationError):

    def __init__(self, parameter_name, route):
        super().__init__(f'The parameter {parameter_name} for method {route.handler.__name__} is bound to route path, '
                         f'but the route doesn`t contain a parameter with matching name.')


def get_from_body_parameter(method) -> FromBody:
    """Extracts a single FromBody parameter from the given signature,
    throwing exception if more than one is defined."""
github RobertoPrevato / BlackSheep / blacksheep / server / normalization.py View on Github external
FromBody,
                       RequestBinder,
                       ExactBinder,
                       IdentityBinder)
from guardpost.authentication import User, Identity
from blacksheep.normalization import copy_special_attributes


_next_handler_binder = object()


class NormalizationError(Exception):
    ...


class UnsupportedSignatureError(NormalizationError):

    def __init__(self, method):
        super().__init__(f'Cannot normalize method `{method.__qualname__}` because its signature contains '
                         f'*args or *kwargs parameters. If you use a decorator, please use `functools.@wraps` '
                         f'with your wrapper, to fix this error.')


class MultipleFromBodyBinders(NormalizationError):

    def __init__(self, method, first_match, new_match):
        super().__init__(f'Cannot use more than one `FromBody` binder for the same method ({method.__qualname__}). '
                         f'The first match was: {first_match}, a second one {new_match}.')


class AmbiguousMethodSignatureError(NormalizationError):
github RobertoPrevato / BlackSheep / blacksheep / server / normalization.py View on Github external
class MultipleFromBodyBinders(NormalizationError):

    def __init__(self, method, first_match, new_match):
        super().__init__(f'Cannot use more than one `FromBody` binder for the same method ({method.__qualname__}). '
                         f'The first match was: {first_match}, a second one {new_match}.')


class AmbiguousMethodSignatureError(NormalizationError):

    def __init__(self, method):
        super().__init__(f'Cannot normalize method `{method.__qualname__}` due to its ambiguous signature. '
                         f'Please specify exact binders for its arguments.')


class RouteBinderMismatch(NormalizationError):

    def __init__(self, parameter_name, route):
        super().__init__(f'The parameter {parameter_name} for method {route.handler.__name__} is bound to route path, '
                         f'but the route doesn`t contain a parameter with matching name.')


def get_from_body_parameter(method) -> FromBody:
    """Extracts a single FromBody parameter from the given signature,
    throwing exception if more than one is defined."""
    sig = Signature.from_callable(method)

    from_body_parameter = None

    for name, parameter in sig.parameters.items():
        if isinstance(parameter.annotation, FromBody):
            if from_body_parameter is None: