arkyn

v3.0.1-beta.122

NotFound

The NotFound class represents an HTTP error response with status code 404. It is used to standardize "Not Found" error responses, typically when the requested resource does not exist.

Import

ts

import { NotFound } from "@arkyn/server";

Constructor

  • message (required): A descriptive message explaining why the resource was not found.
  • cause (optional): Additional information about the error cause, which can be any serializable data.

Methods

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.

Usage example

typescript

import { NotFound } from "@arkyn/server";
// Basic usage - throw the error
throw new NotFound("User not found");
// With cause information
throw new NotFound("Product not found", {
productId: "abc123",
searchedIn: "products_table",
});
// Convert to Response object
const error = new NotFound("Order not found");
return error.toResponse();
// Using toJson alternative
return error.toJson();

Response structure

The response body follows a standardized structure:

json

{
"ok": false,
"status": 404,
"message": "User not found"
}

Notes

When thrown, this class automatically emits a debug log to the console showing the file and function where the error originated. See DebugService to configure ignored files for accurate caller detection.
The cause parameter is serialized to JSON and stored for debugging purposes but is not included in the response body sent to clients.
Common use cases include missing database records, invalid resource IDs, deleted resources, and non-existent API endpoints.
On this page