Skip to content

Commit 0fad04b

Browse files
authored
fix(server): answer handler defects with a readable 500 instead of an empty one (#64022)
1 parent d5ea1ee commit 0fad04b

11 files changed

Lines changed: 319 additions & 9 deletions

File tree

‎packages/core/src/database/drizzle/effect-sqlite/session.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Exit from "effect/Exit"
55
import * as Scope from "effect/Scope"
66
import type { SqlClient } from "effect/sql/SqlClient"
77
import type { SqlError } from "effect/sql/SqlError"
8+
import { StorageFailure } from "../../storage-failure.js"
89
import type { EffectCacheShape } from "drizzle-orm/cache/core/cache-effect"
910
import type { WithCacheConfig } from "drizzle-orm/cache/core/types"
1011
import type { EffectDrizzleQueryError } from "drizzle-orm/effect-core/errors"
@@ -186,13 +187,23 @@ export class EffectSQLiteSession<TRelations extends AnyRelations> extends SQLite
186187
),
187188
)
188189
: this.executeTransactionStatement(connection, `release savepoint effect_sql_${id}`)
189-
: id === 0
190-
? this.executeTransactionStatement(connection, "rollback")
191-
: this.executeTransactionStatement(connection, `rollback to savepoint effect_sql_${id}`).pipe(
192-
Effect.andThen(
193-
this.executeTransactionStatement(connection, `release savepoint effect_sql_${id}`),
194-
),
195-
)
190+
: (id === 0
191+
? this.executeTransactionStatement(connection, "rollback")
192+
: this.executeTransactionStatement(connection, `rollback to savepoint effect_sql_${id}`).pipe(
193+
Effect.andThen(
194+
this.executeTransactionStatement(connection, `release savepoint effect_sql_${id}`),
195+
),
196+
)
197+
).pipe(
198+
// After errors such as SQLITE_FULL, SQLite rolls the whole transaction back itself, so this
199+
// rollback has nothing to undo and fails. Report the body's error, not that one. Any other
200+
// rollback failure still fails.
201+
Effect.catch((error) =>
202+
Exit.isFailure(exit) && StorageFailure.rolledBack(exit.cause)
203+
? Effect.void
204+
: Effect.fail(error),
205+
),
206+
)
196207

197208
return finalize.pipe(Effect.flatMap(() => exit))
198209
}),

‎packages/core/src/database/sqlite.bun.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SQLiteError } from "bun:sqlite"
12
import { SqliteClient } from "@effect/sql-sqlite-bun"
23
import { Layer } from "effect"
34
import type { SqlClient } from "effect/sql"
@@ -11,3 +12,6 @@ export const supportsForeignKeyToggle = true
1112
// busyTimeout; Database.layer treats both as fatal.
1213
export const sqliteLayer = (config: SqliteClient.SqliteClientConfig): Layer.Layer<SqlClient.SqlClient> =>
1314
Layer.orDie(SqliteClient.layer(config))
15+
16+
// bun:sqlite throws SQLiteError, whose errno is SQLite's extended result code.
17+
export const resultCode = (cause: unknown) => (cause instanceof SQLiteError ? cause.errno : undefined)

‎packages/core/src/database/sqlite.node.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SqliteClient } from "@effect/sql-sqlite-node"
2-
import { Layer } from "effect"
2+
import { Layer, Option, Schema } from "effect"
33
import type { SqlClient } from "effect/sql"
44

55
export const supportsTuningPragmas = true
@@ -11,3 +11,15 @@ export const supportsForeignKeyToggle = true
1111
// busyTimeout; Database.layer treats both as fatal.
1212
export const sqliteLayer = (config: SqliteClient.SqliteClientConfig): Layer.Layer<SqlClient.SqlClient> =>
1313
Layer.orDie(SqliteClient.layer(config))
14+
15+
// node:sqlite has no error class: SQLite failures are an Error marked ERR_SQLITE_ERROR that carries SQLite's extended
16+
// result code in errcode.
17+
const decodeDriverError = Schema.decodeUnknownOption(
18+
Schema.Struct({ code: Schema.Literal("ERR_SQLITE_ERROR"), errcode: Schema.Number }),
19+
)
20+
21+
export const resultCode = (cause: unknown) =>
22+
decodeDriverError(cause).pipe(
23+
Option.map((error) => error.errcode),
24+
Option.getOrUndefined,
25+
)

