Skip to content

Add custom error types for catchable ORM failures#359

Merged
rsslldnphy merged 1 commit intomainfrom
custom-error-types
May 3, 2026
Merged

Add custom error types for catchable ORM failures#359
rsslldnphy merged 1 commit intomainfrom
custom-error-types

Conversation

@rsslldnphy
Copy link
Copy Markdown
Member

Summary

  • Adds an OrmError base class so consumers can catch any ORM-thrown error with instanceof.
  • Adds NotFoundError (single-row op found 0 rows), TooManyRowsError (single-row op found >1), and CreateFailedError (createOne got 0 rows back) — all extend OrmError.
  • Each carries modelName and operation; TooManyRowsError also exposes rowCount.
  • Replaces the generic throw new Error(...) calls in findOne, updateOne, deleteOne, and createOne with the new typed errors. Messages are preserved, so existing rejects.toThrow("...") assertions still match.
  • Other throw new Error sites (schema lookups, missing where clauses, connection-state checks, operator validation) were left as plain Error — they're programmer errors, not runtime conditions worth catching.
import { NotFoundError, TooManyRowsError, OrmError } from "@casekit/orm";

try {
    await db.findOne("user", { where: { id } });
} catch (e) {
    if (e instanceof NotFoundError) {
        // e.modelName, e.operation
    } else if (e instanceof OrmError) {
        // any ORM-thrown error
    }
}

Test plan

  • pnpm typecheck clean
  • All previously affected tests still pass (findOne, updateOne, deleteOne, createOne, middleware.where, findOne.select, findOne.where, findOne.includeManyToOne)
  • Reviewer sanity-check that the chosen error names and field shapes are what you want exposed publicly

Introduces an OrmError base class plus NotFoundError, TooManyRowsError,
and CreateFailedError so consumers can distinguish runtime row-count
failures from generic errors via instanceof. Each carries the modelName
and operation that caused it; TooManyRowsError also exposes rowCount.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 3, 2026

Coverage Report

  • orm-cli: 96.85%
  • orm-migrate: 94.02%
  • orm: 98.41%
  • sql: 97.03%
  • toolbox: 100%

@rsslldnphy rsslldnphy merged commit 4b71aa7 into main May 3, 2026
29 checks passed
@rsslldnphy rsslldnphy deleted the custom-error-types branch May 3, 2026 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant