20 lines
731 B
Python
20 lines
731 B
Python
"""API errors that map onto the contract body. Details never include SQL or secrets."""
|
|
from __future__ import annotations
|
|
|
|
|
|
class ApiError(Exception):
|
|
def __init__(self, status: int, code: str, message: str, headers: dict | None = None):
|
|
super().__init__(message)
|
|
self.status = status
|
|
self.code = code
|
|
self.message = message
|
|
self.headers = headers or {}
|
|
|
|
|
|
def unauthorized(kind: str) -> ApiError:
|
|
if kind == "basic":
|
|
header = 'Basic realm="messenger", charset="UTF-8"'
|
|
else:
|
|
header = 'Bearer realm="messenger"'
|
|
return ApiError(401, "unauthorized", "Требуется действительная аутентификация", {"WWW-Authenticate": header})
|