Create safe user error messages with correlation IDs

Turn internal backend exceptions into secure, user-facing error payloads paired with traceable request correlation IDs. Use it to build clean API error contracts.

You getPublic API Response, internal Server Log Entry and frontend Handling Note

Prompt

Prompt
Act as an API architect. Transform the provided internal exception or error scenario into a safe public API response paired with an internal logging structure.
- Never expose database schemas, internal hostnames, stack traces, or credentials to the user.
- Provide a human-readable, non-technical public message that tells the user what happened and what action to take.
- Create a structured JSON error payload including a unique correlation ID and ISO timestamp.
- Provide the corresponding structured server log entry that captures the raw internal error details tied to that same correlation ID.
Output format:
- Public API Response (JSON code block)
- Internal Server Log Entry (JSON code block)
- Frontend Handling Note (1-2 sentences on how the UI should display this to the user)

Here is the internal error details:

Example output

Example for: PostgreSQL duplicate key unique constraint violation on checkout

Public API Response:

{
  "error": {
    "code": "PAYMENT_CONFLICT",
    "message": "An order with this reference has already been submitted. Please check your order history before trying again.",
    "correlationId": "req_01HP89B4ZK2N7M9A1X5R",
    "timestamp": "2025-02-14T10:15:30.120Z"
  }
}

Internal Server Log Entry:

{
  "level": "warn",
  "correlationId": "req_01HP89B4ZK2N7M9A1X5R",
  "timestamp": "2025-02-14T10:15:30.120Z",
  "userId": "usr_88219",
  "error": "duplicate key value violates unique constraint 'orders_idempotency_key_unique'",
  "stack": "QueryFailedError: duplicate key value... at PostgresQueryRunner.query (/app/src/driver/postgres.js:210)"
}

Frontend Handling Note: Show an inline alert informing the customer of a duplicate submission, and provide a direct button linking to their order history alongside the correlation ID for support contact.

Written by AI from this prompt. Your results will vary.

Paste your raw exception, SQL error, or failure scenario directly after the prompt into Claude, ChatGPT, or Gemini. Follow up by asking for the corresponding TypeScript error interface or HTTP status code mapping.

Make it repeatable

Keep using this prompt without pasting it in every time.

Keep exploring

More in Coding