22 KiB
SqlError Reason Pattern
Overview
Refactor SqlError to follow the reason pattern used by AiError and
WorkerError. The new design introduces per-reason error classes and updates
SqlError to wrap a reason field instead of a flat cause/message pair.
This enables ergonomic error handling via Effect.catchReason and structured
error classification across all SQL drivers.
Goals
- Replace the flat
cause/messagefields with areasonunion of semantic error classes. - Cover the most common cross-database error categories: connection, authentication, authorization, syntax, constraints, deadlocks, serialization failures, lock timeouts, statement timeouts, and unknown.
- Add an
operationfield (free-form string) so callers know what operation was being attempted. - Add
isRetryableto each reason indicating transient vs permanent errors. - Update all 11 SQL driver packages to classify native errors into reasons on a
best-effort basis, falling back to
UnknownErrorfor unrecognized codes.
Non-Goals
- Exhaustive error code mapping for every database engine.
- Adding retry policies or backoff logic to the SQL layer.
- Changing
ResultLengthMismatch(it remains a separate error class). - Changing SQL client behavior beyond error construction.
- Modifying the
SqlConnection,Statement, orSqlClientinterfaces (they already useSqlErroras their error type).
Current State
SqlError is a Schema.TaggedErrorClass with two fields:
export class SqlError extends Schema.TaggedErrorClass<SqlError>(
"effect/sql/SqlError"
)("SqlError", {
cause: Schema.Defect,
message: Schema.optional(Schema.String)
}) {
readonly [TypeId] = TypeId
}
All 62 instantiation sites across 11 driver packages use
new SqlError({ cause, message: "..." }). The message field is a
human-readable string like "Failed to execute statement" or
"PgClient: Failed to connect". The cause field holds the raw native driver
error. There is no error classification, no retryability indication, and no
structured reason.
Proposed Design
Reason Classes
Define reason classes in packages/effect/src/unstable/sql/SqlError.ts using
Schema.TaggedErrorClass, which defines _tag automatically via its second
argument. Each reason class:
- Has a unique
_tag(set bySchema.TaggedErrorClass's tag argument). - Includes
cause: Schema.Defect(the underlying native error). - Includes
message: Schema.optional(Schema.String)for human-readable context. - Includes
operation: Schema.optional(Schema.String)describing what was being attempted (e.g."connect","execute","beginTransaction"). - Exposes an
isRetryablegetter. - Has a
[ReasonTypeId]brand. - Includes
@since 4.0.0JSDoc tags on all exports.
Reason List
| Class | _tag |
isRetryable |
Description |
|---|---|---|---|
ConnectionError |
"ConnectionError" |
true |
Failed to connect to the database (refused, DNS, timeout, pool exhaustion) |
AuthenticationError |
"AuthenticationError" |
false |
Invalid credentials |
AuthorizationError |
"AuthorizationError" |
false |
Insufficient privileges for the operation |
SqlSyntaxError |
"SqlSyntaxError" |
false |
SQL syntax or semantic error (bad table name, invalid column, etc.) |
ConstraintError |
"ConstraintError" |
false |
Constraint violation (unique, foreign key, check, not null) |
DeadlockError |
"DeadlockError" |
true |
Deadlock detected, transaction was aborted |
SerializationError |
"SerializationError" |
true |
Serialization failure in a serializable transaction |
LockTimeoutError |
"LockTimeoutError" |
true |
Lock wait timeout exceeded |
StatementTimeoutError |
"StatementTimeoutError" |
true |
Statement execution time exceeded configured timeout |
UnknownError |
"UnknownError" |
false |
Catch-all for errors that cannot be classified |
Reason Class Template
Each reason class follows this structure:
export class ConnectionError extends Schema.TaggedErrorClass<ConnectionError>(
"effect/sql/SqlError/ConnectionError"
)("ConnectionError", {
cause: Schema.Defect,
message: Schema.optional(Schema.String),
operation: Schema.optional(Schema.String)
}) {
readonly [ReasonTypeId] = ReasonTypeId
get isRetryable(): boolean {
return true
}
}
ReasonTypeId
Add a ReasonTypeId constant:
const ReasonTypeId = "~effect/sql/SqlError/Reason" as const
Reason Union
export type SqlErrorReason =
| ConnectionError
| AuthenticationError
| AuthorizationError
| SqlSyntaxError
| ConstraintError
| DeadlockError
| SerializationError
| LockTimeoutError
| StatementTimeoutError
| UnknownError
export const SqlErrorReason: Schema.Union<[
typeof ConnectionError,
typeof AuthenticationError,
typeof AuthorizationError,
typeof SqlSyntaxError,
typeof ConstraintError,
typeof DeadlockError,
typeof SerializationError,
typeof LockTimeoutError,
typeof StatementTimeoutError,
typeof UnknownError
]> = Schema.Union([
ConnectionError,
AuthenticationError,
AuthorizationError,
SqlSyntaxError,
ConstraintError,
DeadlockError,
SerializationError,
LockTimeoutError,
StatementTimeoutError,
UnknownError
])
Updated SqlError
Keep SqlError as Schema.TaggedErrorClass (matching the current pattern).
Set cause on the wrapper via override readonly cause = this.reason
(matching AiError):
export class SqlError extends Schema.TaggedErrorClass<SqlError>(
"effect/sql/SqlError"
)("SqlError", {
reason: SqlErrorReason
}) {
readonly [TypeId] = TypeId
override readonly cause = this.reason
override get message(): string {
return this.reason.message ?? this.reason._tag
}
get isRetryable(): boolean {
return this.reason.isRetryable
}
}
Key changes:
causeandmessageremoved fromSqlErrorschema fields.messagedelegates toreason.message, falling back toreason._tag.isRetryabledelegates toreason.isRetryable.causeset tothis.reasonvia override (matchingAiErrorpattern at line 1400), ensuringError.causeis set for stack trace chaining.TypeIdstring remains"~effect/sql/SqlError"for guard compatibility.
Type Guards
Add isSqlError and isSqlErrorReason guards:
export const isSqlError = (u: unknown): u is SqlError =>
Predicate.hasProperty(u, TypeId)
export const isSqlErrorReason = (u: unknown): u is SqlErrorReason =>
Predicate.hasProperty(u, ReasonTypeId)
Note: Predicate must be added to the imports in SqlError.ts.
Construction Pattern
Before:
new SqlError({ cause, message: "Failed to execute statement" })
After:
new SqlError({
reason: new UnknownError({
cause,
message: "Failed to execute statement",
operation: "execute"
})
})
With classification:
new SqlError({
reason: new ConnectionError({
cause,
message: "PgClient: Failed to connect",
operation: "connect"
})
})
Recommended Operation Values
While operation is a free-form string, drivers should use these canonical
values for consistency:
"connect"— establishing a database connection"acquireConnection"— acquiring a connection from the pool"execute"— executing a prepared statement"executeUnprepared"— executing an unprepared statement"stream"— streaming query results"prepare"— preparing a statement"beginTransaction"— beginning a transaction"commitTransaction"— committing a transaction"rollbackTransaction"— rolling back a transaction"createSavepoint"— creating a savepoint"listen"— subscribing to notifications (PostgreSQL)"notify"— sending a notification (PostgreSQL)"export"— exporting a database"import"— importing a database"backup"— backing up a database"loadExtension"— loading a SQLite extension"insert"— inserting data (ClickHouse batch insert)"openDatabase"— opening a database file"parseRow"— parsing a result row
Breaking Change Notice
This is a breaking change to SqlError's constructor and serialized shape.
The encoded JSON changes from {_tag, cause, message} to
{_tag, reason: {_tag, cause, message, operation}}. Since SqlError lives
under effect/unstable/sql, this is acceptable under the unstable API policy.
Changesets should use minor version bumps.
Driver Error Classification
Each driver should implement a local classifyError helper function that
inspects the native error and returns the appropriate reason class instance.
The classification is best-effort: drivers should map well-known error codes
and fall back to UnknownError for anything unrecognized.
Shared SQLite Classifier
Since 6 driver packages use SQLite-based databases (sqlite-node, sqlite-bun,
sqlite-wasm, sqlite-do, sqlite-react-native, d1), a shared
classifySqliteError helper should be exported from
packages/effect/src/unstable/sql/SqlError.ts. This helper inspects the
error's code property (string like "SQLITE_CONSTRAINT") or numeric code
and returns the appropriate reason. Drivers that don't expose structured codes
(D1, Durable Objects) should fall back to UnknownError.
PostgreSQL (@effect/sql-pg)
PostgreSQL errors expose a code field with SQLSTATE codes:
| SQLSTATE | Reason |
|---|---|
08* (connection exception) |
ConnectionError |
28* (invalid authorization) |
AuthenticationError |
42501 (insufficient privilege) |
AuthorizationError |
42* (syntax error or access rule violation, except 42501) |
SqlSyntaxError |
23* (integrity constraint violation) |
ConstraintError |
40P01 (deadlock detected) |
DeadlockError |
40001 (serialization failure) |
SerializationError |
55P03 (lock not available) |
LockTimeoutError |
57014 (query cancelled / statement timeout) |
StatementTimeoutError |
| Everything else | UnknownError |
Note: 42501 must be checked before the general 42* prefix to ensure it
maps to AuthorizationError rather than SqlSyntaxError.
MySQL (@effect/sql-mysql2)
MySQL errors expose an errno field:
| errno | Reason |
|---|---|
1040 (too many connections) |
ConnectionError |
1042, 1043 (can't get hostname, bad handshake) |
ConnectionError |
1129, 1130 (host blocked, host not allowed) |
ConnectionError |
1203 (max user connections) |
ConnectionError |
1044 (access denied to database) |
AuthorizationError |
1045 (access denied for user) |
AuthenticationError |
1142, 1143, 1227 (insufficient privileges) |
AuthorizationError |
1064 (syntax error) |
SqlSyntaxError |
1146 (table doesn't exist) |
SqlSyntaxError |
1054 (unknown column) |
SqlSyntaxError |
1022, 1048, 1062, 1169 (constraint violations) |
ConstraintError |
1216, 1217, 1451, 1452, 1557 (FK violations) |
ConstraintError |
1213 (deadlock) |
DeadlockError |
1205 (lock wait timeout) |
LockTimeoutError |
3024 (query execution interrupted / timeout) |
StatementTimeoutError |
| Everything else | UnknownError |
Note: MySQL does not distinguish serialization failures from deadlocks; both
use errno 1213. SerializationError is not mapped for MySQL.
MSSQL (@effect/sql-mssql)
MSSQL errors (from tedious) expose a number field:
| Error number | Reason |
|---|---|
233 (connection closed) |
ConnectionError |
10054 (connection forcibly closed) |
ConnectionError |
18456 (login failed) |
AuthenticationError |
18452 (login failed, untrusted domain) |
AuthenticationError |
4060 (cannot open database) |
AuthenticationError |
229, 230, 262, 297, 300 (permission denied) |
AuthorizationError |
102 (syntax error) |
SqlSyntaxError |
207 (invalid column name) |
SqlSyntaxError |
208 (invalid object name) |
SqlSyntaxError |
2714 (object already exists) |
SqlSyntaxError |
547 (FK constraint) |
ConstraintError |
2601 (unique index violation) |
ConstraintError |
2627 (unique constraint violation) |
ConstraintError |
515 (cannot insert NULL) |
ConstraintError |
1205 (deadlock victim) |
DeadlockError |
3960 (snapshot isolation conflict) |
SerializationError |
1222 (lock request timeout) |
LockTimeoutError |
| Everything else | UnknownError |
For connection-level errors (timeout during pool connect), use
ConnectionError regardless of error number.
SQLite (all SQLite drivers)
SQLite errors expose a code string property (e.g. "SQLITE_CONSTRAINT")
and/or a numeric result code:
| Code (string / numeric) | Reason |
|---|---|
SQLITE_AUTH / 23 |
AuthenticationError |
SQLITE_PERM / 3 |
AuthorizationError |
SQLITE_CONSTRAINT / 19 (and extended codes like SQLITE_CONSTRAINT_UNIQUE) |
ConstraintError |
SQLITE_BUSY / 5 |
LockTimeoutError |
SQLITE_LOCKED / 6 |
LockTimeoutError |
SQLITE_CANTOPEN / 14 |
ConnectionError |
SQLITE_ERROR / 1 |
UnknownError |
| Everything else | UnknownError |
Notes:
SQLITE_AUTHis numeric code23, not14.SQLITE_LOCKED(code6) maps toLockTimeoutError(notDeadlockError), because SQLite uses a simpler locking model without true deadlock detection.SQLITE_ERROR(code1) is too generic for reliable syntax detection via message parsing. Default toUnknownError.- Drivers that don't expose structured error codes (D1, Durable Objects)
should default to
UnknownError.
ClickHouse (@effect/sql-clickhouse)
ClickHouse errors from @clickhouse/client expose error codes:
| Code | Reason |
|---|---|
516 (authentication failed) |
AuthenticationError |
497 (access denied) |
AuthorizationError |
62 (syntax error) |
SqlSyntaxError |
60 (table not found) |
SqlSyntaxError |
36 (invalid column) |
SqlSyntaxError |
242 (table already exists) |
SqlSyntaxError |
159, 469 (timeout) |
StatementTimeoutError |
| Connection-level errors (timeout during connect) | ConnectionError |
| Everything else | UnknownError |
LibSQL (@effect/sql-libsql)
LibSQL errors from @libsql/client wrap SQLite error codes. Use the shared
classifySqliteError helper where the underlying code is accessible, falling
back to UnknownError.
Impacted Files
Core (effect package)
packages/effect/src/unstable/sql/SqlError.ts— reason classes, updatedSqlError, sharedclassifySqliteErrorhelper, type guards
Driver Packages
packages/sql/pg/src/PgClient.tspackages/sql/mysql2/src/MysqlClient.tspackages/sql/mssql/src/MssqlClient.tspackages/sql/sqlite-node/src/SqliteClient.tspackages/sql/sqlite-bun/src/SqliteClient.tspackages/sql/sqlite-wasm/src/SqliteClient.tspackages/sql/sqlite-wasm/src/OpfsWorker.tspackages/sql/sqlite-do/src/SqliteClient.tspackages/sql/sqlite-react-native/src/SqliteClient.tspackages/sql/d1/src/D1Client.tspackages/sql/libsql/src/LibsqlClient.tspackages/sql/clickhouse/src/ClickhouseClient.ts
Tests
packages/effect/test/unstable/sql/SqlError.test.ts(new)- Existing driver test files that assert on
SqlErrorinstances
Implementation Plan
Changing SqlError's constructor signature is a breaking change that affects
all 62 call sites across 11 driver packages. Since pnpm check:tsgo runs
across the full monorepo, the core change and all driver updates must be
done together to maintain a passing build.
Task 1: Core SqlError + all drivers + tests + changesets
This is a single atomic task. All changes must land together for the build to pass.
Task State (core checkpoint)
- Core
SqlError.tsreason-pattern refactor (items 1-9) completed. - Driver updates (items 10-12) completed across all 11 SQL driver packages.
- Tests and changesets (items 13-16) completed (core and targeted driver classification tests landed; pending minor changesets added for
effectand all affected SQL driver packages). - Full monorepo validation (items 17-21) completed (
pnpm codegen,pnpm lint-fix,pnpm check:tsgo,pnpm docgen, and fullpnpm testsweep all pass).
Discoveries / Issues
- The previous 62 SQL driver
new SqlError({ cause, message })call sites were migrated tonew SqlError({ reason: ... })with per-driver classification. ResultLengthMismatchshares the SqlError module TypeId brand, soisSqlErrormust also check_tag === "SqlError"to avoid false positives.- Added core smoke tests in
packages/effect/test/unstable/sql/SqlError.test.tsfor wrapper delegation, guards, andclassifySqliteErrorstring/numeric mappings. - SQLite-based drivers now use
classifySqliteErrorwith canonical operations (prepare,execute,openDatabase,export,import,backup,loadExtension,stream); D1 intentionally defaults toUnknownErrorbecause native D1 errors do not expose stable SQLite result codes. - Added per-driver
classifyErrorhelpers for pg / mysql2 / mssql / clickhouse with SQLSTATE / errno / number / code mappings from this spec andUnknownErrorfallback for unmapped native errors. - Expanded
packages/effect/test/unstable/sql/SqlError.test.tswith exhaustive assertions for all 10 reason classes, wrapper delegation checks formessage/cause/isRetryable, and schema encode/decode round-trips forSqlErrorwrapping each reason. - Added targeted driver classification suites:
packages/sql/pg/test/SqlErrorClassification.test.tsverifies SQLSTATE precedence (42501before generic42*) and unknown fallback.packages/sql/mysql2/test/SqlErrorClassification.test.tsverifies errno mapping for connection / auth / authz / syntax / constraint / deadlock / lock-timeout / statement-timeout plus unknown fallback.packages/sql/mssql/test/SqlErrorClassification.test.tsverifiesnumbermapping for connection / auth / authz / syntax / constraint / deadlock / serialization / lock-timeout plus unknown fallback.packages/sql/clickhouse/test/SqlErrorClassification.test.tsverifiescodemapping for auth / authz / syntax / statement-timeout and unknown fallback on execute.packages/sql/d1/test/Client.test.tsandpackages/sql/sqlite-do/test/Client.test.tsnow assertUnknownErrorfallback when native errors do not expose stable SQLite codes.
- Added 12 pending minor changesets for the SqlError reason-pattern migration:
one for
effectand one per affected SQL driver package (pg / mysql2 / mssql / sqlite-node / sqlite-bun / sqlite-wasm / sqlite-do / sqlite-react-native / d1 / libsql / clickhouse). - Post-changeset validation rerun completed:
pnpm lint-fix,pnpm test packages/effect/test/unstable/sql/SqlError.test.ts,pnpm check:tsgo, andpnpm docgen. - Review verification: targeted suites pass via
pnpm testfor pg / mysql2 / mssql / clickhouse / d1 / sqlite-do classification test files. - Full monorepo
pnpm testinitially failed in@effect/sql-libsqldue transient testcontainer connectivity (ECONNREFUSED) in this environment.packages/sql/libsql/test/util.tsnow usescontainer.getHost()(instead of hardcodedlocalhost) and waits for server readiness after container start, stabilizing libsql integration tests while preserving SqlError behavior.
Core changes (packages/effect/src/unstable/sql/SqlError.ts):
- Add
ReasonTypeIdconstant. - Add
Predicateimport. - Define all 10 reason classes (
ConnectionError,AuthenticationError,AuthorizationError,SqlSyntaxError,ConstraintError,DeadlockError,SerializationError,LockTimeoutError,StatementTimeoutError,UnknownError) usingSchema.TaggedErrorClass. - Define
SqlErrorReasontype alias and schema union (using array syntaxSchema.Union([...])). - Update
SqlErrorto useSchema.TaggedErrorClasswithreason: SqlErrorReason(replacingcause/messagefields). - Add
message,isRetryablegetters andcauseoverride that delegate to the reason. - Add
isSqlErrorandisSqlErrorReasontype guards. - Add shared
classifySqliteErrorhelper for SQLite-based drivers. - Add
@since 4.0.0JSDoc tags on all new exports.
Driver updates (all driver files listed in Impacted Files):
- For each driver, add a local
classifyErrorhelper (or use the shared SQLite helper) that maps native error codes to reason classes. - Update all
new SqlError({ cause, message })call sites tonew SqlError({ reason: new <ReasonClass>({ cause, message, operation }) }). - Use the error code mappings defined in the Driver Error Classification section.
Tests:
- Add
packages/effect/test/unstable/sql/SqlError.test.tscovering:- Reason construction and
isRetryablevalues for all 10 reason classes SqlErrorwrapper:messagedelegation,isRetryabledelegation,causedelegationisSqlErrorandisSqlErrorReasonguards- Schema encode/decode round-trips for
SqlErrorwith each reason type
- Reason construction and
- Update any existing driver tests that assert on
SqlErrorshape (e.g.error.cause,error.message).
Changesets:
- Add changeset for
effect(minor):SqlErrornow uses the reason pattern with structured error classification. - Add changesets for each SQL driver package (minor): driver now classifies
native errors into
SqlErrorreason types.
Validation:
pnpm codegen(regenerate barrel files)pnpm lint-fixpnpm check:tsgo(runpnpm cleanfirst if needed)pnpm docgenpnpm test
Test Plan
- Unit tests for all 10 reason classes: construction,
isRetryablevalues,messagegetters, Schema encode/decode round-trips. - Unit tests for
SqlErrorwrapper:messagedelegation,isRetryabledelegation,causeoverride,isSqlErrorandisSqlErrorReasonguards. - Integration: existing driver tests pass with the new error structure.
Validation
pnpm codegenpnpm lint-fixpnpm check:tsgo(runpnpm cleanif check fails)pnpm test <affected_test_file.ts>pnpm docgen
Review Follow-up Tasks
- Run full monorepo
pnpm testsweep (beyond targeted suites) before merge. - Monitor CI stability for
@effect/sql-libsqlcontainer startup timing; if flakiness persists, replace ad-hoc readiness polling with a dedicated testcontainers wait strategy / healthcheck endpoint.