Promptabide Logo

Prompting For Error Handling That Is Not Just try/catch

Named error classes, correlation IDs, and a rule about what never reaches the client.

backend
node
error-handling
reliability
Keywords:
error classes
express error middleware
correlation id
Ask for "error handling" and you get try/catch wrapped around everything with console.log in the catch. Ask for this instead:

"Add error handling to this module. Requirements:
  • • A small hierarchy: AppError base, then ValidationError, NotFoundError, ConflictError, ExternalServiceError

  • • Each carries an HTTP status and a machine-readable code

  • • One central Express error middleware maps them to responses

  • • Unknown errors become a 500 with a correlation ID; the real message goes to the logger, never to the client

  • • Stack traces only when NODE_ENV !== production

  • • External calls get a timeout and one retry with backoff; a retry exhaustion is an ExternalServiceError, not a 500"


  • The line that earns its place is "the real message goes to the logger, never to the client". Without it you leak table names in 500 bodies.
    7.6k8

    Generated Outputs (1)

    1 weeks ago
    Claude
    claude-opus-4
    Generated Output
    export class ConflictError extends AppError {
    constructor(message: string, code = "CONFLICT") {
    super(message, 409, code)
    }
    }

    // error middleware
    if (err instanceof AppError) {
    return res.status(err.status).json({ success: false, code: err.code, message: err.message })
    }
    const correlationId = randomUUID()
    logger.error({ correlationId, err })
    return res.status(500).json({ success: false, code: "INTERNAL", correlationId })
    Comments (8)
    No comments yet. Be the first to share your thoughts!
    Top Contributors
    Loading...
    Follow PromptAbide

    New bides, prompt breakdowns and community picks, on whichever feed you already read.

    Trending Tags
    Loading...
    Prompting For Error Handling That Is Not Just try/catch | PromptAbide