Skip to content

Custom Error Classes

Custom error classes carry typed failure payloads in E. In VibeLang, declare them with error (or schema error for schema serialization) and raise them with throw inside an effect function; effect function* produces a Stream, and plain function* remains a JavaScript generator.

Form_tagSerializationWhen to use
error Name { ... }Readonly "Name"NoneStandard tagged domain errors in .vibe
schema error Name { ... }Readonly "Name"Effect SchemaErrors that cross RPC, storage, or wire boundaries
class ... extends Data.Error<...>NoneNoneUntagged yieldable errors shared with TypeScript generators

Defining an error without a tag

// expect-warning: EK4830
class EmptyDocument extends Data.Error<{
  readonly documentId: string
}> {}
 
effect function requireText(documentId: string, text: string) {
  if (text.trim() === "") throw new EmptyDocument({ documentId })
  return text
}
// string!EmptyDocument
// Run in an effect function:
//   try Console.log(try requireText("doc-1", "Hello"))
//   try Console.log((try flip(requireText("doc-1", " "))).documentId)
Hello
doc-1

Data.Error defines a typed error class that can also be yielded in TypeScript generators. Inside an effect function, throw puts EmptyDocument into E; merely constructing or returning an instance does not fail the computation.

function emptyDocumentError(documentId: string) {
  return new EmptyDocument({ documentId })
}
 
effect function rejectDocument(documentId: string) {
  const error = emptyDocumentError(documentId)
  throw error
}
// never!EmptyDocument
// Run in an effect function:
//   try Console.log(emptyDocumentError("doc-1").documentId)
//   try Console.log((try flip(rejectDocument("doc-1"))).documentId)
doc-1
doc-1

new only allocates the object, and a function that returns an error instance returns plain data. throw error inside an effect function fails with that value.

Creating tagged error classes

error EmptyTitle
error TitleTooLong {
  length: number
}
 
effect function title(text: string) {
  if (text.length === 0) throw new EmptyTitle()
  if (text.length > 80) throw new TitleTooLong({ length: text.length })
  return text
}
// string!(EmptyTitle | TitleTooLong)
// Run in an effect function:
//   const longTitle = try orDie(Effect.try(() => "x".repeat(81)))
//   try Console.log(try title("Draft"))
//   try Console.log((try flip(title("")))._tag)
//   const error = try flip(title(longTitle))
//   try Console.log(match (error) { when TitleTooLong and { let length }: length; default: 0; })
Draft
EmptyTitle
81

An error declaration compiles to a class extending Data.TaggedError with no import required. Each instance carries a readonly _tag matching the class name, and the declaration serves as both constructor and instance type; use schema error when you also need schema codecs.

Keep tags distinct across error cases. catchTag, _tag narrowing, is, and match compare tag strings rather than class identity, so duplicate tags are not distinguished and two packages declaring the same error name match each other's instances.

Recovering failures by tag

// expect-warning: EK4835
effect function displayTitle(text: string) {
  return try catchTag(title(text), "EmptyTitle", () => succeed("Untitled"))
}
// string!TitleTooLong
 
effect function editableTitle(text: string) {
  return title(text) catch effect (error) => match (error) {
    when EmptyTitle: "Untitled";
    default: throw error;
  }
}
// string!TitleTooLong
// Run in an effect function:
//   const longTitle = try orDie(Effect.try(() => "x".repeat(81)))
//   try Console.log(try displayTitle(""))
//   try Console.log(try editableTitle(""))
//   try Console.log(try displayTitle("Draft"))
//   try Console.log(try editableTitle("Draft"))
//   try Console.log((try flip(displayTitle(longTitle)))._tag)
//   try Console.log((try flip(editableTitle(longTitle)))._tag)
Untitled
Untitled
Draft
Draft
TitleTooLong
TitleTooLong

Both forms remove EmptyTitle from E while leaving TitleTooLong in the error channel. Use Effect.catchTags when handling several tags with separate callbacks, or catch with match in .vibe files.

Coming from Effect

Existing .ts generators that yield Data.Error or Data.TaggedError continue to work; call them from .vibe with try. Inside an effect function, return yield* new ErrorCase(...) becomes throw new ErrorCase(...).

In .vibe files, class NotFound extends Data.TaggedError("NotFound")<{ … }> {} triggers EK4830 (suggesting error NotFound { … }), and an eligible Schema.TaggedError class triggers EK4837 (suggesting schema error NotFound { … }, which preserves its Schema; see TypeScript-style Effect code). Native Schema.TaggedError classes with spread fields and native error classes with an explicit static _tag remain valid as written with no advisory.

Use effect function* for a Stream producer; plain function* stays a JavaScript generator.


The matching Effect page: effect.website/docs/v4/error-management/yieldable-errors