Spaces:
Sleeping
Sleeping
File size: 7,106 Bytes
36a47ad f1664b4 36a47ad 3b7f853 36a47ad f1664b4 36a47ad 3b7f853 36a47ad f1664b4 36a47ad | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass
from enum import StrEnum
from http import HTTPStatus
from typing import Literal
class ErrorCode(StrEnum):
UNKNOWN_TOOL = "unknown_tool"
UNSUPPORTED_FILE_TYPE = "unsupported_file_type"
UNSAFE_FILE_NAME = "unsafe_file_name"
DUPLICATE_FILE = "duplicate_file"
TOO_MANY_FILES = "too_many_files"
TOO_FEW_FILES = "too_few_files"
FILE_TOO_LARGE = "file_too_large"
INVALID_OPTIONS = "invalid_options"
INVALID_INPUT = "invalid_input"
INVALID_REQUEST = "invalid_request"
UNREADABLE_FILE = "unreadable_file"
UNPROCESSED_FILE = "unprocessed_file"
PASSWORD_REQUIRED = "password_required" # noqa: S105 An error code.
WRONG_PASSWORD = "wrong_password" # noqa: S105 An error code.
MAPPING_REQUIRED = "mapping_required"
INVALID_MAPPING = "invalid_mapping"
OCR_REQUIRED = "ocr_required"
UNSUPPORTED_LAYOUT = "unsupported_layout"
NO_RECORDS = "no_records"
UNAUTHENTICATED = "unauthenticated"
JOB_NOT_FOUND = "job_not_found"
JOB_EXPIRED = "job_expired"
JOB_STATE_CONFLICT = "job_state_conflict"
SERVER_BUSY = "server_busy"
TIMEOUT = "timeout"
INTERNAL_ERROR = "internal_error"
@dataclass(frozen=True)
class ErrorSpec:
status: HTTPStatus
message: str
ERRORS: Mapping[ErrorCode, ErrorSpec] = {
ErrorCode.UNKNOWN_TOOL: ErrorSpec(HTTPStatus.NOT_FOUND, "There is no such tool."),
ErrorCode.UNSUPPORTED_FILE_TYPE: ErrorSpec(
HTTPStatus.UNSUPPORTED_MEDIA_TYPE, "This tool does not accept this type of file."
),
ErrorCode.UNSAFE_FILE_NAME: ErrorSpec(
HTTPStatus.BAD_REQUEST, "The file name is not allowed. Rename the file and try again."
),
ErrorCode.DUPLICATE_FILE: ErrorSpec(
HTTPStatus.CONFLICT, "A file with this name is already in the job."
),
ErrorCode.TOO_MANY_FILES: ErrorSpec(
HTTPStatus.BAD_REQUEST, "Too many files for this tool. Remove some and start again."
),
ErrorCode.TOO_FEW_FILES: ErrorSpec(
HTTPStatus.BAD_REQUEST, "This tool needs more files. Add them and start again."
),
ErrorCode.FILE_TOO_LARGE: ErrorSpec(
HTTPStatus.REQUEST_ENTITY_TOO_LARGE, "The file is larger than the upload limit."
),
ErrorCode.INVALID_OPTIONS: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY, "Some details are not valid. Correct them and start again."
),
ErrorCode.INVALID_INPUT: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"The file does not hold what this tool expects. Check the file.",
),
ErrorCode.INVALID_REQUEST: ErrorSpec(
HTTPStatus.BAD_REQUEST, "The request was not understood. Refresh the page and try again."
),
ErrorCode.UNREADABLE_FILE: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY, "The file could not be opened. Upload a fresh copy."
),
ErrorCode.UNPROCESSED_FILE: ErrorSpec(
HTTPStatus.INTERNAL_SERVER_ERROR,
"Something went wrong on the server while reading this file. Try again.",
),
ErrorCode.PASSWORD_REQUIRED: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"The PDF is password protected. Enter its password to continue.",
),
ErrorCode.WRONG_PASSWORD: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"That password did not open the PDF. Check it and try again.",
),
ErrorCode.MAPPING_REQUIRED: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"Check how the registers' columns are read, then continue.",
),
ErrorCode.INVALID_MAPPING: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"The column choices do not match the sheets the job asked about. Refresh and try again.",
),
ErrorCode.OCR_REQUIRED: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"The page is a scanned image and could not be read. "
"Upload a clearer scan or the digital statement.",
),
ErrorCode.UNSUPPORTED_LAYOUT: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"This statement layout is not supported. "
"Upload the statement as downloaded from the bank or the portal.",
),
ErrorCode.NO_RECORDS: ErrorSpec(
HTTPStatus.UNPROCESSABLE_ENTITY,
"No entries were found in the file. Check that it is the right one.",
),
ErrorCode.UNAUTHENTICATED: ErrorSpec(
HTTPStatus.UNAUTHORIZED, "Your sign-in has ended. Sign in again to continue."
),
ErrorCode.JOB_NOT_FOUND: ErrorSpec(
HTTPStatus.NOT_FOUND, "This job does not exist. Start a new one."
),
ErrorCode.JOB_EXPIRED: ErrorSpec(
HTTPStatus.GONE, "The files for this job were deleted after the time limit."
),
ErrorCode.JOB_STATE_CONFLICT: ErrorSpec(
HTTPStatus.CONFLICT, "The job cannot do that right now. Refresh to see where it is."
),
ErrorCode.SERVER_BUSY: ErrorSpec(
HTTPStatus.SERVICE_UNAVAILABLE,
"The server is busy with other jobs. Try again in a few minutes.",
),
ErrorCode.TIMEOUT: ErrorSpec(
HTTPStatus.GATEWAY_TIMEOUT,
"The job took too long and was stopped. Try again with fewer files.",
),
ErrorCode.INTERNAL_ERROR: ErrorSpec(
HTTPStatus.INTERNAL_SERVER_ERROR, "Something went wrong on the server. Try again."
),
}
PasswordReason = Literal[ErrorCode.PASSWORD_REQUIRED, ErrorCode.WRONG_PASSWORD]
ADAPTER_CODES = frozenset(
{
ErrorCode.TOO_FEW_FILES,
ErrorCode.INVALID_OPTIONS,
ErrorCode.INVALID_INPUT,
ErrorCode.UNREADABLE_FILE,
ErrorCode.UNPROCESSED_FILE,
ErrorCode.OCR_REQUIRED,
ErrorCode.UNSUPPORTED_LAYOUT,
ErrorCode.NO_RECORDS,
ErrorCode.INVALID_MAPPING,
}
)
class CodedError(Exception):
def __init__(
self,
code: ErrorCode,
file: str | None = None,
page: int | None = None,
detail: str | None = None,
) -> None:
if file == "":
raise ValueError("file must be a name or None")
if page is not None and page < 1:
raise ValueError("pages are numbered from 1")
super().__init__(code, file, page)
self.code = code
self.file = file
self.page = page
self.detail = detail
@property
def message(self) -> str:
return ERRORS[self.code].message
def __str__(self) -> str:
return str(self.code)
class ToolError(CodedError):
def __init__(
self,
code: ErrorCode,
file: str | None = None,
page: int | None = None,
detail: str | None = None,
) -> None:
if code not in ADAPTER_CODES:
raise ValueError(f"an adapter cannot raise {code}")
super().__init__(code, file, page, detail)
class PasswordError(CodedError):
def __init__(self, files: Mapping[str, PasswordReason]) -> None:
if not files or "" in files:
raise ValueError("name every file that needs a password")
super().__init__(ErrorCode.PASSWORD_REQUIRED)
self.files = dict(files)
|