-
-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
Problem
Currently, Display implementation for Error shows either too little information (only kind without colored feature) or too much (everything including source chain, metadata, and potentially sensitive data with colored feature).
Production environments need:
- Structured JSON logging for machine parsing (Datadog, Grafana, etc.)
- Minimal, safe output without internal details
- No colors (not needed for structured logs)
Development environments need:
- Human-readable format with full context
- Complete source chain and backtrace
- Colored output (when
coloredfeature is enabled and TTY is detected)
Proposed Solution
Implement environment-based display levels controlled by MASTERROR_ENV environment variable:
MASTERROR_ENV=prod (Production)
- Lightweight JSON format
- Fields:
kind,code,message(if not redacted) - Filtered metadata (no sensitive fields)
- NO source chain
- NO backtrace
- NO colors (JSON doesn't need coloring)
Example output:
{"kind":"NotFound","code":"NOT_FOUND","message":"User not found"}MASTERROR_ENV=local (Development)
- Human-readable format
- Full source chain (all levels)
- Complete metadata
- Full backtrace (if
backtracefeature enabled) - Colors if
coloredfeature enabled AND TTY detected
Example output:
Error: NotFound
Code: NOT_FOUND
Message: User not found
Caused by: database error
Caused by: connection failed
Context:
user_id: 12345
Backtrace:
0: app::handlers::user at src/handlers.rs:42
MASTERROR_ENV=staging (Optional)
- JSON format with additional context
- Fields:
kind,code,message,source_chain(limited depth),metadata - NO backtrace
- NO colors
Example output:
{"kind":"NotFound","code":"NOT_FOUND","message":"User not found","source_chain":["database error","connection failed"],"metadata":{"user_id":12345}}Requirements
-
Separation of concerns:
MASTERROR_ENVcontrols structure and contentcoloredfeature controls coloring only- TTY detection auto-disables colors for non-terminal output
-
Auto-detection:
- Default to
prodifMASTERROR_ENVnot set and!cfg!(debug_assertions) - Default to
localifMASTERROR_ENVnot set andcfg!(debug_assertions) - Detect Kubernetes environment (
KUBERNETES_SERVICE_HOST) → forceprod
- Default to
-
Performance:
- Cache env variable check (use
OnceLockor similar) - Zero-alloc JSON formatting where possible
- No runtime overhead in hot paths
- Cache env variable check (use
-
Backward compatibility:
- Existing
coloredfeature behavior preserved - No breaking changes to public API
- Default behavior should work for most users
- Existing
-
Security:
- Respect
MessageEditPolicy::Redact - Filter sensitive metadata fields in all modes
- Never expose internal paths or backtraces in
prodmode
- Respect
Implementation Notes
- Add new module
src/app_error/display.rsfor display formatting logic - Implement
fmt_prod,fmt_local,fmt_stagingmethods - Modify
Displaytrait implementation to dispatch based on environment - Add comprehensive tests for all display modes
- Add doctests showing each mode
Testing
- Unit tests for each display mode
- Tests for env variable detection
- Tests for TTY detection
- Tests for metadata filtering in prod mode
- Tests for colored output (when feature enabled)
- Tests for security (no sensitive data leaks in prod)
Documentation
- Document
MASTERROR_ENVvariable in README - Add examples for each mode
- Migration guide for users (if needed)
Metadata
Metadata
Assignees
Labels
No labels