Skip to content

@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.

typescript
class CustomLambdaError extends Error {
  readonly statusCode: number;
  readonly context?: ErrorContext;
  constructor(message: string, statusCode?: number, context?: ErrorContext);
}

Error Classes ​

ClassStatus CodeUse Case
ValidationError400Invalid request body, missing fields
UnauthorizedError401Missing or invalid authentication
ForbiddenError403Authenticated but not authorized
NotFoundError404Resource not found
DatabaseError500Database operation failure
ServiceUnavailableError503External service down
UnexpectedError500Catch-all for unhandled errors
ForeignKeyViolationError409Referenced parent row does not exist

ValidationError ​

Includes structured field-level errors:

typescript
throw new ValidationError("Bad Request", {
  email: ["Required", "Must be a valid email"],
  name: ["Required"],
});

ErrorContext ​

typescript
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.

typescript
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.

typescript
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.