ErrorSchema for JavaScript Error objects.
Details
Default JSON serializer:
Encodes an Error as an object with message, optional name, and optional
cause properties, and decodes that object back into an Error. Stack
traces are omitted by default for security. Pass { includeStack: true } to
include stack traces, or { excludeCause: true } to omit causes.
export interface Error extends interface instanceOf<T, Iso = T>Creates a schema that validates values using instanceof.
Decoding and encoding pass the value through unchanged.
Example (Defining a schema for a built-in class)
import { Schema } from "effect"
const DateSchema = Schema.instanceOf(Date)
const decoded = Schema.decodeUnknownSync(DateSchema)(new Date("2024-01-01"))
// decoded: Date
Type-level representation returned by
instanceOf
.
instanceOf<module globalThisglobalThis.Error> {
readonly "Rebuild": Error
}
/**
* Options for {@link Error} and {@link Defect}.
*
* @category options
* @since 4.0.0
*/
export interface ErrorOptions {
/**
* Includes string stack traces in encoded `Error` values when set to `true`.
*
* @default false
*/
readonly ErrorOptions.includeStack?: boolean | undefinedIncludes string stack traces in encoded Error values when set to true.
includeStack?: boolean | undefined
/**
* Excludes `Error.cause` values from encoded `Error` values when set to
* `true`.
*
* @default false
*/
readonly ErrorOptions.excludeCause?: boolean | undefinedExcludes Error.cause values from encoded Error values when set to
true.
excludeCause?: boolean | undefined
}
type type ErrorOptionsKey = 0 | 2 | 1 | 3ErrorOptionsKey = 0 | 1 | 2 | 3
const const getErrorOptionsKey: (
options?: ErrorOptions
) => ErrorOptionsKey
getErrorOptionsKey = (options: ErrorOptionsoptions?: ErrorOptions): type ErrorOptionsKey = 0 | 2 | 1 | 3ErrorOptionsKey =>
((options: ErrorOptionsoptions?.ErrorOptions.includeStack?: boolean | undefinedIncludes string stack traces in encoded Error values when set to true.
includeStack === true ? 1 : 0) |
(options: ErrorOptionsoptions?.ErrorOptions.excludeCause?: boolean | undefinedExcludes Error.cause values from encoded Error values when set to
true.
excludeCause === true ? 2 : 0)) as type ErrorOptionsKey = 0 | 2 | 1 | 3ErrorOptionsKey
const const getErrorOptions: (
key: ErrorOptionsKey
) => ErrorOptions | undefined
getErrorOptions = (key: ErrorOptionsKeykey: type ErrorOptionsKey = 0 | 2 | 1 | 3ErrorOptionsKey): ErrorOptions | undefined => {
switch (key: ErrorOptionsKeykey) {
case 0:
return var undefinedundefined
case 1:
return { ErrorOptions.includeStack?: boolean | undefinedIncludes string stack traces in encoded Error values when set to true.
includeStack: true }
case 2:
return { ErrorOptions.excludeCause?: boolean | undefinedExcludes Error.cause values from encoded Error values when set to
true.
excludeCause: true }
case 3:
return { ErrorOptions.includeStack?: boolean | undefinedIncludes string stack traces in encoded Error values when set to true.
includeStack: true, ErrorOptions.excludeCause?: boolean | undefinedExcludes Error.cause values from encoded Error values when set to
true.
excludeCause: true }
}
}
const const errorSchemaCache: Array<
Error | undefined
>
errorSchemaCache: interface Array<T>Array<Error | undefined> = []
/**
* Schema for JavaScript `Error` objects.
*
* **Details**
*
* Default JSON serializer:
*
* Encodes an `Error` as an object with `message`, optional `name`, and optional
* `cause` properties, and decodes that object back into an `Error`. Stack
* traces are omitted by default for security. Pass `{ includeStack: true }` to
* include stack traces, or `{ excludeCause: true }` to omit causes.
*
* @category constructors
* @since 4.0.0
*/
export function function Error(
options?: ErrorOptions
): Error
Schema for JavaScript Error objects.
Details
Default JSON serializer:
Encodes an Error as an object with message, optional name, and optional
cause properties, and decodes that object back into an Error. Stack
traces are omitted by default for security. Pass { includeStack: true } to
include stack traces, or { excludeCause: true } to omit causes.
Error(options: ErrorOptionsoptions?: ErrorOptions): Error {
const const key: ErrorOptionsKeykey = const getErrorOptionsKey: (
options?: ErrorOptions
) => ErrorOptionsKey
getErrorOptionsKey(options: ErrorOptionsoptions)
const const cached: Error | undefinedcached = const errorSchemaCache: Array<
Error | undefined
>
errorSchemaCache[const key: ErrorOptionsKeykey]
if (const cached: Error | undefinedcached !== var undefinedundefined) {
return const cached: Errorconst cached: {
Rebuild: Error;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<globalThis.Error, readonly []>) => Error;
annotateKey: (annotations: Annotations.Key<globalThis.Error>) => Error;
check: (checks_0: SchemaAST.Check<globalThis.Error>, ...checks: Array<SchemaAST.Check<globalThis.Error>>) => Error;
rebuild: (ast: SchemaAST.Declaration) => Error;
make: (input: globalThis.Error, options?: MakeOptions) => globalThis.Error;
makeOption: (input: globalThis.Error, options?: MakeOptions) => Option_.Option<globalThis.Error>;
makeEffect: (input: globalThis.Error, options?: MakeOptions) => Effect.Effect<globalThis.Error, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
cached
}
const const normalizedOptions:
| ErrorOptions
| undefined
normalizedOptions = const getErrorOptions: (
key: ErrorOptionsKey
) => ErrorOptions | undefined
getErrorOptions(const key: ErrorOptionsKeykey)
const const schema: instanceOf<
globalThis.Error,
globalThis.Error
>
const schema: {
Rebuild: instanceOf<T, Iso>;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<globalThis.Error, readonly []>) => instanceOf<globalThis.Error, globalThis.Error>;
annotateKey: (annotations: Annotations.Key<globalThis.Error>) => instanceOf<globalThis.Error, globalThis.Error>;
check: (checks_0: SchemaAST.Check<globalThis.Error>, ...checks: Array<SchemaAST.Check<globalThis.Error>>) => instanceOf<globalThis.Error, globalThis.Error>;
rebuild: (ast: SchemaAST.Declaration) => instanceOf<globalThis.Error, globalThis.Error>;
make: (input: globalThis.Error, options?: MakeOptions) => globalThis.Error;
makeOption: (input: globalThis.Error, options?: MakeOptions) => Option_.Option<globalThis.Error>;
makeEffect: (input: globalThis.Error, options?: MakeOptions) => Effect.Effect<globalThis.Error, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schema = function instanceOf<
C extends abstract new (...args: any) => any,
Iso = InstanceType<C>
>(
constructor: C,
annotations?:
| Annotations.Declaration<InstanceType<C>>
| undefined
): instanceOf<InstanceType<C>, Iso>
Creates a schema that validates values using instanceof.
Decoding and encoding pass the value through unchanged.
Example (Defining a schema for a built-in class)
import { Schema } from "effect"
const DateSchema = Schema.instanceOf(Date)
const decoded = Schema.decodeUnknownSync(DateSchema)(new Date("2024-01-01"))
// decoded: Date
instanceOf(module globalThisglobalThis.var Error: ErrorConstructorError, {
Annotations.Declaration<globalThis.Error, readonly []>.typeConstructor?: {
readonly [key: string]: unknown;
readonly _tag: string;
} | undefined
typeConstructor: {
_tag: string_tag: "Error",
...(const normalizedOptions:
| ErrorOptions
| undefined
normalizedOptions === var undefinedundefined ? {} : { options: ErrorOptions(property) options: {
includeStack: boolean | undefined;
excludeCause: boolean | undefined;
}
options: const normalizedOptions: ErrorOptionsconst normalizedOptions: {
includeStack: boolean | undefined;
excludeCause: boolean | undefined;
}
normalizedOptions })
},
Annotations.Declaration<globalThis.Error, readonly []>.generation?: {
readonly runtime: string;
readonly Type: string;
readonly Encoded?: string | undefined;
readonly importDeclaration?: string | undefined;
} | undefined
generation: {
runtime: stringruntime: const normalizedOptions:
| ErrorOptions
| undefined
normalizedOptions !== var undefinedundefined ? `Schema.Error(${import formatformat(const normalizedOptions: ErrorOptionsconst normalizedOptions: {
includeStack: boolean | undefined;
excludeCause: boolean | undefined;
}
normalizedOptions)})` : `Schema.Error()`,
type Type: stringType: `globalThis.Error`
},
Annotations.Augment.expected?: string | undefinedHuman-readable description of what a value is expected to satisfy.
Details
For filter and refinement failures, the default formatter uses
message first, then expected, and finally falls back to <filter>.
Use this to name a failed filter in the default message:
Expected <expected>, got <actual>.
expected: "Error",
Annotations.Declaration<globalThis.Error, readonly []>.toCodecJson?: ((typeParameters: readonly []) => SchemaAST.Link) | undefinedtoCodecJson: () => function link<T>(): <
To extends Constraint
>(
encodeTo: To,
transformation: {
readonly decode: SchemaGetter.Getter<
T,
NoInfer<To["Type"]>
>
readonly encode: SchemaGetter.Getter<
NoInfer<To["Type"]>,
T
>
}
) => SchemaAST.Link
Constructs an SchemaAST.Link that describes how a value of type T encodes to and decodes from a To schema.
Used when building low-level AST transformations that bridge two schema types.
link<module globalThisglobalThis.Error>()(const JsonError: Struct<{
readonly message: String
readonly name: optionalKey<String>
readonly stack: optionalKey<String>
readonly cause: optionalKey<
Codec<Json, Json, never, never>
>
}>
const JsonError: {
Type: Struct.Type<Fields>;
Encoded: Struct.Encoded<Fields>;
DecodingServices: Struct.DecodingServices<Fields>;
EncodingServices: Struct.EncodingServices<Fields>;
Iso: Struct.Iso<Fields>;
fields: Fields;
mapFields: (f: (fields: { readonly message: String; readonly name: optionalKey<String>; readonly stack: optionalKey<String>; readonly cause: optionalKey<Codec<Json, Json, never, never>> }) => To, options?: { readonly unsafePreserveChecks?: boolean | …;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<{ readonly message: string; readonly name?: string | undefined; readonly cause?: Json | undefined; readonly stack?: string | undefined; }, readonly []>) => Struct<{ readonly message: String; readonly name: …;
annotateKey: (annotations: Annotations.Key<{ readonly message: string; readonly name?: string | undefined; readonly cause?: Json | undefined; readonly stack?: string | undefined }>) => Struct<{ readonly message: String; readonly name: optionalKey<Strin…;
check: (checks_0: SchemaAST.Check<{ readonly message: string; readonly name?: string | undefined; readonly cause?: Json | undefined; readonly stack?: string | undefined }>, ...checks: Array<SchemaAST.Check<{ readonly message: string; readonly nam…;
rebuild: (ast: SchemaAST.Objects) => Struct<{ readonly message: String; readonly name: optionalKey<String>; readonly stack: optionalKey<String>; readonly cause: optionalKey<Codec<Json, Json, never, never>> }>;
make: (input: { readonly message: string; readonly name?: string | undefined; readonly cause?: unknown; readonly stack?: string | undefined }, options?: MakeOptions) => { readonly message: string; readonly name?: string | undefined; readonly cau…;
makeOption: (input: { readonly message: string; readonly name?: string | undefined; readonly cause?: unknown; readonly stack?: string | undefined }, options?: MakeOptions) => Option_.Option<{ readonly message: string; readonly name?: string | undefine…;
makeEffect: (input: { readonly message: string; readonly name?: string | undefined; readonly cause?: unknown; readonly stack?: string | undefined }, options?: MakeOptions) => Effect.Effect<{ readonly message: string; readonly name?: string | undefined…;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
JsonError, import SchemaTransformationSchemaTransformation.const errorFromJsonError: (
options?: ErrorOptions
) => Transformation<Error, JsonError>
errorFromJsonError(const normalizedOptions:
| ErrorOptions
| undefined
normalizedOptions)),
Annotations.Declaration<globalThis.Error, readonly []>.toArbitrary?: Annotations.ToArbitrary.Declaration<globalThis.Error, readonly []> | undefinedtoArbitrary: () => (fc: typeof FastCheckfc) => fc: typeof FastCheckfc.string().map((message: anymessage) => new module globalThisglobalThis.var Error: ErrorConstructor
new (message?: string, options?: globalThis.ErrorOptions) => globalThis.Error (+1 overload)
Error(message: anymessage))
})
const errorSchemaCache: Array<
Error | undefined
>
errorSchemaCache[const key: ErrorOptionsKeykey] = const schema: instanceOf<
globalThis.Error,
globalThis.Error
>
const schema: {
Rebuild: instanceOf<T, Iso>;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<globalThis.Error, readonly []>) => instanceOf<globalThis.Error, globalThis.Error>;
annotateKey: (annotations: Annotations.Key<globalThis.Error>) => instanceOf<globalThis.Error, globalThis.Error>;
check: (checks_0: SchemaAST.Check<globalThis.Error>, ...checks: Array<SchemaAST.Check<globalThis.Error>>) => instanceOf<globalThis.Error, globalThis.Error>;
rebuild: (ast: SchemaAST.Declaration) => instanceOf<globalThis.Error, globalThis.Error>;
make: (input: globalThis.Error, options?: MakeOptions) => globalThis.Error;
makeOption: (input: globalThis.Error, options?: MakeOptions) => Option_.Option<globalThis.Error>;
makeEffect: (input: globalThis.Error, options?: MakeOptions) => Effect.Effect<globalThis.Error, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schema
return const schema: instanceOf<
globalThis.Error,
globalThis.Error
>
const schema: {
Rebuild: instanceOf<T, Iso>;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<globalThis.Error, readonly []>) => instanceOf<globalThis.Error, globalThis.Error>;
annotateKey: (annotations: Annotations.Key<globalThis.Error>) => instanceOf<globalThis.Error, globalThis.Error>;
check: (checks_0: SchemaAST.Check<globalThis.Error>, ...checks: Array<SchemaAST.Check<globalThis.Error>>) => instanceOf<globalThis.Error, globalThis.Error>;
rebuild: (ast: SchemaAST.Declaration) => instanceOf<globalThis.Error, globalThis.Error>;
make: (input: globalThis.Error, options?: MakeOptions) => globalThis.Error;
makeOption: (input: globalThis.Error, options?: MakeOptions) => Option_.Option<globalThis.Error>;
makeEffect: (input: globalThis.Error, options?: MakeOptions) => Effect.Effect<globalThis.Error, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schema
}