Skip to content

precondition() should be an error thrown #649

Description

@herzi

Describe the issue

PostgresRow.decode(_:context:file:line) triggers a precondition failure when the number of column types does not match the number of returned column

Vapor version

vapor=n/a postgres-nio=1.33.0

Operating system and version

macOS 26.5.1

Swift version

swift-driver version: 1.148.6 Apple Swift version 6.3.3 (swiftlang-6.3.3.1.3 clang-2100.1.1.101)

Steps to reproduce

Use a code snippet like this to iterate a mismatch query/decode:

let client: PostgresClient // populate however you prefer
try await client.withConnection { connection in
  // note how the query just returns one value
  for try await row in try await connection.query("SELECT now() as time", logger: logger) {
    // note how we try to decode more than one column
    try row.decode((Date, String).self) 
  }
}

Expected outcomes:

  1. Code compiles
  2. Code runs and throws an error

Outcome

Observed outcomes:

  1. Code compiles
  2. Code crashes with PostgresNIO/VariadicGenerics.swift:56: Precondition failed (which also isn't a very good error message for this thing happening)

Additional notes

While this is clearly a programming error (on my side), I think it would be more consistently handled with the rest of the framework's behavior. A programming error is also an invalid SQL query, however that will throw PSQLError, so that other tasks in a Swift-based server can continue.

More importantly, in a multi-service environment, throwing an error will at least provide a way to shut down other services cleanly compared to a hard crash that might lose data in-flight.

NOTE: If maintainers agree that this is a useful improvement, I will happily work on a PR to implement this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions