Skip to content

Improve error handling documentation and TypeScript support #1132

@danielo515

Description

@danielo515

Hello and thank you for your time and dedication to postgress-js.

The current error handling implementation uses Object.assign to dynamically add properties to error objects (see errors.js). While the README mentions following the PostgreSQL error format, it doesn't document:

  • Which property contains the error code
  • The complete structure of error properties
  • How to properly type-check these errors in TypeScript

Suggestions

1. Enhanced Documentation

Document the complete error object structure, including:

  • Available properties (code, message, detail, hint, etc.)
  • Which properties are guaranteed vs optional
  • Examples of accessing error information

2. Class-Based Error Types

Consider replacing Object.assign with proper error classes:

class PostgresError extends Error {
  code: string;
  severity: string;
  detail?: string;
  hint?: string;
  // ... other well-defined properties
}

I know there is already a PostgressError extending error, but it is not clear what it is use for (I only saw one or two references in the entire codebase) and everything else is just Error.generic which has all the problems I mentioned.

Benefits:

  • Better TypeScript autocomplete and type safety
  • Clear API surface for error handling
  • Improved developer experience

This would significantly improve the TypeScript developer experience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions