error-handling

작성자: microsoft

统一 오류 처리 시스템. API 엔드포인트 추가, 오류 처리 수정, 프론트엔드 API 호출 추가, 오류 관련 테스트 작성 시 사용합니다.

npx skills add https://github.com/microsoft/data-formulator --skill error-handling

Error Handling Skill

Unified error handling system for DF. Use when adding API endpoints, modifying error handling, or adding frontend API calls.

Prerequisites: Read docs/dev-guides/7-unified-error-handling.md before changing API error behavior. Read docs/dev-guides/2-log-sanitization.md when the work involves logging, credentials, external services, or DataLoaders. If your work introduces new error handling patterns or conventions, update this file and related dev-guides accordingly.

Architecture Overview

Frontend                              Backend
────────                              ───────
apiClient.ts                          errors.py
├── apiRequest()    ←── JSON ────     ├── ErrorCode (enum)
├── streamRequest() ←── NDJSON ──     └── AppError (exception)
└── parseStreamLine()
                                      error_handler.py
errorCodes.ts                         ├── register_error_handlers(app)
└── getErrorMessage()                 ├── classify_and_wrap_llm_error()
                                      └── stream_error_event()
errorHandler.ts
└── handleApiError()                  security/sanitize.py
                                      └── classify_llm_error() (internal)
MessageSnackbar ← dfSlice.messages

Protocol Snapshot

Use this contract for all new or reworked DF APIs:

ScenarioHTTPShape
Non-streaming success200{"status": "success", "data": ...}
Non-streaming business/validation error200{"status": "error", "error": {"code", "message", "retry", "request_id"}}
Non-streaming auth/authorization error401 / 403same structured error body
Streaming preflight error200application/json + {"status": "error", "error": ...}
Streaming in-flight fatal error200NDJSON line: {"type": "error", "error": ...}
No Flask route / too large / unhandled crash404 / 413 / 500transport-level error

Do not use HTTP 400/422 for application validation errors in new code. Do not convert in-flight NDJSON errors to status: "error"; once the stream has started, event type is the protocol discriminator.

Backend: Adding a New API Endpoint

HTTP Status Code Policy

Application-controlled business and validation errors return HTTP 200 with status: "error" in the body. Only these use non-200:

  • 401/403 — auth errors (AUTH_REQUIRED, AUTH_EXPIRED, ACCESS_DENIED)
  • 404 — no matching Flask route
  • 413 — WSGI body limit exceeded
  • 500 — unhandled exception (program bug)

Non-streaming endpoint

from data_formulator.errors import AppError, ErrorCode
from data_formulator.error_handler import json_ok

@bp.route('/my-endpoint', methods=['POST'])
def my_endpoint():
    content = request.get_json()
    if not content.get('required_field'):
        raise AppError(ErrorCode.INVALID_REQUEST, "Missing required_field")

    try:
        result = do_work(content)
    except SomeBusinessError as e:
        raise AppError(ErrorCode.DATA_LOAD_ERROR, "Failed to load data") from e
    except Exception as e:
        from data_formulator.error_handler import classify_and_wrap_llm_error
        raise classify_and_wrap_llm_error(e) from e

    return json_ok(result)
# Global handler returns: HTTP 200 + {"status": "error", "error": {code, message, retry}}
# Auth errors (AUTH_REQUIRED/AUTH_EXPIRED/ACCESS_DENIED) return 401/403

Legacy {"status": "error", "message": "..."}, error_message, bare {error}, and status: "ok" responses are historical formats. Do not add new compatibility branches for them; migrate the route to json_ok() / AppError before using apiRequest().

Streaming endpoint

Validation MUST be outside the generator. Failures return 200 JSON (not NDJSON).

from data_formulator.errors import AppError, ErrorCode
from data_formulator.error_handler import (
    classify_and_wrap_llm_error,
    stream_error_event,
    stream_preflight_error,
)

@bp.route('/my-stream', methods=['POST'])
def my_stream():
    if not request.is_json:
        return stream_preflight_error(
            AppError(ErrorCode.INVALID_REQUEST, "Invalid request")
        )

    content = request.get_json()
    client = get_client(content['model'])

    def generate():
        try:
            for event in agent.run(...):
                yield json.dumps(event, ensure_ascii=False) + "\n"
        except Exception as e:
            yield stream_error_event(classify_and_wrap_llm_error(e))

    return Response(stream_with_context(generate()), mimetype='application/x-ndjson')

