Unauthorized class represents an HTTP error response with status code 401. It is used to standardize "Unauthorized" error responses, typically when the user is not authenticated or the authentication credentials are invalid.ts
import { Unauthorized } from "@arkyn/server";
message (required): A descriptive message explaining why the request is unauthorized.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 { Unauthorized } from "@arkyn/server";// Basic usage - throw the errorthrow new Unauthorized("Authentication required");// With cause informationthrow new Unauthorized("Invalid token", {tokenType: "JWT",reason: "Token expired",});// Convert to Response objectconst error = new Unauthorized("Please log in to continue");return error.toResponse();// Using toJson alternativereturn error.toJson();
json
{"ok": false,"status": 401,"message": "Authentication required"}
cause parameter is serialized to JSON and stored for debugging purposes but is not included in the response body sent to clients.Unauthorized (401) when the user is not authenticated. Use Forbidden (403) when the user is authenticated but lacks permission to access the resource.