HttpError
import { HttpError } from "jsr:@ende/http/unstable-error";class ยท experimental
class HttpError extends ErrorAn error class for representing HTTP errors with status codes.
Example: Usage without custom message or options
import { HttpError } from "@ende/http/unstable-error";
import { assertEquals, assertInstanceOf } from "@ende/assert";
try {
throw new HttpError(404);
} catch (error) {
assertInstanceOf(error, HttpError);
assertEquals(error.status, 404);
assertEquals(error.message, "Not Found");
}
Example: Usage with custom message
import { HttpError } from "@ende/http/unstable-error";
import { assertEquals, assertInstanceOf } from "@ende/assert";
try {
throw new HttpError(500, "Something went wrong");
} catch (error) {
assertInstanceOf(error, HttpError);
assertEquals(error.status, 500);
assertEquals(error.message, "Something went wrong");
}
Example: Usage with response init options
import { HttpError } from "@ende/http/unstable-error";
import { assertEquals, assertInstanceOf } from "@ende/assert";
try {
throw new HttpError(403, "Forbidden", {
init: { headers: { "WWW-Authenticate": 'Basic realm="Secure Area"' } },
});
} catch (error) {
assertInstanceOf(error, HttpError);
assertEquals(error.status, 403);
assertEquals(error.message, "Forbidden");
assertEquals(
(error.init.headers as Record<string, string>)["WWW-Authenticate"],
'Basic realm="Secure Area"',
);
}
Example: Usage with cause
import { HttpError } from "@ende/http/unstable-error";
import { assertEquals, assertInstanceOf } from "@ende/assert";
try {
throw new HttpError(500, "Internal Server Error", {
cause: new Error("Database connection failed"),
});
} catch (error) {
assertInstanceOf(error, HttpError);
assertEquals(error.status, 500);
assertEquals(error.message, "Internal Server Error");
assertInstanceOf(error.cause, Error);
assertEquals(error.cause?.message, "Database connection failed");
}
Constructors
constructor
new HttpError(status: ErrorStatus, message: string = [STATUS_TEXT[UNSUPPORTED]], options?: HttpErrorOptions)Constructs a new instance.
status: ErrorStatusThe HTTP status code (e.g., 404, 500, 403)
message: string = [STATUS_TEXT[UNSUPPORTED]]Optional error message. Defaults to the standard status text for the given status code
options?: HttpErrorOptionsOptional error options including cause and response init configuration
Properties
status
status: ErrorStatusThe HTTP status code (e.g., 404, 500, 403)
Example: Usage
import { HttpError } from "@ende/http/unstable-error";
import { assertEquals, assertInstanceOf } from "@ende/assert";
try {
throw new HttpError(404);
} catch (error) {
assertInstanceOf(error, HttpError);
assertEquals(error.status, 404);
assertEquals(error.message, "Not Found");
}
init
init: ResponseInitConfiguration options for the HTTP response associated with this error.
init.status always equals the status argument passed to the constructor
and represents the HTTP status code. Other ResponseInit fields
(headers, statusText) come from options.init if supplied.
Example: Usage
import { HttpError } from "@ende/http/unstable-error";
import { assertEquals, assertInstanceOf } from "@ende/assert";
try {
throw new HttpError(403, "Forbidden", {
init: { headers: { "WWW-Authenticate": 'Basic realm="Secure Area"' } },
});
} catch (error) {
assertInstanceOf(error, HttpError);
assertEquals(error.status, 403);
assertEquals(error.message, "Forbidden");
assertEquals(
(error.init.headers as Record<string, string>)["WWW-Authenticate"],
'Basic realm="Secure Area"',
);
}