Streaming runtime errors intentionally use {"type": "error", "error": ...}. They cannot use a top-level status envelope because the HTTP response and NDJSON event stream have already started.

Frontend: Consuming an API

Non-streaming

import { apiRequest } from '../app/apiClient';
import { handleApiError } from '../app/errorHandler';

try {
    const { data } = await apiRequest<ResponseType>(getUrls().MY_ENDPOINT, {
        method: 'POST',
        body: JSON.stringify(payload),
        headers: { 'Content-Type': 'application/json' },
    });
} catch (e) {
    handleApiError(e, 'MyComponent');
}

For UI loading state, model the request lifecycle explicitly with LoadableState from src/app/loadableState.ts. Do not infer loading from missing data (!data), because failed requests may legitimately leave data empty while loading has ended.

Streaming

import { streamRequest } from '../app/apiClient';
import { handleApiError } from '../app/errorHandler';

try {
    for await (const event of streamRequest(url, options, abortController.signal)) {
        switch (event.type) {
            case 'text_delta':
                break;
            case 'error':
                // Error arrived mid-stream — show inline in component.
                break;
            case 'done':
                break;
        }
    }
} catch (e) {
    handleApiError(e, 'MyComponent');
}

With callbacks

handleApiError(e, 'MyComponent', {
    onAuth: () => redirectToLogin(),        // AUTH_REQUIRED / AUTH_EXPIRED
    onRetryable: () => retryOperation(),    // LLM_RATE_LIMIT / LLM_TIMEOUT
    silent: true,                           // don't show Snackbar (component handles display)
});

Migration and special cases

DF API consumers should use apiRequest() / streamRequest() and handleApiError(). Direct fetchWithIdentity() is for lower-level client helpers and explicit protocol exceptions such as file downloads, blob/CSV responses, OIDC redirects, SPA fallback, or third-party URLs.

Do not apply the normal JSON API protocol mechanically to file downloads / CSV streaming, SPA fallback, OIDC redirect flows, frontend fetches to third-party URLs, or errors after a streaming response has already started. Check the route's protocol first, then preserve safe error bodies and avoid str(exc) exposure.

Adding a New Error Code

  1. Backend — Add to py-src/data_formulator/errors.py ErrorCode:

    MY_NEW_ERROR = "MY_NEW_ERROR"
    

    No HTTP mapping needed — defaults to HTTP 200. Only add to ERROR_CODE_HTTP_STATUS if it's an auth code.

  2. Frontend mapping — Add to src/app/errorCodes.ts ERROR_CODE_I18N_MAP:

    MY_NEW_ERROR: 'errors.myNewError',
    
  3. Translations — Add to both locale files:

    • src/i18n/locales/en/errors.json: "myNewError": "English message"
    • src/i18n/locales/zh/errors.json: "myNewError": "中文消息"

Migrated Endpoints Reference

All streaming endpoints are now on the unified protocol:

EndpointFormatNotes
/data-agent-streamingNDJSON + stream_error_event()Emits top-level type events; errors use {type:"error", error:{...}}
/get-recommendation-questionsNDJSON + stream_error_event()Was error: {json} prefix
/generate-report-chatPure NDJSON + stream_error_event()Was SSE data: {json} prefix
/data-loading-chatNDJSON + stream_error_event()str(e) removed
/clean-data-streamNDJSON + stream_error_event()Was \n{json}\n format

Non-streaming endpoints:

EndpointError FormatNotes
/chart-insightAppError → HTTP 200 + {status:"error", error:{code,message,retry}}Fully migrated. Frontend uses fetchChartInsight rejected reducer.
All migrated endpointsAppError → HTTP 200 + unified error bodycredentials, knowledge, sessions, tables, agents
/derive-data, /refine-data, /sort-data, /process-data-on-load, /test-modeljson_ok() / AppErrorMigrated to new format

Empty Catch Policy

Not all .catch(() => {}) are bugs. Use this decision tree:

  1. User-initiated action (delete, refresh, submit) → must notify with addMessages or handleApiError()
  2. Background/best-effort fetch (connector list on mount, session list) → OK to swallow, but add a comment
  3. RTK thunks → always add .rejected handler with addMessages
  4. AbortError → filter out with if (action.error?.name !== 'AbortError')

Frontend Stream Parsing Pattern

When consuming a migrated streaming endpoint, handle the current NDJSON event format directly:

