mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
refactor: use typed error mapping operators (#45727)
This commit is contained in:
committed by
GitHub
Unverified
parent
b0c8a8c827
commit
5990679ebd
@@ -277,11 +277,7 @@ export class SQLiteEffectPreparedQuery<
|
||||
}
|
||||
|
||||
assertUnreachable(cacheStrat)
|
||||
}).pipe(
|
||||
Effect.catch((e) => {
|
||||
return Effect.fail(new EffectDrizzleQueryError({ query: queryString, params, cause: Cause.fail(e) }))
|
||||
}),
|
||||
)
|
||||
}).pipe(Effect.mapError((e) => new EffectDrizzleQueryError({ query: queryString, params, cause: Cause.fail(e) })))
|
||||
}
|
||||
|
||||
getQuery(): Query {
|
||||
|
||||
@@ -6,10 +6,11 @@ import { expect, test } from "bun:test"
|
||||
import { SqliteClient } from "@effect/sql-sqlite-bun"
|
||||
import { eq, sql } from "drizzle-orm"
|
||||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
|
||||
import { Effect, Tracer } from "effect"
|
||||
import { Cause, Effect, Tracer } from "effect"
|
||||
import type { SqlClient } from "effect/unstable/sql/SqlClient"
|
||||
import { isSqlError } from "effect/unstable/sql/SqlError"
|
||||
import { EffectDrizzleSqlite } from "@opencode-ai/core/database/drizzle"
|
||||
import { EffectDrizzleQueryError } from "drizzle-orm/effect-core/errors"
|
||||
|
||||
const users = sqliteTable("users", {
|
||||
id: integer().primaryKey({ autoIncrement: true }),
|
||||
@@ -47,6 +48,22 @@ test("selects rows through Effect-yieldable query builders", async () => {
|
||||
)
|
||||
})
|
||||
|
||||
test("maps query failures with query, params, and cause", async () => {
|
||||
await run(
|
||||
Effect.gen(function* () {
|
||||
const db = yield* EffectDrizzleSqlite.makeWithDefaults()
|
||||
const error = yield* db.run(sql`select * from missing_table where id = ${42}`).pipe(Effect.flip)
|
||||
|
||||
expect(error).toBeInstanceOf(EffectDrizzleQueryError)
|
||||
expect(error.query).toBe("select * from missing_table where id = ?")
|
||||
expect(error.params).toEqual([42])
|
||||
expect(Cause.isCause(error.cause)).toBe(true)
|
||||
if (!Cause.isCause(error.cause)) return
|
||||
expect(error.cause.reasons[0]?._tag).toBe("Fail")
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
test("suppresses statement spans", async () => {
|
||||
const spans: Tracer.NativeSpan[] = []
|
||||
const tracer = Tracer.make({
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import fs from "fs/promises"
|
||||
import { randomUUID } from "node:crypto"
|
||||
import { realpathSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import path from "node:path"
|
||||
@@ -12,6 +13,7 @@ import { testEffect } from "../lib/effect"
|
||||
const it = testEffect(LayerNode.compile(AppProcess.node))
|
||||
|
||||
const NODE = process.execPath
|
||||
const MISSING_CWD = path.join(tmpdir(), `opencode-missing-cwd-${randomUUID()}`)
|
||||
const cmd = (...args: string[]) => ChildProcess.make(NODE, args)
|
||||
|
||||
const waitForFile = (file: string) =>
|
||||
@@ -41,6 +43,17 @@ describe("AppProcess", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.live(
|
||||
"maps command setup failures to AppProcessError",
|
||||
Effect.gen(function* () {
|
||||
const svc = yield* AppProcess.Service
|
||||
const error = yield* svc.run(ChildProcess.make(NODE, [], { cwd: MISSING_CWD })).pipe(Effect.flip)
|
||||
|
||||
expect(error).toBeInstanceOf(AppProcess.AppProcessError)
|
||||
expect(error.command).toBe(NODE)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect(
|
||||
"captures stdout and stderr in emission order",
|
||||
Effect.gen(function* () {
|
||||
@@ -280,6 +293,19 @@ describe("AppProcess", () => {
|
||||
})
|
||||
|
||||
describe("runStream", () => {
|
||||
it.live(
|
||||
"maps streaming command setup failures to AppProcessError",
|
||||
Effect.gen(function* () {
|
||||
const svc = yield* AppProcess.Service
|
||||
const error = yield* svc
|
||||
.runStream(ChildProcess.make(NODE, [], { cwd: MISSING_CWD }))
|
||||
.pipe(Stream.runCollect, Effect.flip)
|
||||
|
||||
expect(error).toBeInstanceOf(AppProcess.AppProcessError)
|
||||
expect(error.command).toBe(NODE)
|
||||
}),
|
||||
)
|
||||
|
||||
it.live(
|
||||
"emits lines incrementally and ends cleanly on exit 0",
|
||||
Effect.gen(function* () {
|
||||
|
||||
@@ -193,7 +193,7 @@ const layer = Layer.effect(
|
||||
),
|
||||
)
|
||||
: timed
|
||||
return aborted.pipe(Effect.catch((cause) => Effect.fail(wrapError(description, cause))))
|
||||
return aborted.pipe(Effect.mapError((cause) => wrapError(description, cause)))
|
||||
}
|
||||
|
||||
const run = Effect.fn("AppProcess.run")(function* (command: ChildProcess.Command, options?: RunOptions) {
|
||||
@@ -253,9 +253,7 @@ const layer = Layer.effect(
|
||||
return Stream.concat(lines, tail) as Stream.Stream<string, AppProcessError | PlatformError>
|
||||
}),
|
||||
)
|
||||
const mapped = built.pipe(
|
||||
Stream.catch((cause): Stream.Stream<string, AppProcessError> => Stream.fail(wrapError(description, cause))),
|
||||
)
|
||||
const mapped = built.pipe(Stream.mapError((cause) => wrapError(description, cause)))
|
||||
if (!options?.signal) return mapped
|
||||
const signal = options.signal
|
||||
return mapped.pipe(
|
||||
|
||||
Reference in New Issue
Block a user