mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(server): report response encoding failures as server errors (#59459)
This commit is contained in:
committed by
GitHub
Unverified
parent
54bb0a9f6f
commit
c11a32ff1e
@@ -1,6 +1,7 @@
|
||||
import { Effect } from "effect"
|
||||
import { HttpServerResponse } from "effect/http"
|
||||
import { HttpApiMiddleware } from "effect/http-api"
|
||||
import { InvalidRequestError } from "@opencode/protocol/errors"
|
||||
import { InvalidRequestError, UnknownError } from "@opencode/protocol/errors"
|
||||
import { SchemaErrorMiddleware } from "@opencode/protocol/middleware/schema-error"
|
||||
export { SchemaErrorMiddleware } from "@opencode/protocol/middleware/schema-error"
|
||||
|
||||
@@ -11,10 +12,38 @@ function truncateReason(reason: string) {
|
||||
return reason.slice(0, REASON_LIMIT) + `... (${reason.length - REASON_LIMIT} more chars)`
|
||||
}
|
||||
|
||||
export const schemaErrorLayer = HttpApiMiddleware.layerSchemaErrorTransform(SchemaErrorMiddleware, (error) => {
|
||||
const reason = truncateReason(error.cause.message)
|
||||
return Effect.logWarning("schema rejection").pipe(
|
||||
Effect.annotateLogs({ kind: error.kind, reason }),
|
||||
Effect.andThen(Effect.fail(new InvalidRequestError({ message: reason, kind: error.kind }))),
|
||||
)
|
||||
})
|
||||
export const schemaErrorLayer = HttpApiMiddleware.layerSchemaErrorTransform(
|
||||
SchemaErrorMiddleware,
|
||||
(error, context) => {
|
||||
const reason = truncateReason(error.cause.message)
|
||||
// Effect tags response encoding as Body or ResponseHeaders; request decoding uses Params, Headers, Query or Payload.
|
||||
if (error.kind === "Body" || error.kind === "ResponseHeaders") {
|
||||
const ref = `err_${crypto.randomUUID().slice(0, 8)}`
|
||||
return Effect.logError("failed to encode response").pipe(
|
||||
Effect.annotateLogs({
|
||||
ref,
|
||||
kind: error.kind,
|
||||
endpoint: `${context.group.identifier}.${context.endpoint.name}`,
|
||||
route: `${context.endpoint.method} ${context.endpoint.path}`,
|
||||
reason,
|
||||
}),
|
||||
// Respond directly rather than failing with a declared error, so every endpoint's contract stays unchanged
|
||||
// and this 500 reads like any other unexpected server failure.
|
||||
Effect.as(
|
||||
HttpServerResponse.jsonUnsafe(
|
||||
{
|
||||
_tag: "UnknownError",
|
||||
message: "Server failed to encode its response. Check server logs for details.",
|
||||
ref,
|
||||
} satisfies typeof UnknownError.Encoded,
|
||||
{ status: 500 },
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
return Effect.logWarning("schema rejection").pipe(
|
||||
Effect.annotateLogs({ kind: error.kind, reason }),
|
||||
Effect.andThen(Effect.fail(new InvalidRequestError({ message: reason, kind: error.kind }))),
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { expect } from "bun:test"
|
||||
import { Generate } from "@opencode/core/generate"
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { it } from "../../core/test/lib/effect"
|
||||
import { ServerFetch } from "../src/fetch"
|
||||
|
||||
// The success schema declares `text` as a string, so a number fails response encoding.
|
||||
const generate = makeLocationNode({
|
||||
service: Generate.Service,
|
||||
layer: Layer.succeed(Generate.Service, Generate.Service.of({ text: () => Effect.succeed(42 as unknown as string) })),
|
||||
deps: [],
|
||||
})
|
||||
|
||||
it.live("reports response encoding failures as server errors and request decoding failures as bad requests", () =>
|
||||
Effect.gen(function* () {
|
||||
const handler = yield* ServerFetch.make(
|
||||
{
|
||||
database: { path: ":memory:" },
|
||||
fs: { filewatcher: false },
|
||||
models: { fetch: false },
|
||||
},
|
||||
{ overrides: [Generate.node.replace(generate)] },
|
||||
)
|
||||
|
||||
const encoding = yield* request(handler, { prompt: "hello" })
|
||||
expect(encoding.status).toBe(500)
|
||||
expect(encoding.body).toMatchObject({
|
||||
_tag: "UnknownError",
|
||||
message: "Server failed to encode its response. Check server logs for details.",
|
||||
ref: expect.stringMatching(/^err_/),
|
||||
})
|
||||
|
||||
const decoding = yield* request(handler, { prompt: 1 })
|
||||
expect(decoding.status).toBe(400)
|
||||
expect(decoding.body).toMatchObject({ _tag: "InvalidRequestError", kind: "Payload" })
|
||||
}),
|
||||
)
|
||||
|
||||
function request(handler: (request: Request) => Promise<Response>, body: unknown) {
|
||||
return Effect.promise(async () => {
|
||||
const response = await handler(
|
||||
new Request("http://opencode.local/api/experimental/generate", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
)
|
||||
const json: unknown = await response.json()
|
||||
return { status: response.status, body: json }
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user