ai/skills/aidd-error-causes/SKILL.md
Use the error-causes library for structured error handling in JavaScript/TypeScript. Use when throwing errors, catching errors, defining error types, or implementing error routing.
npx skillsauth add paralleldrive/aidd aidd-error-causesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Use the error-causes library for all error handling in JavaScript/TypeScript code to enable structured error handling with named causes.
instanceof checksinstanceofimport { createError } from "error-causes";
Instead of throwing plain errors:
// ❌ DON'T
throw new Error('Config key "API_KEY" is required');
Use createError with structured metadata:
// ✅ DO
throw createError({
name: 'ConfigurationError',
message: 'Required configuration key "API_KEY" is not defined',
code: 'MISSING_CONFIG_KEY',
requestedKey: 'API_KEY'
});
Always include these properties in createError:
name - Error name for matching (e.g., 'ValidationError', 'AuthenticationError')message - Human-readable error messagecode (optional) - Error code for programmatic handlingWhen catching and re-throwing errors, preserve the original error as cause:
try {
await someOperation();
} catch (originalError) {
throw createError({
name: 'OperationError',
message: 'Failed to perform operation',
code: 'OPERATION_FAILED',
cause: originalError // Preserve original error
});
}
For factory functions that validate parameters at creation time:
const createMiddleware = ({ requiredParam } = {}) => {
if (!requiredParam) {
throw createError({
name: 'ValidationError',
message: 'requiredParam is required',
code: 'MISSING_REQUIRED_PARAM'
});
}
return async ({ request, response }) => {
// middleware implementation
};
};
In tests, verify the error's cause property:
let error;
try {
functionThatThrows();
} catch (e) {
error = e;
}
assert({
given: 'invalid input',
should: 'throw Error with cause',
actual: error instanceof Error && error.cause !== undefined,
expected: true
});
assert({
given: 'invalid input',
should: 'have correct error name',
actual: error.cause.name,
expected: 'ValidationError'
});
assert({
given: 'invalid input',
should: 'have correct error code',
actual: error.cause.code,
expected: 'MISSING_REQUIRED_PARAM'
});
For APIs that define multiple error types, use the errorCauses pattern:
import { errorCauses, createError } from "error-causes";
// Define all possible errors for your API
const [apiErrors, handleApiErrors] = errorCauses({
NotFound: {
code: 404,
message: 'Resource not found'
},
ValidationError: {
code: 400,
message: 'Invalid input'
},
Unauthorized: {
code: 401,
message: 'Authentication required'
}
});
const { NotFound, ValidationError, Unauthorized } = apiErrors;
// Throw errors
if (!resource) throw createError(NotFound);
// Handle errors with automatic routing
someAsyncCall()
.catch(handleApiErrors({
NotFound: ({ message }) => console.log(message),
ValidationError: ({ message }) => console.log(message),
Unauthorized: ({ message }) => redirect('/login')
}));
createError instead of new Error() for thrown errorsname and message in error metadatacode when the error needs programmatic handlingcause property when re-throwingcause property in error tests, not just the error messageerrorCauses() for APIs with multiple error typesdocumentation
Top tier author skill for delivering essential truths with the persuasive power to inspire positive change. Use when writing, reviewing, editing, or scoring any content.
development
Guide for crafting high-quality AIDD skills. Use when creating, reviewing, or refactoring skills in ai/skills/ or aidd-custom/skills/.
testing
Reflective Thought Composition. Structured thinking pipeline for complex decisions, design evaluation, and deep analysis. Use when quality of reasoning matters more than speed of response.
tools
Teaches agents how to write correct riteway ai prompt evals (.sudo files) for multi-step flows that involve tool calls. Use when writing prompt evals, creating .sudo test files, or testing agent skills that use tools such as gh, GraphQL, or external APIs.