const data = JSON.parse(line);
if (data.type === 'error') {
    const errMsg = data.error?.message || 'Unknown error';
    // show to user...
}

Backend: Database/Workspace Errors (tables.py)

For table CRUD endpoints, use the specialized classifier:

from data_formulator.routes.tables import classify_and_raise_db_error

@tables_bp.route('/my-table-op', methods=['POST'])
def my_table_op():
    try:
        result = workspace.do_something()
        return jsonify({"status": "success", "data": result})
    except Exception as e:
        classify_and_raise_db_error(e)

classify_and_raise_db_error maps common DB errors to appropriate AppError codes (returned as HTTP 200 by the global handler, except ACCESS_DENIED → 403):

  • "Table does not exist" → TABLE_NOT_FOUND (HTTP 200)
  • "Table already exists" → INVALID_REQUEST (HTTP 200)
  • "Permission denied" → ACCESS_DENIED (HTTP 403)
  • Other → CONNECTOR_ERROR (HTTP 200)

Backend: Connector Errors (data_connector.py)

For connector endpoints, use:

from data_formulator.data_connector import classify_and_raise_connector_error

except Exception as e:
    classify_and_raise_connector_error(e, operation="preview")

Connector/DataLoader classification is intentionally simple and lives in data_formulator.data_loader.connector_errors. It maps common failures to a small stable set: INVALID_REQUEST, CONNECTOR_AUTH_FAILED, AUTH_EXPIRED, ACCESS_DENIED, DB_CONNECTION_FAILED, DB_QUERY_ERROR, DATA_LOAD_ERROR, or CONNECTOR_ERROR. Do not add endpoint-local string matching unless the classifier cannot reasonably cover the category.

All JSON errors include error.request_id and an X-Request-Id response header. Show/copy this ID for users when reporting backend failures; do not show raw exception text in production. Unhandled 500 responses must never include raw tracebacks, even in debug mode; return a safe category plus request_id and keep full stack traces in server logs only.

Debugging Error Propagation

When an error isn't reaching the frontend:

  1. Check backend logs — is the error logged?
  2. Check response format:
    • Non-streaming: {"status": "error", "error": {"code": ..., "message": ...}}
    • Streaming: one line {"type": "error", "error": {"code": ..., "message": ...}}
  3. Check Content-Type — streaming must be application/x-ndjson, not application/json or text/event-stream
  4. Check frontend parser — is the consumer looking for data.type === 'error'?
  5. Check global handler — verify register_error_handlers(app) is called in app.py
  6. Check blueprint handlers — blueprint-level errorhandler(Exception) takes priority over global handlers

Legacy message / error_message bodies are protocol violations on migrated API paths.

Log Sanitization (Sensitive Data in Server Logs)

Server-side logs must never leak passwords, tokens, API keys, or connection strings. The project uses a defense-in-depth approach with two layers.

Layer 1: Explicit Utilities (call-site)

from data_formulator.security.log_sanitizer import (
    sanitize_url, sanitize_params, redact_token,
)

# Dict with credentials → sanitize_params()
log.info("Connecting with: %s", sanitize_params(params))

# URL that may embed credentials → sanitize_url()
logger.info("Issuer: %s", sanitize_url(issuer_url))

# Token/API key → redact_token()
logger.debug("Token: %s", redact_token(token))

Layer 2: SensitiveDataFilter (global safety net)

