@j0nathan-ll0yd/errors
Typed error classes with HTTP status code mapping for Lambda handlers.
CustomLambdaError
Base class for all Mantle errors. Extends Error with statusCode and context.
class CustomLambdaError extends Error {
readonly statusCode: number;
readonly context?: ErrorContext;
constructor(message: string, statusCode?: number, context?: ErrorContext);
}Error Classes
| Class | Status Code | Use Case |
|---|---|---|
ValidationError | 400 | Invalid request body, missing fields |
UnauthorizedError | 401 | Missing or invalid authentication |
ForbiddenError | 403 | Authenticated but not authorized |
NotFoundError | 404 | Resource not found |
DatabaseError | 500 | Database operation failure |
ServiceUnavailableError | 503 | External service down |
UnexpectedError | 500 | Catch-all for unhandled errors |
ForeignKeyViolationError | 409 | Referenced parent row does not exist |
ValidationError
Includes structured field-level errors:
throw new ValidationError("Bad Request", {
email: ["Required", "Must be a valid email"],
name: ["Required"],
});ErrorContext
interface ErrorContext {
[key: string]: unknown;
}All error classes accept an optional ErrorContext as the last constructor argument for structured logging.
ForeignKeyViolationError
Aurora DSQL accepts foreign key syntax but does not enforce it at runtime, so Mantle enforces it in the application layer. assertRowExists and assertRowsExist from @j0nathan-ll0yd/database throw this error when the referenced row is absent.
new ForeignKeyViolationError(table: string, column: string, value: string);The instance exposes table, column, and value; statusCode is 409 and code is FOREIGN_KEY_VIOLATION.
Message Helpers
getErrorMessage(error)
Extract a readable message from an unknown thrown value. Returns the message of an Error, the string itself for a thrown string, and a stable fallback otherwise. Use it wherever a catch binding is typed unknown.
try {
await risky();
} catch (error) {
logError("risky failed", { error: getErrorMessage(error) });
}sanitizeErrorMessage(message)
Strip database internals out of a message before it reaches a client. Messages that look like leaked SQL -- query text, constraint-violation detail -- are replaced with a generic string. buildErrorResponse applies it automatically; call it directly only when building a response by hand.