export class AppError extends Error { constructor(code, message, status = 400, details = {}) { super(message); this.name = 'AppError'; this.code = code; this.status = status; this.details = details; } } export function toErrorEnvelope(error, requestId) { const status = error?.status && Number.isInteger(error.status) ? error.status : 500; const code = error?.code || 'INTERNAL_ERROR'; const message = error?.message || 'Unexpected error'; const details = error?.details || {}; return { status, body: { code, message, details, requestId, }, }; }