artifacts-dashboard/backend/app/services/error_service.py
Paweł Orzech 75313b83c0
Add multi-user workflows/pipelines and error tracking
Add multi-user automation features and per-user error tracking.

- Database migrations: add workflow_configs/workflow_runs (004), app_errors (005), pipeline_configs/pipeline_runs (006), and add user_token_hash to app_errors (007).
- Backend: introduce per-request token handling (X-API-Token) via app.api.deps and update many API routes (auth, automations, bank, characters, dashboard, events, exchange, logs) to use user-scoped Artifacts client and character scoping. Auth endpoints no longer store tokens server-side (validate-only); clear is a no-op on server.
- New Errors API and services: endpoint to list, filter, resolve, and report errors scoped to the requesting user; add error models, schemas, middleware/error handler and error_service for recording/hashing tokens.
- Pipelines & Workflows: add API routers, models, schemas and engine modules (pipeline/worker/coordinator, workflow runner/conditions) and action_executor updates to support workflow/pipeline execution.
- Logs: logs endpoint now prefers fetching recent action logs from the game API (with fallback to local DB), supports paging and filtering, and scopes results to the user.
- Frontend: add pipeline/workflow builders, lists, progress components and hooks (use-errors, use-pipelines, use-workflows), sentry client config, and updates to API client/constants/types.
- Misc: add middleware error handler, various engine strategy tweaks, tests adjusted.

Overall this change enables per-user API tokens, scopes DB queries to each user, introduces pipelines/workflows runtime support, and centralizes application error tracking.
2026-03-01 23:02:34 +01:00

77 lines
2.3 KiB
Python

"""Error logging service - writes errors to the database and optionally to Sentry."""
from __future__ import annotations
import hashlib
import logging
import traceback
import uuid
from contextvars import ContextVar
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from app.models.app_error import AppError
logger = logging.getLogger(__name__)
# Per-request correlation ID
correlation_id_var: ContextVar[str | None] = ContextVar("correlation_id", default=None)
def hash_token(token: str) -> str:
"""Return a stable SHA-256 hex digest for a user API token."""
return hashlib.sha256(token.encode()).hexdigest()
def new_correlation_id() -> str:
cid = uuid.uuid4().hex[:12]
correlation_id_var.set(cid)
return cid
async def log_error(
db_factory: async_sessionmaker[AsyncSession],
*,
severity: str = "error",
source: str = "backend",
error_type: str = "UnknownError",
message: str = "",
exc: BaseException | None = None,
context: dict[str, Any] | None = None,
correlation_id: str | None = None,
user_token_hash: str | None = None,
) -> AppError | None:
"""Persist an error record to the database.
Returns the created AppError, or None if the DB write itself fails.
"""
stack_trace = None
if exc is not None:
stack_trace = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
if not error_type or error_type == "UnknownError":
error_type = type(exc).__qualname__
if not message:
message = str(exc)
cid = correlation_id or correlation_id_var.get()
try:
async with db_factory() as db:
record = AppError(
severity=severity,
source=source,
error_type=error_type,
message=message[:4000],
stack_trace=stack_trace[:10000] if stack_trace else None,
context=context,
correlation_id=cid,
user_token_hash=user_token_hash,
)
db.add(record)
await db.commit()
await db.refresh(record)
return record
except Exception:
logger.exception("Failed to persist error record to database")
return None