‎packages/core/src/database/sqlite.workerd.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,6 @@ export const sqliteLayer = (config: Config): Layer.Layer<SqlClient.SqlClient> =>
221221
),
222222
),
223223
)
224+
225+
// Durable Object SQLite throws a plain Error without SQLite result codes.
226+
export const resultCode = (_cause: unknown): number | undefined => undefined
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export * as StorageFailure from "./storage-failure.js"
2+
3+
import { resultCode } from "#sqlite"
4+
import { EffectDrizzleQueryError } from "drizzle-orm/effect-core/errors"
5+
import { Cause } from "effect"
6+
import { isSqlError, type SqlError } from "effect/sql/SqlError"
7+
8+
// SQLite primary result codes that mean storage itself can't be used, as opposed to a bad statement or a constraint,
9+
// each with a message that doesn't name the database engine, since callers only know about "storage". BUSY and LOCKED
10+
// only surface once a write has already waited out `busy_timeout`, so the write is lost just like on a full disk.
11+
const messages = new Map([
12+
[5, "Storage is in use by another process"], // SQLITE_BUSY
13+
[6, "Storage is in use by another process"], // SQLITE_LOCKED
14+
[8, "Storage is read-only"], // SQLITE_READONLY
15+
[10, "Storage couldn't be read or written"], // SQLITE_IOERR
16+
[11, "Storage is damaged"], // SQLITE_CORRUPT
17+
[13, "Storage is full"], // SQLITE_FULL
18+
[14, "Storage couldn't be opened"], // SQLITE_CANTOPEN
19+
[26, "Storage is damaged"], // SQLITE_NOTADB
20+
])
21+
22+
/** The readable message for the storage failure that failed an effect, if that is what failed it. */
23+
export function message(cause: Cause.Cause<unknown>) {
24+
return sqlErrors(cause)
25+
.map(fromSqlError)
26+
.find((found) => found !== undefined)
27+
}
28+
29+
/** The readable message for a SqlError the driver reported as a storage failure. */
30+
export function fromSqlError(error: SqlError) {
31+
const code = resultCode(error.reason.cause)
32+
// An extended result code (SQLITE_IOERR_WRITE) keeps its primary code in the low byte.
33+
return code === undefined ? undefined : messages.get(code & 0xff)
34+
}
35+
36+
// Result codes after which SQLite may have rolled the whole transaction back itself
37+
// (https://www.sqlite.org/lang_transaction.html#response_to_errors_within_a_transaction): SQLITE_FULL, SQLITE_IOERR,
38+
// SQLITE_BUSY, SQLITE_NOMEM and SQLITE_INTERRUPT.
39+
const autoRollback = new Set([5, 7, 9, 10, 13])
40+
41+
/** Whether SQLite may have already rolled back the transaction this cause failed, so a rollback has nothing to undo. */
42+
export function rolledBack(cause: Cause.Cause<unknown>) {
43+
return sqlErrors(cause).some((error) => {
44+
const code = resultCode(error.reason.cause)
45+
return code !== undefined && autoRollback.has(code & 0xff)
46+
})
47+
}
48+
49+
// A failed transaction boundary is a SqlError; a failed query is a query error holding the Cause of its SqlError.
50+
function sqlErrors(cause: Cause.Cause<unknown>): SqlError[] {
51+
return cause.reasons.flatMap((reason) => {
52+
if (Cause.isInterruptReason(reason)) return []
53+
const error = Cause.isFailReason(reason) ? reason.error : reason.defect
54+
if (isSqlError(error)) return [error]
55+
if (error instanceof EffectDrizzleQueryError && Cause.isCause(error.cause)) return sqlErrors(error.cause)
56+
return []
57+
})
58+
}

‎packages/core/test/fixture/sqlite-node-client.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SqlClient } from "effect/sql"
99
import { isSqlError } from "effect/sql/SqlError"
1010
import { Database } from "../../src/database/database"
1111
import { sqliteLayer } from "../../src/database/sqlite.node"
12+
import { StorageFailure } from "../../src/database/storage-failure"
1213

1314
const typed = sqliteTable("typed", { id: integer().primaryKey(), name: text().notNull() })
1415

