-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror_codes.py
More file actions
32 lines (25 loc) · 916 Bytes
/
error_codes.py
File metadata and controls
32 lines (25 loc) · 916 Bytes
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
"""Stable machine-readable error codes for API JSON error responses."""
from __future__ import annotations
from enum import StrEnum
from flask import Response, jsonify
class ErrorCode(StrEnum):
SEARCH_INVALID_LIMIT = "SEARCH_INVALID_LIMIT"
INVALID_PATH = "INVALID_PATH"
SESSION_NOT_FOUND = "SESSION_NOT_FOUND"
INVALID_REQUEST_BODY = "INVALID_REQUEST_BODY"
INVALID_SINCE_MODE = "INVALID_SINCE_MODE"
PARSE_ERROR = "PARSE_ERROR"
EXPORT_NOTHING_TO_EXPORT = "EXPORT_NOTHING_TO_EXPORT"
INTERNAL_ERROR = "INTERNAL_ERROR"
def error_response(
code: ErrorCode,
message: str,
status: int,
**extra: object,
) -> tuple[Response, int]:
body: dict[str, object] = {"error": message, "code": code}
reserved = frozenset({"error", "code"})
for key, value in extra.items():
if key not in reserved:
body[key] = value
return jsonify(body), status