# -*- coding: utf-8 -*- """ 自定义异常模块 定义 API 层的异常类型 """ class OCRAPIException(Exception): """OCR API 基础异常""" def __init__(self, message: str, status_code: int = 500): self.message = message self.status_code = status_code super().__init__(self.message) class InvalidImageError(OCRAPIException): """无效的图片文件""" def __init__(self, message: str = "无效的图片文件"): super().__init__(message, status_code=400) class FileTooLargeError(OCRAPIException): """文件过大""" def __init__(self, message: str = "文件大小超过限制"): super().__init__(message, status_code=413) class UnsupportedFormatError(OCRAPIException): """不支持的文件格式""" def __init__(self, message: str = "不支持的文件格式"): super().__init__(message, status_code=415) class OCRProcessingError(OCRAPIException): """OCR 处理错误""" def __init__(self, message: str = "OCR 处理失败"): super().__init__(message, status_code=500) class ModelNotLoadedError(OCRAPIException): """模型未加载""" def __init__(self, message: str = "OCR 模型尚未加载完成"): super().__init__(message, status_code=503)