Skip to Content
gentlemans-stack
ReferenceLibs

app-errors

Backend-only error handling: AppError for the errors code throws on purpose, and handleError, which classifies anything thrown during a request. Public codes, statuses and metadata schemas live in app-error-model; this package adds the private side (internal codes and diagnostic context) and must never be imported by contracts or clients.

API

  • AppError (class), isAppError, handleError: throw, classify and normalize errors.
  • allowedErrors, extractWhereFromError: helpers for error classification.
  • Types: AppErrorCodes, AppErrorContext, AppErrorMetadata, AllowedError, ClassifiedError, ErrorHandlerReturnType, InternalErrorCode.

Throwing

throw new AppError("RESOURCE_NOT_FOUND", { resource: "todo" }, { query: { id } });

metadata (second argument) is parsed against the code's public schema and is sent to the client. context (third argument) is private: it is logged as diagnosticContext and never sent.

A server-side fault that the caller cannot fix, such as a procedure with no RBAC intent in its contract meta, is an INTERNAL_SERVICE_ERROR whose reason stays in the context.

throw new AppError("INTERNAL_SERVICE_ERROR", {}, { cause: "missing_rbac_intent", path });

There is no public CONFIGURATION_ERROR; see docs/learnings.md (plan 3.4).

Classifying

handleError(error) returns { appErrorCode, data, errorMessage, errorName, httpStatus } (the public part) plus internalErrorCode, diagnosticContext and where (stack file, private). libs/server-setup is its only caller: it logs the whole record with the request's evlog logger and sends only the public part as an ORPCError.

ThrownPublic code (status)Public datainternalErrorCodediagnosticContext
AppError(code, metadata, context)code (its status)metadatacodecontext
oRPC input validation failureINPUT_VALIDATION_ERROR (400){ issues }INPUT_VALIDATION_ERRORnone
oRPC output validation failureINTERNAL_SERVICE_ERROR (500){}OUTPUT_VALIDATION_ERROR{ issues, validationMessage }
a MongoDB driver error (error.name starts with Mongo; the driver is never imported)INTERNAL_SERVICE_ERROR (500){}MONGO_DB_ERROR{ code, errmsg }
anything else, including a SyntaxError thrown by a procedureINTERNAL_SERVICE_ERROR (500){}UNKNOWN_ERROR{ cause: message }

A request body that is not JSON fails before any procedure runs; libs/server-setup turns it into AppError("SYNTAX_ERROR", { cause }) (400) and sends it through the same path.

Commands

pnpm nx run app-errors:typecheck   # type-check the package
pnpm nx run app-errors:test        # run the Vitest suite

On this page