ZerospinError
Create structured errors with stable codes, human messages, and optional metadata.
Quick patterns
- •Prefer full form with
code,message, andcausewhen wrapping. - •Use the short form only for trivial cases:
new ZerospinError('some-code'). - •Return errors from
Effect.genwithreturn yield* new ZerospinError(...).
typescript
const error = new ZerospinError({
code: 'failed-to-fetch',
message: 'Failed to fetch data',
cause: originalError,
extra: { url },
status: 502,
});
Rules
- •Use a stable, kebab-case
codestring; never include dynamic data in the code. - •Keep
messagehuman-readable and specific to the failure. - •Always attach the original
causewhen wrapping another error. - •Use
extrafor structured metadata (ids, inputs, context), not for free-form text. - •Use
statusonly for HTTP-facing errors.
Effect usage
Return it don't throw it, duh
typescript
const res = yield* Effect.promise(() => fetch('/api/data'));
if (res.status === 404) {
return yield* new ZerospinError({
code: 'resource-not-found',
message: 'Resource not found',
});
}
Working with promises - mapError!
typescript
const parsed = Effect.promise(() => res.json()).pipe(
Effect.mapError((error) => {
return new ZerospinError({
code: 'failed-to-parse-json',
message: 'Failed to parse JSON',
cause: error,
});
})
);
Effect.fn
Catch all
typescript
const safe = effect.pipe(
Effect.catchAll((error) => {
return new ZerospinError({
code: 'operation-failed',
message: error.message,
cause: error,
});
})
);