ServerError class represents an HTTP error response with status code 500. It is used to standardize "Internal Server Error" responses, typically when an unexpected error occurs on the server side.ts
import { ServerError } from "@arkyn/server/serverError";
message (required): A descriptive message explaining the server error cause.cause (optional): Additional information about the error cause, which can be any serializable data.toResponse() - Converts the instance into a Response object with JSON body and Content-Type: application/json header.toJson() - Alternative method using Response.json() for generating the JSON error response.typescript
import { ServerError } from "@arkyn/server/serverError";// Basic usage - throw the errorthrow new ServerError("An unexpected error occurred");// With cause informationthrow new ServerError("Database operation failed", {operation: "insert",table: "users",originalError: "Connection lost",});// Convert to Response objectconst error = new ServerError("Failed to process request");return error.toResponse();// Using toJson alternativereturn error.toJson();
json
{"name": "ServerError","message": "An unexpected error occurred"}
500, set on the Response object itself, the status code is not part of the JSON body.cause is passed to the constructor, it is included in the body as well:json
{"name": "ServerError","message": "Database operation failed","cause": { "operation": "insert", "table": "users", "originalError": "Connection lost" }}
cause parameter, when provided, is included in the response body under the cause key as-is, it is sent to clients, not just kept for server-side debugging. Since v3.0.12, cause is no longer serialized with JSON.stringify() first, it's stored and returned exactly as passed (an object stays a nested object, not a JSON-encoded string). See Breaking Changes.cause is omitted from the response body entirely when NODE_ENV === "production", to avoid leaking internal error detail (stack traces, SQL fragments, infrastructure info) to clients. message and name are unaffected in every environment. See Breaking Changes.