TalawaRestError
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
details?
unknown
message
string
statusCodeOverride?
number
Returns
TalawaRestError
Overrides
Error.constructor
Properties
code
readonlycode:ErrorCode
Defined in: src/utilities/errors/TalawaRestError.ts:38
The standardized error code
details?
readonlyoptionaldetails:unknown
Defined in: src/utilities/errors/TalawaRestError.ts:42
Optional additional error context
statusCode
readonlystatusCode: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
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"
// }
// }