refactor(core): replace legacy logger with Effect logging (#31310)
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
import { NamedError } from "@opencode-ai/core/util/error"
|
||||
import * as Log from "@opencode-ai/core/util/log"
|
||||
import { Cause, Effect } from "effect"
|
||||
import { HttpRouter, HttpServerError, HttpServerRespondable, HttpServerResponse } from "effect/unstable/http"
|
||||
|
||||
const log = Log.create({ service: "server" })
|
||||
|
||||
// Keep typed HttpApi failures on their declared error path; this boundary only replaces defect-only empty 500s.
|
||||
export const errorLayer = HttpRouter.middleware<{ handles: unknown }>()((effect) =>
|
||||
effect.pipe(
|
||||
@@ -20,15 +17,15 @@ export const errorLayer = HttpRouter.middleware<{ handles: unknown }>()((effect)
|
||||
const error = defect.defect
|
||||
const ref = `err_${crypto.randomUUID().slice(0, 8)}`
|
||||
|
||||
log.error("failed", { ref, error, cause: Cause.pretty(cause) })
|
||||
|
||||
return Effect.succeed(
|
||||
HttpServerResponse.jsonUnsafe(
|
||||
new NamedError.Unknown({
|
||||
message: "Unexpected server error. Check server logs for details.",
|
||||
ref,
|
||||
}).toObject(),
|
||||
{ status: 500 },
|
||||
return Effect.logError("failed", { ref, error, cause: Cause.pretty(cause) }).pipe(
|
||||
Effect.as(
|
||||
HttpServerResponse.jsonUnsafe(
|
||||
new NamedError.Unknown({
|
||||
message: "Unexpected server error. Check server logs for details.",
|
||||
ref,
|
||||
}).toObject(),
|
||||
{ status: 500 },
|
||||
),
|
||||
),
|
||||
)
|
||||
}),
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { Effect } from "effect"
|
||||
import { HttpServerResponse } from "effect/unstable/http"
|
||||
import { HttpApiMiddleware } from "effect/unstable/httpapi"
|
||||
import * as Log from "@opencode-ai/core/util/log"
|
||||
import { InvalidRequestError } from "../errors"
|
||||
|
||||
const log = Log.create({ service: "server" })
|
||||
|
||||
// Effect's Issue formatter recursively dumps the rejected `actual` value with
|
||||
// no truncation, so a 5KB invalid array produces a ~360KB string. Cap to keep
|
||||
// 4xx responses small and avoid mirroring entire request payloads (which may
|
||||
@@ -27,16 +24,18 @@ export class SchemaErrorMiddleware extends HttpApiMiddleware.Service<SchemaError
|
||||
|
||||
export const schemaErrorLayer = HttpApiMiddleware.layerSchemaErrorTransform(SchemaErrorMiddleware, (error, context) => {
|
||||
const reason = truncateReason(error.cause.message)
|
||||
log.warn("schema rejection", { kind: error.kind, reason })
|
||||
if (context.endpoint.path.startsWith("/api/")) {
|
||||
return Effect.fail(
|
||||
new InvalidRequestError({
|
||||
message: reason,
|
||||
kind: error.kind,
|
||||
}),
|
||||
)
|
||||
}
|
||||
return Effect.succeed(
|
||||
HttpServerResponse.jsonUnsafe({ name: "BadRequest", data: { message: reason, kind: error.kind } }, { status: 400 }),
|
||||
)
|
||||
const response = context.endpoint.path.startsWith("/api/")
|
||||
? Effect.fail(
|
||||
new InvalidRequestError({
|
||||
message: reason,
|
||||
kind: error.kind,
|
||||
}),
|
||||
)
|
||||
: Effect.succeed(
|
||||
HttpServerResponse.jsonUnsafe(
|
||||
{ name: "BadRequest", data: { message: reason, kind: error.kind } },
|
||||
{ status: 400 },
|
||||
),
|
||||
)
|
||||
return Effect.logWarning("schema rejection", { kind: error.kind, reason }).pipe(Effect.andThen(response))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user