Remove Zod from named errors (#26982)
This commit is contained in:
@@ -1,51 +1,9 @@
|
||||
import { Schema } from "effect"
|
||||
import { NamedError } from "@opencode-ai/core/util/error"
|
||||
|
||||
/**
|
||||
* Create a Schema-backed NamedError-shaped class.
|
||||
*
|
||||
* Drop-in replacement for `NamedError.create(tag, zodShape)` but backed by
|
||||
* `Schema.Struct` under the hood. The wire shape emitted by the derived
|
||||
* `.Schema` is still `{ name: tag, data: {...fields} }` so the generated
|
||||
* OpenAPI/SDK output is byte-identical to the original NamedError schema.
|
||||
*
|
||||
* Preserves the existing surface:
|
||||
* - static `Schema` (Effect schema of the wire shape)
|
||||
* - static `isInstance(x)`
|
||||
* - instance `toObject()` returning `{ name, data }`
|
||||
* - `new X({ ...data }, { cause })`
|
||||
*/
|
||||
export function namedSchemaError<Tag extends string, Fields extends Schema.Struct.Fields>(tag: Tag, fields: Fields) {
|
||||
const dataSchema = Schema.Struct(fields)
|
||||
// Wire shape matches the original NamedError output so the SDK stays stable.
|
||||
const effectSchema = Schema.Struct({
|
||||
name: Schema.Literal(tag),
|
||||
data: dataSchema,
|
||||
}).annotate({ identifier: tag })
|
||||
|
||||
type Data = Schema.Schema.Type<typeof dataSchema>
|
||||
|
||||
class NamedSchemaError extends Error {
|
||||
static readonly Schema = effectSchema
|
||||
static readonly EffectSchema = effectSchema
|
||||
static readonly tag = tag
|
||||
public static isInstance(input: unknown): input is NamedSchemaError {
|
||||
return typeof input === "object" && input !== null && "name" in input && (input as { name: unknown }).name === tag
|
||||
}
|
||||
|
||||
public override readonly name: Tag = tag
|
||||
public readonly data: Data
|
||||
|
||||
constructor(data: Data, options?: ErrorOptions) {
|
||||
super(tag, options)
|
||||
this.data = data
|
||||
}
|
||||
|
||||
toObject(): { name: Tag; data: Data } {
|
||||
return { name: tag, data: this.data }
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(NamedSchemaError, "name", { value: tag })
|
||||
|
||||
return NamedSchemaError
|
||||
return NamedError.create(tag, fields)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user