office_translator/utils/exceptions.py

52 lines
1.5 KiB
Python

"""
Custom exceptions for the Document Translation API
"""
from fastapi import HTTPException
class TranslationError(Exception):
"""Base exception for translation errors"""
pass
class UnsupportedFileTypeError(TranslationError):
"""Raised when an unsupported file type is provided"""
pass
class FileSizeLimitExceededError(TranslationError):
"""Raised when a file exceeds the size limit"""
pass
class LanguageNotSupportedError(TranslationError):
"""Raised when a language code is not supported"""
pass
class DocumentProcessingError(TranslationError):
"""Raised when there's an error processing the document"""
pass
def handle_translation_error(error: Exception) -> HTTPException:
"""
Convert translation errors to HTTP exceptions
Args:
error: Exception that occurred
Returns:
HTTPException with appropriate status code and message
"""
if isinstance(error, UnsupportedFileTypeError):
return HTTPException(status_code=400, detail=str(error))
elif isinstance(error, FileSizeLimitExceededError):
return HTTPException(status_code=413, detail=str(error))
elif isinstance(error, LanguageNotSupportedError):
return HTTPException(status_code=400, detail=str(error))
elif isinstance(error, DocumentProcessingError):
return HTTPException(status_code=500, detail=str(error))
else:
return HTTPException(status_code=500, detail="An unexpected error occurred during translation")