ApexIntermediaterest-apiintegrationerror-handling
Design Apex-friendly error envelope for external REST consumers
Real World Scenario
Partners parse Apex REST error bodies inconsistently. Some get generic 500 with stack trace fragments exposing internal class names.
Expected Answer
• Consistent JSON error schema: code, message, correlationId, details array
• Map domain exceptions to HTTP 400; unexpected to 500 without stack in body
• Correlation Id in header and body for support cross-reference
• Validation errors include field-level detail array for partner mapping
• Log full exception server-side only with correlation Id link
• Version error schema in API /v1/ documentation
• Test error paths: 401, 403, 404, 409, 422 with assert on JSON shape
Follow-Up Questions & Answers
Click to expand — each follow-up includes a direct, interview-ready answer
Direct answer: Consistent JSON error schema: code, message, correlationId, details array Also consider: Map domain exceptions to HTTP 400; unexpected to 500 without stack in body In practice: Correlation Id in header and body for support cross-reference Balance speed of delivery with maintainability.
Architect Perspective
API errors are contract—publish error schema alongside success schema in partner docs.