Skip to content

Create specializations for common Database exceptions. #3282

Closed

Description

@SandPod

Problem to Solve

When a database query exception occurs, a lot of knowledge is required to understand what common exceptions are good to handle when performing database calls.

The possible exceptions thrown from Postgres are listed here: https://www.postgresql.org/docs/current/errcodes-appendix.html

Although there is good external documentation and guides around this, the framework has an opportunity to guide developers around which of these are to be expected from common operations.

Proposal

Introduce specializations for common database query exceptions that allow these to be explicitly cached in a try-catch statement.

Before specialization:

try {
  await MyModel.db.inserRow(session, userInfo);
} on DatabaseQueryException catch e {
  if (e.code != PgErrorCode.foreignKeViolation) rethrow; // Note that the rethrow here is important otherwise all DatabaseQueryExceptions would be silenced.

  // Handling of already existing user during insert...
}

After specialization:

try {
  await MyModel.db.inserRow(session, userInfo);
} on UniqueViolationQueryException {
  // Handling of already existing user during insert...
}

This, combined with an added description for all database operation methods informing the user about the exception structure, would make it easy to understand what exception handling is required.

Use Case

As suggested in the proposal, handling multiple entries into a table with a unique index would be greatly alleviated.

Alternatives

N/A

Additional context

No response

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    area: database
    enhancement
    on Mar 4, 2025
  2. moved this from Backlog 🗂️ to Done 🚀 in Serverpod Frameworkson Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: databaseenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions