define-errors

Define typed, serializable error variants in TypeScript with defineErrors.

35|4|Updated Dec 18, 2024
One-click install
npx skills add https://github.com/wellcrafted-dev/wellcrafted --skill define-errors
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: define-errors
Source: https://github.com/wellcrafted-dev/wellcrafted/tree/main/.claude/skills/define-errors
Command: npx skills add https://github.com/wellcrafted-dev/wellcrafted --skill define-errors

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill simplifies error handling in TypeScript by providing a structured and type-safe way to define, manage, and use custom error variants, preventing common pitfalls and improving code clarity.

Core Features & Use Cases

  • Type-Safe Error Definitions: Define domain-specific errors with typed fields using defineErrors.
  • Automatic Discriminated Unions: Generates a union type for all error variants, enabling precise error handling.
  • Simplified Error Construction: Variant factories directly return Err(...) results.
  • Use Case: When building a user authentication service, you can define specific errors like UserNotFoundError, InvalidPasswordError, and EmailAlreadyExistsError with relevant context, making your error handling robust and readable.

Quick Start

Use the define-errors skill to create a new error type named 'ApiError' with variants 'BadRequest' and 'Unauthorized'.

Frequently Asked Questions about define-errors

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define custom typed errors in TypeScript?

Type-safe error definitions in TypeScript are created by using a utility to generate error variants with specific typed fields, forming a discriminated union that ensures precise error handling and code clarity.

What is the best way to handle API errors with discriminated unions in TypeScript?

Handling API errors with discriminated unions is best achieved by defining serializable error variants. This approach generates a union type, allowing precise matching of API error responses like BadRequest or Unauthorized.

How do I extract error messages from caught errors in TypeScript asynchronous operations?

To extract error messages from caught errors in asynchronous operations, use a dedicated helper function to unwrap the error cause. This simplifies extracting the underlying message from complex nested error objects.

Can I create serializable and composable error variants for domain-specific logic?

Yes, you can create serializable and composable error variants for domain-specific logic. By defining structured error types, you ensure that custom errors carry relevant context and can be safely serialized across boundaries.

Does this error handling approach work without external dependencies?

Yes, this structured error handling approach works without external dependencies. It relies entirely on TypeScript's native type system to generate discriminated unions and manage typed error variants safely.