Skip to main content

TalawaRestError

API Docs


Class: TalawaRestError

Defined in: src/utilities/errors/TalawaRestError.ts:36

Custom error class for REST API endpoints with standardized error codes and HTTP status mapping.

TalawaRestError provides a structured way to throw errors in REST routes that will be automatically transformed by the global error handler into consistent error responses.

Example​

// Basic usage
throw new TalawaRestError({
code: ErrorCode.NOT_FOUND,
message: "User not found"
});

// With additional details
throw new TalawaRestError({
code: ErrorCode.NOT_FOUND,
message: "User not found",
details: { userId: "123", requestedBy: "admin" }
});

// With custom status code override
throw new TalawaRestError({
code: ErrorCode.NOT_FOUND,
message: "Custom error",
statusCodeOverride: 418
});

Extends​

  • Error

Constructors​

Constructor​

new TalawaRestError(args): TalawaRestError

Defined in: src/utilities/errors/TalawaRestError.ts:53

Creates a new TalawaRestError instance.

Parameters​

args​

Error configuration object containing:

  • code: Standardized error code from ErrorCode enum
  • message: Human-readable error message
  • details: Optional additional error context and details
  • statusCodeOverride: Optional HTTP status code override
code​

ErrorCode

details?​

unknown

message​

string

statusCodeOverride?​

number

Returns​

TalawaRestError

Overrides​

Error.constructor

Properties​

code​

readonly code: ErrorCode

Defined in: src/utilities/errors/TalawaRestError.ts:38

The standardized error code


details?​

readonly optional details: unknown

Defined in: src/utilities/errors/TalawaRestError.ts:42

Optional additional error context


statusCode​

readonly statusCode: number

Defined in: src/utilities/errors/TalawaRestError.ts:40

HTTP status code for this error

Methods​

toJSON()​

toJSON(correlationId?): StandardErrorPayload

Defined in: src/utilities/errors/TalawaRestError.ts:95

Converts the error to a standardized JSON response format.

This method is used by the global error handler to create consistent error responses for REST endpoints.

Parameters​

correlationId?​

string

Optional request correlation ID for tracing

Returns​

StandardErrorPayload

Standardized error payload object

Example​

const error = new TalawaRestError({
code: ErrorCode.NOT_FOUND,
message: "User not found",
details: { userId: "123" }
});

const payload = error.toJSON("req-abc123");
// Returns: {
// error: {
// code: "not_found",
// message: "User not found",
// details: { userId: "123" },
// correlationId: "req-abc123"
// }
// }