Registered in app.py:configure_logging(). Automatically redacts:

  • URL credentials (://user:pass@host)
  • Bearer tokens
  • password=xxx, api_key=xxx, secret=xxx patterns
  • JWT-like base64 strings
  • Python dict repr with sensitive keys

Disable with LOG_SANITIZE=false for local debugging only.

When to Use What

DataUtilityWhy not just filter?
dict with password keyssanitize_params()Filter can't identify arbitrary password values in dict repr
URL from config/envsanitize_url()Explicit is clearer; filter is backup
Token/key valueredact_token()Explicit is clearer; filter is backup
Normal textNothingFilter handles edge cases

New Module Checklist

When adding a module that handles credentials or external services:

  1. Audit all logger.*() calls for credential/URL/token logging
  2. Use sanitize_params() for dicts, sanitize_url() for URLs, redact_token() for tokens
  3. Prefer type(exc).__name__ over str(exc) in warning-level logs
  4. If introducing new credential key names, add to SENSITIVE_KEYS in log_sanitizer.py

Key Files

FilePurpose
py-src/data_formulator/errors.pyErrorCode enum + AppError exception
py-src/data_formulator/error_handler.pyGlobal handlers, classify_and_wrap_llm_error, stream_error_event
py-src/data_formulator/security/log_sanitizer.pysanitize_url, sanitize_params, redact_token, SensitiveDataFilter
py-src/data_formulator/routes/tables.pyclassify_and_raise_db_error (database/workspace errors)
py-src/data_formulator/data_connector.pyclassify_and_raise_connector_error (connector errors)
py-src/data_formulator/security/sanitize.pyclassify_llm_error (internal), sanitize_error_message
src/app/apiClient.tsapiRequest, streamRequest, parseStreamLine, ApiRequestError
src/app/errorHandler.tshandleApiError
src/app/errorCodes.tsERROR_CODE_I18N_MAP, getErrorMessage
src/i18n/locales/{en,zh}/errors.jsonError message translations

microsoft의 다른 스킬

oss-growth
microsoft
OSS 성장 해커 페르소나
agent-framework-azure-ai-py
microsoft
Microsoft Agent Framework Python SDK(agent-framework-azure-ai)를 사용하여 Azure AI Foundry 에이전트를 구축합니다. AzureAIAgentsProvider로 지속적 에이전트를 만들 때, 호스팅 도구(코드 인터프리터, 파일 검색, 웹 검색)를 사용할 때, MCP 서버를 통합할 때, 대화 스레드를 관리할 때, 또는 스트리밍 응답을 구현할 때 사용합니다. 함수 도구, 구조화된 출력, 다중 도구 에이전트를 다룹니다.
development
airunway-aks-setup
microsoft
AKS에서 AI Runway 설정 — 빈 클러스터에서 실행 중인 모델까지. 클러스터 검증, 컨트롤러 설치, GPU 평가, 공급자 설정, 첫 배포를 다룹니다. 시기: "AI Runway 설정", "AKS 클러스터 온보딩", "AI Runway 설치", "airunway 설정", "AKS에 모델 배포", "AKS에서 GPU 추론", "AKS에서 KAITO 설정", "AKS에서 LLM 실행", "AKS에서 vLLM", "AKS에서 모델 서빙 설정", "AI Runway 컨트롤러".
devops
appinsights-instrumentation
microsoft
Azure Application Insights로 웹앱을 계측하기 위한 지침입니다. 원격 분석 패턴, SDK 설정, 구성 참조를 제공합니다. WHEN: 앱 계측 방법, App Insights SDK, 원격 분석 패턴, App Insights란 무엇인가, Application Insights 지침, 계측 예시, APM 모범 사례.
devops
applicationinsights-web-ts
microsoft
브라우저/웹 앱을 Application Insights JavaScript SDK(@microsoft/applicationinsights-web)로 계측합니다. Real User Monitoring(RUM) — 페이지 뷰, 클릭, AJAX/fetch 종속성, 예외, 사용자 지정 이벤트, 백엔드 OpenTelemetry 트레이스와 상관관계가 있는 브라우저 측 GenAI 에이전트 트레이스에 사용합니다. SDK Loader Script 및 npm 설정, 프레임워크 확장(React, React Native, Angular), Click Analytics, 텔레메트리 이니셜라이저, 브라우저에서 생성된 에이전트/도구/모델 스팬에 대한 OTel GenAI 의미론적 규칙을 다룹니다.
devops
azure-ai-anomalydetector-java
microsoft
Azure AI Anomaly Detector SDK for Java로 이상 탐지 애플리케이션을 구축하세요. 단변량/다변량 이상 탐지, 시계열 분석 또는 AI 기반 모니터링을 구현할 때 사용하세요.
development
azure-ai-language-conversations-py
microsoft
azure-ai-language-conversations Python SDK를 사용하여 대화형 언어 이해(CLU)를 구현합니다. ConversationAnalysisClient로 대화 의도와 엔터티를 분석하거나, NLP 기능을 구축하거나, 애플리케이션에 언어 이해를 통합할 때 사용합니다.
development
azure-ai-ml-py
microsoft
Azure Machine Learning SDK v2 for Python. ML 작업 영역, 작업, 모델, 데이터 세트, 컴퓨팅 및 파이프라인에 사용합니다. 트리거: "azure-ai-ml", "MLClient", "workspace", "model registry", "training jobs", "datasets".
development