Spaces:
Running
Running
File size: 1,104 Bytes
33e8bbc 371613f 33e8bbc 371613f 33e8bbc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | class ApiError(Exception):
"""An error with an HTTP status and a stable machine-readable code."""
def __init__(self, status: int, code: str, message: str, headers: dict | None = None, details: dict | None = None):
super().__init__(message)
self.status = status
self.code = code
self.message = message
self.headers = headers or {}
self.details = details
def not_found(what: str = "not found") -> ApiError:
# Used both for "missing" and "exists but you may not see it", so callers
# cannot probe for channels or agents outside their grants.
return ApiError(404, "not_found", what)
def forbidden(message: str = "not allowed") -> ApiError:
return ApiError(403, "forbidden", message)
def bad_request(message: str) -> ApiError:
return ApiError(400, "invalid_request", message)
def conflict(code: str, message: str) -> ApiError:
return ApiError(409, code, message)
def unauthorized(message: str = "invalid or missing token") -> ApiError:
return ApiError(401, "unauthorized", message, {"WWW-Authenticate": "Bearer"})
|