RespKit

Optional integrations

RespKit keeps framework and database dependencies out of its core module. Optional integrations live in separate Go modules and are imported only by applications that need them. They provide explicit mappings for typed errors from a particular framework, database, or driver.

Available database adapter: pgx

The pgx adapter maps PostgreSQL errors using pgx/v5. It does not open database connections or change RespKit's global configuration.

The core module already recognizes PostgreSQL SQLSTATE 23505 through the SQLState() string interface, without importing a database driver. The optional adapter maps additional SQLSTATEs and records the pgx mapper in diagnostics.

Install the adapter in addition to the core package:

go get github.com/ZihxS/RespKit/integrations/pgx

The adapter depends on pgx/v5 v5.11.0 and requires Go 1.25. This dependency applies only to the optional module; the core module supports Go 1.22 and later.

Configure the adapter on an engine:

import (
	pgxmapper "github.com/ZihxS/RespKit/integrations/pgx"
	"github.com/ZihxS/RespKit/resp"
)

api, err := resp.New(pgxmapper.Option())
if err != nil {
	return err
}

return api.Fail(ctx, dbErr)

PostgreSQL SQLSTATE mappings

SQLSTATECommon conditionRespKit codeHTTP status
22003, 22007, 22008, 22018, 22021, 22023, 22P02, 22P03Invalid numeric, date/time, text, or binary inputBAD_REQUEST400
22001Text value exceeds the column lengthVALIDATION_FAILED422
23502, 23503, 23514Not-null, foreign-key, or check constraint violationVALIDATION_FAILED422
23505Unique constraint violationRESOURCE_ALREADY_EXISTS409
23000, 23001, 23P01Integrity, restrict, or exclusion conflictRESOURCE_CONFLICT409
40000, 40001, 40P01Transaction rollback, serialization failure, or deadlockRESOURCE_CONFLICT409
08000, 08001, 08003, 08004, 08006, 08007Database connection failureSERVICE_UNAVAILABLE503
53000, 53100, 53200, 53300, 53400Insufficient resources or a server configuration limitSERVICE_UNAVAILABLE503
57P01, 57P02, 57P03Database shutdown or not accepting connectionsSERVICE_UNAVAILABLE503
55P03Lock not availableRESOURCE_CONFLICT409

Other current or future PostgreSQL codes map conservatively to INTERNAL_ERROR (500). This avoids returning a client error for database bugs, authentication or configuration problems, protocol failures, or internal conditions. The raw PostgreSQL message is never included in the client response; SQLSTATE remains available in diagnostics.

Adding another integration

New integrations should have a clear source error type and deterministic mappings. Keep third-party dependencies in a separate module, make the integration opt-in, and test wrapped errors and fallback behavior. Do not classify errors based only on their message text.

On this page