Error objects with ad-hoc properties, catch blocks have no reliable structure to work with — you end up guessing field names or silencing errors entirely. @figentra/error solves this by providing a hierarchy of typed error classes that carry a machine-readable code, an HTTP statusCode, and optional structured details. Every class in the hierarchy extends the native Error, so existing tooling — stack traces, instanceof checks, serialisation libraries — all work without modification.
Installation
Built-in Error Classes
FigentraError — base class
All Figentra errors extend FigentraError. It adds code, statusCode, and details to the standard Error interface. Throw this directly for errors that don’t fit a more specific subclass.
NotFoundError — 404
Throw when a requested resource does not exist. Maps to HTTP 404 Not Found.
ValidationError — 422
Throw when user-supplied input fails validation. Pass structured field errors in details so the caller can surface actionable feedback.
UnauthorizedError — 401
Throw when the caller is not authenticated or their credentials are invalid. Maps to HTTP 401 Unauthorized.
ConflictError — 409
Throw when an operation would violate a uniqueness constraint or create a conflicting state. Maps to HTTP 409 Conflict.
InternalError — 500
Throw for unexpected failures that are not the caller’s fault. Maps to HTTP 500 Internal Server Error. Avoid leaking sensitive details in the message; use details for internal diagnostic info that your error-reporting middleware can log.
Creating Custom Errors
ExtendFigentraError to create domain-specific error classes. Pass your code and statusCode to super so the base class serialises correctly.
instanceof checks and in FigentraError.isFigentraError() narrowing, so existing error-handling middleware picks them up automatically.
Catching and Handling
Useinstanceof to distinguish between specific error types in catch blocks. Always re-throw errors that are not FigentraError instances — those are unexpected and should surface as 500s or be handled by a top-level error boundary.
Error Shape
All Figentra errors serialise to theErrorContract type defined in @figentra/contracts. Align your HTTP response bodies to this shape for a consistent API surface across all services.
.toContract() helper method on any FigentraError instance to produce a plain ErrorContract object safe for JSON serialisation: