Files

371 lines
13 KiB
Python
Raw Permalink Normal View History

import logging
from collections import OrderedDict
from http import HTTPStatus
from typing import NoReturn
from flask import current_app, got_request_exception
from werkzeug.exceptions import HTTPException
from core.app.apps.base_app_generate_response_converter import (
ModerationRejectedHTTPError,
ModerationUnavailableHTTPError,
moderation_http_exception,
)
from core.logging.context import get_request_id
from libs.exception import BaseHTTPException
from libs.external_api import ExternalApi
from services.annotation.errors import AnnotationResourceNotFoundError
from services.credentials.refresh import CredentialRefreshUnavailableError
from services.errors.base import NoPermissionError
from services.errors.dataset import DatasetNotFoundError
from services.errors.document import DocumentAccessDeniedError, DocumentNotFoundError, DocumentSourceNotFoundError
from services.moderation_invocation_errors import (
ModerationRejectedInvocationError,
ModerationUnavailableInvocationError,
)
2024-11-01 15:51:22 +08:00
class PaidPlanRequiredError(BaseHTTPException):
error_code = "paid_plan_required"
description = "This feature requires a paid plan."
code = 403
2024-11-01 15:51:22 +08:00
class FilenameNotExistsError(BaseHTTPException):
error_code = "filename_not_exists_error"
2024-11-01 15:51:22 +08:00
code = 400
description = "The specified filename does not exist."
class RemoteFileUploadError(BaseHTTPException):
error_code = "remote_file_upload_error"
code = 400
description = "Error uploading remote file."
class RemoteFileInvalidUrlError(BaseHTTPException):
error_code = "remote_file_invalid_url"
description = "The remote file URL is invalid."
code = 400
class RemoteFileUrlBlockedError(BaseHTTPException):
error_code = "remote_file_url_blocked"
description = "The remote file URL is not allowed."
code = 400
class RemoteFileNotFoundError(BaseHTTPException):
error_code = "remote_file_not_found"
description = "The remote file could not be found."
code = 404
class RemoteFileAccessDeniedError(BaseHTTPException):
error_code = "remote_file_access_denied"
description = "The remote file cannot be accessed without authorization."
code = 400
class RemoteFileUnavailableError(BaseHTTPException):
error_code = "remote_file_unavailable"
description = "The remote file is temporarily unavailable."
code = 502
class RemoteFileInvalidResponseError(BaseHTTPException):
error_code = "remote_file_invalid_response"
description = "The remote file server returned an invalid response."
code = 502
class FileTooLargeError(BaseHTTPException):
error_code = "file_too_large"
description = "File size exceeded. {message}"
code = 413
class UnsupportedFileTypeError(BaseHTTPException):
error_code = "unsupported_file_type"
description = "File type not allowed."
code = 415
class BlockedFileExtensionError(BaseHTTPException):
error_code = "file_extension_blocked"
description = "The file extension is blocked for security reasons."
code = 400
class TooManyFilesError(BaseHTTPException):
error_code = "too_many_files"
description = "Only one file is allowed."
code = 400
class NoFileUploadedError(BaseHTTPException):
error_code = "no_file_uploaded"
description = "Please upload your file."
code = 400
class NotFoundError(BaseHTTPException):
error_code = "not_found"
code = 404
class ConversationNotFoundHTTPError(BaseHTTPException):
error_code = "conversation_not_found"
description = "The conversation was not found for this user and app."
code = HTTPStatus.NOT_FOUND
class ConversationCursorNotFoundHTTPError(BaseHTTPException):
error_code = "conversation_cursor_not_found"
description = "The conversation cursor is not in the current list. Refresh the list and try again."
code = HTTPStatus.NOT_FOUND
class ConversationVariableNotFoundHTTPError(BaseHTTPException):
error_code = "conversation_variable_not_found"
description = "The conversation variable was not found for this conversation."
code = HTTPStatus.NOT_FOUND
class ConversationVariableTypeMismatchHTTPError(BaseHTTPException):
error_code = "conversation_variable_type_mismatch"
description = "The new value does not match the conversation variable's type."
code = HTTPStatus.BAD_REQUEST
class ConversationFirstMessageNotFoundHTTPError(BaseHTTPException):
error_code = "conversation_first_message_not_found"
description = "The conversation has no message from which to generate a name."
code = HTTPStatus.NOT_FOUND
class MessageFeedbackRatingRequiredError(BaseHTTPException):
error_code = "message_feedback_rating_required"
description = "A rating is required when there is no existing feedback to remove."
code = HTTPStatus.BAD_REQUEST
def __init__(self) -> None:
super().__init__()
self.data = {
"code": self.error_code,
"message": self.description,
"status": self.code,
"details": {"request_id": get_request_id()},
}
class UnauthorizedError(BaseHTTPException):
error_code = "unauthorized"
code = HTTPStatus.UNAUTHORIZED
description = "Authentication is required."
class ForbiddenError(BaseHTTPException):
error_code = "forbidden"
code = HTTPStatus.FORBIDDEN
description = "You don't have the permission to access the requested resource."
class InternalServerError(BaseHTTPException):
"""Expose a safe response while retaining the original exception in server logs."""
error_code = "internal_server_error"
code = HTTPStatus.INTERNAL_SERVER_ERROR
description = (
"The server encountered an internal error and was unable to complete your request. "
"Either the server is overloaded or there is an error in the application."
)
class InvalidArgumentError(BaseHTTPException):
error_code = "invalid_param"
code = 400
class InvalidRequestError(BaseHTTPException):
error_code = "bad_request"
code = 400
description = "The browser (or proxy) sent a request that this server could not understand."
class AccessDeniedError(BaseHTTPException):
error_code = "forbidden"
code = 403
description = (
"You don't have the permission to access the requested resource. "
"It is either read-protected or not readable by the server."
)
class AuthenticationRequiredError(BaseHTTPException):
error_code = "unauthorized"
code = 401
description = "Authentication is required."
class UnsupportedMediaTypeError(BaseHTTPException):
error_code = "unsupported_media_type"
code = 415
description = "The server does not support the media type transmitted in the request."
class UnprocessableEntityError(BaseHTTPException):
error_code = "unprocessable_entity"
code = 422
class TooManyRequestsError(BaseHTTPException):
error_code = "too_many_requests"
code = 429
description = "This user has exceeded an allotted request count. Try again later."
class RequestBodyTooLargeError(BaseHTTPException):
error_code = "request_entity_too_large"
code = 413
def reraise_http_error(error: Exception) -> None:
"""Preserve framework and application HTTP errors at a transport boundary."""
if isinstance(error, HTTPException):
raise error
def raise_unexpected_error(error: Exception, *, message: str) -> NoReturn:
"""Keep HTTP failures intact and sanitize unclassified failures at the transport boundary.
Framework request parsing and project HTTP errors may both reach a route's
error translator. Neither should be converted to a generic server error.
"""
reraise_http_error(error)
logging.getLogger(__name__).exception(message)
raise InternalServerError() from error
def register_resource_error_handlers(api: ExternalApi) -> None:
"""Register resource failures before generic handlers on an initialized API.
Missing resources retain HTTP 404 and denied documents HTTP 403, including
their documented error codes; unrelated handlers remain registered.
"""
def missing_resource(error: Exception) -> tuple[dict[str, str | int], int]:
got_request_exception.send(current_app, exception=error)
return {"code": "not_found", "message": str(error), "status": 404}, 404
def document_access_denied(error: DocumentAccessDeniedError) -> tuple[dict[str, str | int], int]:
got_request_exception.send(current_app, exception=error)
return {"code": "forbidden", "message": str(error), "status": 403}, 403
handlers: object = api.error_handlers
assert isinstance(handlers, OrderedDict)
for error_type in (
AnnotationResourceNotFoundError,
DatasetNotFoundError,
DocumentNotFoundError,
DocumentSourceNotFoundError,
):
register_handler = api.errorhandler(error_type)
assert callable(register_handler)
register_handler(missing_resource)
handlers.move_to_end(error_type, last=False)
register_access_handler = api.errorhandler(DocumentAccessDeniedError)
assert callable(register_access_handler)
register_access_handler(document_access_denied)
handlers.move_to_end(DocumentAccessDeniedError, last=False)
for error_type in (
AnnotationResourceNotFoundError,
DatasetNotFoundError,
DocumentNotFoundError,
DocumentSourceNotFoundError,
):
api.document_error_handler(error_type, status=HTTPStatus.NOT_FOUND, code=NotFoundError.error_code)
api.document_error_handler(
DocumentAccessDeniedError,
status=HTTPStatus.FORBIDDEN,
code=ForbiddenError.error_code,
)
def register_permission_error_handler(api: ExternalApi) -> None:
"""Preserve the default permission-error response for Console and Service API."""
@api.errorhandler(NoPermissionError) # pyrefly: ignore[not-callable] - Flask-RESTX errorhandler overload
def handle_permission_error(error: NoPermissionError) -> tuple[dict[str, str | int], int]:
got_request_exception.send(current_app, exception=error)
return {
"code": InvalidArgumentError.error_code,
"message": str(error),
"status": HTTPStatus.BAD_REQUEST,
}, HTTPStatus.BAD_REQUEST
# Flask-RESTX picks the first matching handler, including the generic Exception handler.
handlers: object = api.error_handlers
assert isinstance(handlers, OrderedDict)
handlers.move_to_end(NoPermissionError, last=False)
api.document_error_handler(NoPermissionError, status=HTTPStatus.BAD_REQUEST, code=InvalidArgumentError.error_code)
def register_credential_refresh_error_handler(api: ExternalApi) -> None:
"""Give temporary refresh failures the same retryable response on each API surface.
Preconditions: The API supports exception-specific error registration.
Postconditions: Refresh failures return HTTP 503 before the generic handler.
Existing exception handlers and public error documentation remain registered.
"""
register_handler = api.errorhandler(CredentialRefreshUnavailableError)
assert callable(register_handler)
@register_handler
def handle_refresh_unavailable(error: CredentialRefreshUnavailableError) -> tuple[dict[str, str | int], int]:
got_request_exception.send(current_app, exception=error)
status = HTTPStatus.SERVICE_UNAVAILABLE
return {"code": "service_unavailable", "message": str(error), "status": status.value}, status.value
handlers: object = api.error_handlers
assert isinstance(handlers, OrderedDict)
handlers.move_to_end(CredentialRefreshUnavailableError, last=False)
api.document_error_handler(
CredentialRefreshUnavailableError, status=HTTPStatus.SERVICE_UNAVAILABLE, code="service_unavailable"
)
def register_moderation_error_handlers(api: ExternalApi) -> None:
"""Preserve model moderation error codes for non-streaming requests.
Preconditions: The API supports exception-specific registration and ordered
handlers. Postconditions: Rejections return 400 and unavailable checks return
503 before ValueError or generic handlers; both codes are documented without
changing unrelated error responses.
"""
def handle_moderation_error(
error: ModerationRejectedInvocationError | ModerationUnavailableInvocationError,
) -> tuple[dict[str, str | int], int]:
got_request_exception.send(current_app, exception=error)
http_error = moderation_http_exception(error)
assert http_error.code is not None
assert http_error.description is not None
return {
"code": http_error.error_code,
"message": http_error.description,
"status": http_error.code,
}, http_error.code
for error_type, http_error_type in (
(ModerationRejectedInvocationError, ModerationRejectedHTTPError),
(ModerationUnavailableInvocationError, ModerationUnavailableHTTPError),
):
register_handler = api.errorhandler(error_type)
assert callable(register_handler)
register_handler(handle_moderation_error)
handlers: object = api.error_handlers
assert isinstance(handlers, OrderedDict)
handlers.move_to_end(error_type, last=False)
assert http_error_type.code is not None
api.document_error_handler(error_type, status=http_error_type.code, code=http_error_type.error_code)