74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
"""API error taxonomy; every message is safe to return to a client."""
|
|
|
|
|
|
class ApiError(Exception):
|
|
"""Base class for errors that map onto the public error payload."""
|
|
|
|
status = 500
|
|
code = 'internal_error'
|
|
default_message = 'Внутренняя ошибка сервера'
|
|
|
|
def __init__(self, message=None):
|
|
self.message = message or self.default_message
|
|
super().__init__(self.message)
|
|
|
|
|
|
class InvalidRequest(ApiError):
|
|
status = 400
|
|
code = 'invalid_request'
|
|
default_message = 'Некорректный запрос'
|
|
|
|
|
|
class Unauthorized(ApiError):
|
|
status = 401
|
|
code = 'unauthorized'
|
|
default_message = 'Требуется идентификация пользователя'
|
|
www_authenticate = None
|
|
|
|
def __init__(self, message=None, www_authenticate=None):
|
|
self.message = message or self.default_message
|
|
self.www_authenticate = www_authenticate
|
|
super().__init__(self.message)
|
|
|
|
|
|
class NotFound(ApiError):
|
|
status = 404
|
|
code = 'not_found'
|
|
default_message = 'Объект не найден'
|
|
|
|
|
|
class Conflict(ApiError):
|
|
status = 409
|
|
code = 'conflict'
|
|
default_message = 'Конфликт состояния'
|
|
|
|
|
|
class PayloadTooLarge(ApiError):
|
|
status = 413
|
|
code = 'payload_too_large'
|
|
default_message = 'Тело запроса слишком велико'
|
|
|
|
|
|
class UnsupportedMediaType(ApiError):
|
|
status = 415
|
|
code = 'unsupported_media_type'
|
|
default_message = 'Поддерживается только application/json'
|
|
|
|
|
|
class RateLimited(ApiError):
|
|
status = 429
|
|
code = 'rate_limited'
|
|
default_message = 'Слишком много запросов'
|
|
|
|
|
|
class Unavailable(ApiError):
|
|
status = 503
|
|
code = 'unavailable'
|
|
default_message = 'Сервис временно недоступен'
|
|
|
|
|
|
class InternalError(ApiError):
|
|
status = 500
|
|
code = 'internal_error'
|
|
default_message = 'Внутренняя ошибка сервера'
|