@@ -63,6 +64,10 @@ const program = Effect.gen(function* () {
6364

6465
const leakedTransaction = yield* sweep((client) => client.withTransaction(client`SELECT 2`))
6566
const leakedReserve = yield* sweep((client) => Effect.scoped(Effect.andThen(client.reserve, Effect.yieldNow)))
67+
68+
// SQLite never lowers the limit below the current size, so 1 means the database may not grow.
69+
yield* db.run(sql`PRAGMA max_page_count = 1`)
70+
const full = yield* db.run(sql`INSERT INTO probe (id) VALUES (${"x".repeat(60_000)})`).pipe(Effect.flip)
6671
return {
6772
journal: journal?.journal_mode,
6873
reason: isSqlError(cause) ? cause.reason._tag : "not a SqlError",
@@ -73,6 +78,10 @@ const program = Effect.gen(function* () {
7378
raw: raw?.count,
7479
leakedTransaction,
7580
leakedReserve,
81+
storageFailure: {
82+
full: StorageFailure.message(Cause.fail(full)),
83+
constraint: StorageFailure.message(Cause.fail(error)) ?? null,
84+
},
7685
}
7786
})
7887

‎packages/core/test/sqlite-node.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const node = Bun.which("node")
1010

1111
// Skipped on Windows for the same Bun.build panic as sqlite-bundle.test.ts.
1212
describe.skipIf(process.platform === "win32" || !node)("node sqlite client", () => {
13-
test("boots migrations, commits drizzle transactions and types constraint errors", async () => {
13+
test("boots migrations, commits drizzle transactions and types constraint and storage errors", async () => {
1414
const dir = await mkdtemp(join(tmpdir(), "sqlite-node-"))
1515
try {
1616
const build = await Bun.build({
@@ -42,6 +42,7 @@ describe.skipIf(process.platform === "win32" || !node)("node sqlite client", ()
4242
raw: 1,
4343
leakedTransaction: [],
4444
leakedReserve: [],
45+
storageFailure: { full: "Storage is full", constraint: null },
4546
})
4647
} finally {
4748
await rm(dir, { recursive: true, force: true })
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import path from "node:path"
2+
import { Database } from "bun:sqlite"
3+
import { expect } from "bun:test"
4+
import { sql } from "drizzle-orm"
5+
import { Cause, Effect } from "effect"
6+
import { SqlClient } from "effect/sql"
7+
import { EffectDrizzleSqlite } from "@opencode/core/database/drizzle"
8+
import { sqliteLayer } from "@opencode/core/database/sqlite.bun"
9+
import { StorageFailure } from "@opencode/core/database/storage-failure"
10+
import { tmpdirScoped } from "./fixture/tmpdir"
11+
import { it } from "./lib/effect"
12+
13+
// The node:sqlite driver is covered by sqlite-node.test.ts, which runs under Node.
14+
const large = "x".repeat(60_000)
15+
16+
it.live("a transaction SQLite rolled back itself fails with the storage failure, not the rollback", () =>
17+
Effect.gen(function* () {
18+
const client = yield* SqlClient.SqlClient
19+
const db = yield* EffectDrizzleSqlite.makeWithDefaults()
20+
yield* client`CREATE TABLE probe (value TEXT)`
21+
yield* client`PRAGMA max_page_count = 1`
22+
23+
const cause = yield* db
24+
.transaction(() => db.run(sql`INSERT INTO probe VALUES (${large})`), { behavior: "immediate" })
25+
.pipe(Effect.sandbox, Effect.flip)
26+
expect(StorageFailure.message(cause)).toBe("Storage is full")
27+
// Nothing was left open: the connection takes the next transaction.
28+
yield* client`PRAGMA max_page_count = 1073741823`
29+
yield* db.transaction(() => db.run(sql`INSERT INTO probe VALUES ('ok')`))
30+
}).pipe(Effect.provide(sqliteLayer({ filename: ":memory:" }))),
31+
)
32+
33+
it.live("a full database is a storage failure for a statement and for a query", () =>
34+
Effect.gen(function* () {
35+
const client = yield* SqlClient.SqlClient
36+
const db = yield* EffectDrizzleSqlite.makeWithDefaults()
37+
yield* client`CREATE TABLE probe (value TEXT)`
38+
// SQLite never lowers the limit below the current size, so 1 means the database may not grow.
39+
yield* client`PRAGMA max_page_count = 1`
40+
41+
const statement = yield* client`INSERT INTO probe VALUES (${large})`.pipe(Effect.flip)
42+
expect(StorageFailure.fromSqlError(statement)).toBe("Storage is full")
43+
expect(StorageFailure.message(Cause.fail(statement))).toBe("Storage is full")
44+
45+
const query = yield* db.run(sql`INSERT INTO probe VALUES (${large})`).pipe(Effect.flip)
46+
expect(StorageFailure.message(Cause.die(query))).toBe("Storage is full")
47+
}).pipe(Effect.provide(sqliteLayer({ filename: ":memory:" }))),
48+
)
49+
50+
it.live("an extended result code is a storage failure by its primary code", () =>
51+
Effect.gen(function* () {
52+
const dir = yield* tmpdirScoped()
53+
const filename = path.join(dir.path, "probe.db")
54+
yield* Effect.gen(function* () {
55+
const client = yield* SqlClient.SqlClient
56+
yield* client`CREATE TABLE probe (value TEXT)`
57+
// SQLITE_BUSY_SNAPSHOT (517): a WAL reader can't become the writer once another connection has committed.
58+
yield* client`BEGIN`
59+
yield* client`SELECT * FROM probe`
60+
yield* Effect.sync(() => {
61+
const other = new Database(filename)
62+
other.run("INSERT INTO probe VALUES ('other')")
63+
other.close()
64+
})
65+
const error = yield* client`INSERT INTO probe VALUES ('mine')`.pipe(Effect.flip)
66+
yield* client`ROLLBACK`
67+
expect(error.reason.cause).toMatchObject({ errno: 517 })
68+
expect(StorageFailure.fromSqlError(error)).toBe("Storage is in use by another process")
69+
}).pipe(Effect.provide(sqliteLayer({ filename })))
70+
}),
71+
)
72+
73+
it.live("a constraint, a bad statement and other defects are not storage failures", () =>
74+
Effect.gen(function* () {
75+
const client = yield* SqlClient.SqlClient
76+
const db = yield* EffectDrizzleSqlite.makeWithDefaults()
77+
yield* client`CREATE TABLE probe (id TEXT PRIMARY KEY)`
78+
yield* client`INSERT INTO probe VALUES ('a')`
79+
80+
const constraint = yield* client`INSERT INTO probe VALUES ('a')`.pipe(Effect.flip)
81+
expect(StorageFailure.fromSqlError(constraint)).toBeUndefined()
82+
const missing = yield* db.run(sql`SELECT * FROM missing`).pipe(Effect.flip)
83+
expect(StorageFailure.message(Cause.die(missing))).toBeUndefined()
84+
85+
expect(StorageFailure.message(Cause.die(new Error("database or disk is full")))).toBeUndefined()
86+
expect(StorageFailure.message(Cause.interrupt())).toBeUndefined()
87+
}).pipe(Effect.provide(sqliteLayer({ filename: ":memory:" }))),
88+
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { StorageFailure } from "@opencode/core/database/storage-failure"
2+
import { UnknownError } from "@opencode/protocol/errors"
3+
import { Cause, Effect } from "effect"
4+
import { HttpRouter, HttpServerError, HttpServerRequest, HttpServerResponse } from "effect/http"
5+
6+
const respond = HttpServerResponse.schemaJson(UnknownError)
7+
8+
// Effect answers a failure it has no response for with an empty 500. Replace exactly that response's body, like the
9+
// encoding failure in schema-error.ts: the reason when it's one a person can act on (a storage failure), otherwise a
10+
// ref to find it in the server log. Every cause Effect already answers (declared errors, route-not-found, defects that
11+
// carry their own response, aborts) keeps its response.
12+
export const defectResponseLayer = HttpRouter.middleware(
13+
(effect) =>
14+
effect.pipe(
15+
Effect.catchCause((cause) =>
16+
Effect.gen(function* () {
17+
const [response] = yield* HttpServerError.causeResponse(cause)
18+
if (response.status !== 500 || response.body._tag !== "Empty") return yield* Effect.failCause(cause)
19+
const request = yield* HttpServerRequest.HttpServerRequest
20+
const ref = `err_${crypto.randomUUID().slice(0, 8)}`
21+
yield* Effect.logError("request failed", cause).pipe(
22+
Effect.annotateLogs({ ref, route: `${request.method} ${request.url}` }),
23+
)
24+
const body = yield* respond(
25+
new UnknownError({
26+
message:
27+
StorageFailure.message(cause) ?? "Server failed to handle the request. Check server logs for details.",
28+
ref,
29+
}),
30+
{ status: 500 },
31+
).pipe(Effect.orDie)
32+
// Fail with the response beside the original cause rather than succeed with it: Effect sends a response
33+
// found in a cause, and the request's span and response log still record the failure.
34+
return yield* Effect.failCause(Cause.combine(cause, Cause.die(body)))
35+
}),
36+
),
37+
),
38+
{ global: true },
39+
)

‎packages/server/src/routes.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { CorsConfig } from "./cors"
4747
import { handlers } from "./handlers"
4848
import { authorizationLayer } from "./middleware/authorization"
4949
import { schemaErrorLayer } from "./middleware/schema-error"
50+
import { defectResponseLayer } from "./middleware/defect-response"
5051
import { ServerPairing } from "./pairing"
5152
import { layer } from "./location"
5253
import { formLocationLayer } from "./middleware/form-location"
@@ -200,6 +201,7 @@ function makeRoutes<AuthError, AuthServices>(
200201
Layer.provide(schemaErrorLayer),
201202
Layer.provide(auth),
202203
HttpRouter.provideRequest(requestServices),
204+
Layer.merge(defectResponseLayer),
203205
Layer.provideMerge(services),
204206
Layer.provideMerge(HttpRouter.layer),
205207
)

0 commit comments

Comments
 (0)