fix(server): surface the cause behind a persistence SQL error #4837
ClosedSy-D(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/4837/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
- apps/server/src/persistence/Errors.ts lines 99 to 102Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -73,3 +73,15 @@7373 const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);74747575 // Kept for orchestration/projection call sites, which are being revamped separately.76+/**77+ * A rejected payload must never reach diagnostics, so a schema failure78+ * contributes only its issue tags. A driver error names the SQLite condition79+ * and at most the constraint it violated, never a bound value, so its own80+ * message is safe to carry and is the part that makes an occurrence81+ * diagnosable at all.82+ */83+function describeSqlCause(cause: unknown): string | undefined {84+ if (Schema.isSchemaError(cause)) return summarizeSchemaIssue(cause.issue);85+ if (cause instanceof Error && cause.message.length > 0) return cause.message;86+ return undefined;87+}apps/server/src/persistence/Errors.test.ts lines 28 to 37OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -25,3 +25,13 @@2525 assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");2626 });272728+it("carries the driver failure into the mapped SQL error message", () => {29+ const cause = new Error("SQLITE_BUSY: database is locked");30+ const error = toPersistenceSqlError("OrchestrationCommandReceiptRepository.upsert:query")(cause);31+32+ assert.equal(33+ error.message,34+ "SQL error in OrchestrationCommandReceiptRepository.upsert:query: SQLITE_BUSY: database is locked",35+ );36+ assert.equal(error.cause, cause);37+});
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
- @@ -73,3 +73,19 @@7373 const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);74747575 // Kept for orchestration/projection call sites, which are being revamped separately.76+/**77+ * SQLite names its condition through a fixed result-code table, so the code and78+ * its canonical string are bounded and carry no query data. The driver message79+ * is not bounded: a constraint failure spells out the table and column it hit.80+ * Only the normalized pair is carried here; `cause` keeps everything else.81+ */82+function sqliteCondition(cause: unknown): string | undefined {83+ let value: unknown = cause;84+ for (let depth = 0; depth < 4 && typeof value === "object" && value !== null; depth += 1) {85+ const { errcode, errstr } = value as { errcode?: unknown; errstr?: unknown };86+ if (typeof errcode === "number" && typeof errstr === "string") {87+ return `SQLITE(${errcode}) ${errstr}`;88+ }89+ value = (value as { cause?: unknown }).cause;90+ }91+ return undefined;
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
209783da-9274-4582-a73b-01c07ae5ab05You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share