feat: typed constraint errors (unique, not-null, fk, check) - #6154
Open
adsqx wants to merge 2 commits into
Open
Conversation
Map Postgres SQLSTATE, MySQL/SingleStore errno and SQLite SQLITE_CONSTRAINT_* (plus libsql message fallback) onto Unique/NotNull/ForeignKey/Check subclasses of DrizzleQueryError. Wire wrapQueryError in every dialect queryWithCache so gel and singlestore get the same mapping as pg/mysql/sqlite. Unknown errors still become DrizzleQueryError.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
/claim #376
Fixes #376
feat: typed constraint errors across pg, mysql, sqlite, and singlestore
This builds on the typed-error hierarchy explored in #5383 and keeps its public class names, while completing SingleStore wiring, repo-standard
is()support, delete-side MySQL FK errors, and safe handling of non-Errorthrows.Catching a unique-violation today means parsing driver-specific
code/errno/ message strings. This wraps those into one typedinstanceof/is()chain that still keeps the original driver error on.cause.DrizzleQueryErrorkeeps the same behavior for everything that is not a constraint failure (syntax, connection, missing relation, …). Additive — no existing catch ofDrizzleQueryErrorbreaks.Mapping
23505235022350323514106210481451/1452/1216/12173819SQLITE_CONSTRAINT_UNIQUE/PRIMARYKEY+ libsql messageNOTNULLFOREIGNKEYCHECKWired in the one place every driver already goes through:
queryWithCacheinpg-core,mysql-core,sqlite-core, andsinglestore-core.Classes are exported from
drizzle-orm(export * from './errors.ts'already).Related open work
is(), complete MySQL FK extraction/delete codes, and non-Errorsafetytry/catcharound a thenable does not cover async rejectionsonErrorhook and changes 68 files; broader, separate scopeonErrorcallback rather than typed constraint classesNo
onErrorhook in this PR. Constraint classes first; a hook can sit on top later.Tests
drizzle-orm/tests/errors.test.ts— 22 cases, no live database: SQLSTATE, mysql errno (including FK table/column/constraint extraction), sqlite codes + libsql message fallback, unknown codes stayDrizzleQueryError,instanceof/is()behavior, and non-Errorthrows.