feat: foundations for the manager conversion (platform-v2 step 6.1)
- Backend: every HTTPException in routers/ is an error code now (260 raises, ~105 new codes, texts in en + el); XeniaError(detail=...) keeps the three v1-shaped answers old pages read; bad / unknown tokens answer auth.session_ended; test_errors refuses new HTTPExceptions. - Manager: v2.json is the list of pages on the v2 rules: scripts/lint.mjs lints them, tests/v2-guards.test.mjs refuses Greek and colour literals, `strict` paths make set-state-in-effect an error. - src/live/useLiveQuery.js (react-query + live events + refresh on reconnect); Signed in now uses it. - Dead manager files removed (3.1k lines unreachable from main.jsx). - Tests: backend 544 passed, manager unit 33, run-all (no Android) passed (manager's live bell check passed on a rerun). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,8 +11,8 @@ Every error response has this shape:
|
||||
New and converted code raises XeniaError:
|
||||
raise XeniaError("order.not_found", order_id=order_id)
|
||||
The status comes from the registry below; pass status= only to override it.
|
||||
Plain HTTPException (still in most v1 routers; converted gradually, plan step 6.x)
|
||||
is wrapped as code "http.<status>" with its original `detail` untouched.
|
||||
Plain HTTPException is wrapped as code "http.<status>" with its original `detail` untouched
|
||||
(the routers have none since step 6.1; tests/test_errors.py refuses new ones).
|
||||
Request validation errors become "validation.invalid" with the field errors in params.
|
||||
Unhandled exceptions become "internal.error" (500) with the request id in params.
|
||||
|
||||
@@ -147,18 +147,129 @@ CODES: dict[str, tuple[int, str]] = {
|
||||
"system.log_not_found": (404, "There is no log file for {date}."),
|
||||
"catalog.unknown_category": (422, "There is no category {category_id}."),
|
||||
"catalog.primary_not_listed": (422, "The main category ({category_id}) must be one of the product's categories."),
|
||||
# converted from HTTPException (plan step 6.1)
|
||||
"auth.password_current_required": (400, "Enter your current password to set a new one."),
|
||||
"auth.no_password": (400, "This account has no password yet."),
|
||||
"auth.password_wrong": (400, "The current password is wrong."),
|
||||
"auth.password_too_short": (400, "The password must be at least {min} characters long."),
|
||||
"auth.pin_too_short": (400, "The PIN must be at least {min} digits long."),
|
||||
"license.locked": (423, "The system was locked by the administrator. Contact support."),
|
||||
"settings.invalid_json": (422, "The settings must be valid JSON."),
|
||||
"settings.unknown_key": (400, "There is no setting {key}."),
|
||||
"business_day.already_open": (400, "A business day is already open."),
|
||||
"business_day.none_open": (404, "No business day is open."),
|
||||
"business_day.closed": (403, "The venue isn't open: a manager must open the business day first."),
|
||||
"business_day.orders_open": (409, "{open_orders} table(s) still open, {partially_paid} with unpaid items."),
|
||||
"business_day.not_found": (404, "Business day not found."),
|
||||
"business_day.not_closed": (400, "Only a closed business day can be edited."),
|
||||
"business_day.close_in_future": (400, "The closing time can't be in the future."),
|
||||
"business_day.close_before_last_order": (400, "The closing time can't be earlier than the last order's close ({time})."),
|
||||
"business_day.delete_open": (400, "An open business day can't be deleted. Close it first."),
|
||||
"business_day.has_shifts": (409, "This business day has {count} shift(s). Delete those first."),
|
||||
"validation.datetime": (422, "That date or time isn't valid."),
|
||||
"validation.choice": (400, "{field} can't be {value}."),
|
||||
"validation.range": (400, "{field} must be between {min} and {max}."),
|
||||
"validation.positive": (400, "{field} must be more than zero."),
|
||||
"validation.missing": (400, "{field} is required."),
|
||||
"validation.lan_ip": (422, "Enter a local network IPv4 address (e.g. 192.168.1.50 or 10.0.0.5)."),
|
||||
"chat.not_found": (404, "Conversation not found."),
|
||||
"chat.not_participant": (403, "You're not in this conversation."),
|
||||
"chat.empty_message": (400, "The message is empty."),
|
||||
"chat.system_group": (403, "You can't leave the staff group."),
|
||||
"chat.leave_direct": (400, "A one-to-one conversation can't be left."),
|
||||
"chat.direct_two": (400, "A one-to-one conversation has exactly two people."),
|
||||
"chat.group_name": (400, "A group conversation needs a name."),
|
||||
"customer.not_found": (404, "Customer not found."),
|
||||
"transfer.wrong_type": (400, "This file isn't a {expected} export."),
|
||||
"transfer.version": (400, "This export's version isn't supported."),
|
||||
"contact.not_found": (404, "Contact not found."),
|
||||
"expense.not_found": (404, "Expense not found."),
|
||||
"flag.not_found": (404, "Mark not found."),
|
||||
"flag.in_use": (409, "This mark is on {count} table(s). Remove it there first."),
|
||||
"flag.invalid_ids": (400, "Some of these marks don't exist."),
|
||||
"staff_message.template_not_found": (404, "Message template not found."),
|
||||
"staff_message.not_found": (404, "Message not found."),
|
||||
"note.not_found": (404, "Note not found."),
|
||||
"todo.not_found": (404, "To-do not found."),
|
||||
"order.not_yours": (403, "This order isn't one of your tables."),
|
||||
"order.not_open": (400, "This order isn't open any more."),
|
||||
"order.item_not_found": (404, "That item isn't on the order any more."),
|
||||
"order.item_paid": (400, "This item is already paid."),
|
||||
"order.item_not_active": (400, "Only items still on the order can be changed."),
|
||||
"order.nothing_paid": (404, "There are no paid items to undo."),
|
||||
"order.cannot_close": (400, "This order can't be closed now ({status})."),
|
||||
"order.no_active_items": (400, "None of these items are on the order any more."),
|
||||
"order.waiter_assigned": (400, "That waiter is already on this order."),
|
||||
"order.assignment_not_found": (404, "That waiter isn't on this order."),
|
||||
"order.same_table": (400, "The order is already on that table."),
|
||||
"order.same_order": (400, "An order can't be joined with itself."),
|
||||
"order.target_not_found": (404, "The other order isn't there any more."),
|
||||
"order.split_quantity": (400, "Split into 1 to {max} pieces."),
|
||||
"order.discounts_off": (403, "Discounts are turned off for this venue."),
|
||||
"order.no_customer": (400, "Give the order a customer first."),
|
||||
"order.item_not_tabbable": (400, "This item is already {status}: it can't go on a tab."),
|
||||
"order.product_unavailable": (400, "Product {product_id} isn't available."),
|
||||
"printer.not_found": (404, "That printer isn't there or is turned off."),
|
||||
"print_job.not_found": (404, "That print job isn't there any more."),
|
||||
"print_job.not_pending": (400, "Only tickets still waiting to print can be cancelled."),
|
||||
"table.not_found": (404, "Table not found."),
|
||||
"table.busy": (400, "That table already has an open order."),
|
||||
"table.has_order": (400, "A table with an open order can't be deleted or turned off."),
|
||||
"table.name_too_long": (400, "The table names would be too long ({name}: at most {max} characters). Shorten the prefix or make fewer."),
|
||||
"table_group.name_taken": (400, "There is already a group with that name."),
|
||||
"table_group.not_found": (404, "Table group not found."),
|
||||
"prep_zone.not_found": (404, "Prep zone not found."),
|
||||
"price_group.not_found": (404, "Price group not found."),
|
||||
"modifier.not_found": (404, "Price rule not found."),
|
||||
"deal.not_found": (404, "Deal not found."),
|
||||
"catalog.category_not_found": (404, "Category not found."),
|
||||
"catalog.parent_not_found": (404, "The parent category doesn't exist."),
|
||||
"catalog.nest_has_children": (400, "A category with sub-categories can't go under another one."),
|
||||
"catalog.too_deep": (400, "Categories go at most two levels deep."),
|
||||
"catalog.own_parent": (400, "A category can't be its own parent."),
|
||||
"catalog.group_not_found": (404, "Option group not found."),
|
||||
"product.not_found": (404, "Product not found."),
|
||||
"product.in_past_orders": (400, "This product is in past orders, so it can't be deleted. Archive it instead."),
|
||||
"staff.not_found": (404, "Staff member not found."),
|
||||
"staff.username_taken": (409, "That username is already taken."),
|
||||
"staff.superadmin_protected": (403, "A superadmin account can't be changed."),
|
||||
"staff.superadmin_only": (403, "Only a superadmin can give the superadmin role."),
|
||||
"staff.role_invalid": (400, "There is no role {role}."),
|
||||
"staff.avatar_type": (400, "The photo must be a picture file."),
|
||||
"staff.assignment_exists": (400, "That assignment already exists."),
|
||||
"staff.assignment_not_found": (404, "Assignment not found."),
|
||||
"schedule.shift_not_found": (404, "Scheduled shift not found."),
|
||||
"setup.done": (409, "Setup is already done: a manager account exists."),
|
||||
"shift.already_active": (400, "This person already has a shift going."),
|
||||
"shift.start_needs_manager": (403, "Starting the shift needs a manager's approval."),
|
||||
"shift.end_needs_manager": (403, "Ending the shift needs a manager's approval."),
|
||||
"shift.others_need_manager": (403, "Only managers can start shifts for other staff."),
|
||||
"shift.none_active": (404, "No shift is going."),
|
||||
"shift.not_found": (404, "Shift not found."),
|
||||
"shift.not_yours": (403, "This shift belongs to someone else."),
|
||||
"shift.ended": (400, "This shift has already ended."),
|
||||
"shift.on_break": (400, "A break is already going."),
|
||||
"shift.no_break": (404, "No break is going."),
|
||||
"shift.delete_active": (400, "A shift that is still going can't be deleted. End it first."),
|
||||
"tab.not_found": (404, "Tab not found."),
|
||||
"tab.already_open": (400, "This customer already has an open tab."),
|
||||
"tab.over_balance": (400, "That's more than the balance ({balance})."),
|
||||
"tab.has_balance": (400, "The tab still owes {balance}. Pay it or forgive it first."),
|
||||
"waste.not_found": (404, "Throwaway entry not found."),
|
||||
"waste.not_today": (403, "Only throwaways of the open business day can be deleted."),
|
||||
}
|
||||
|
||||
|
||||
class XeniaError(Exception):
|
||||
"""An API error with a registered code. raise XeniaError("order.not_found", order_id=5)."""
|
||||
|
||||
def __init__(self, code: str, status: int | None = None, **params):
|
||||
def __init__(self, code: str, status: int | None = None, *, detail=None, **params):
|
||||
if code not in CODES:
|
||||
raise KeyError(f"unregistered error code {code!r}: add it to core/errors.py CODES")
|
||||
default_status, template = CODES[code]
|
||||
self.code = code
|
||||
self.status = status or default_status
|
||||
self.detail = detail # v1-shaped `detail` some old pages still read (6.x removes)
|
||||
self.params = params
|
||||
try:
|
||||
self.message = template.format(**params)
|
||||
@@ -173,7 +284,7 @@ def error_body(code: str, message: str, params: dict | None = None, detail=None)
|
||||
|
||||
|
||||
async def _xenia_error_handler(request: Request, exc: XeniaError):
|
||||
return JSONResponse(error_body(exc.code, exc.message, exc.params), status_code=exc.status)
|
||||
return JSONResponse(error_body(exc.code, exc.message, exc.params, detail=exc.detail), status_code=exc.status)
|
||||
|
||||
|
||||
async def _http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||||
|
||||
Reference in New Issue
Block a user