Creates a new HttpError instance
Initializes an HttpError with comprehensive information about the failed HTTP request. This constructor calls the parent Error constructor to set the basic error message, and additionally stores HTTP-specific details as readonly properties for later access.
The HTTP method that was used for the request (GET, POST, PUT, DELETE, PATCH, HEAD)
The path or URL that was requested (e.g., '/api/users' or 'https://api.example.com/users')
The HTTP status code returned by the server (e.g., 404, 500, 401)
The HTTP response headers returned by the server as key-value pairs
The error message from the server (typically the response body text)
Readonly
headersThe HTTP response headers returned by the server as key-value pairs
Readonly
methodThe HTTP method that was used for the failed request
Readonly
pathThe path or URL that was requested
Optional
stackReadonly
statusThe HTTP status code returned by the server
Static
stackThe Error.stackTraceLimit
property specifies the number of stack frames
collected by a stack trace (whether generated by new Error().stack
or
Error.captureStackTrace(obj)
).
The default value is 10
but may be set to any valid JavaScript number. Changes
will affect any stack trace captured after the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
Serializes the HttpError instance to a JSON-compatible object
This method serves two primary purposes:
JSON.stringify()
is called on an
HttpError, this method is automatically invoked to provide a clean JSON
representationThe method implements lazy parsing for the response message:
A structured object containing all HTTP error information with the message field containing either the parsed JSON object or the original string
Static
captureCreates a .stack
property on targetObject
, which when accessed returns
a string representing the location in the code at which
Error.captureStackTrace()
was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}
.
The optional constructorOpt
argument accepts a function. If given, all frames
above constructorOpt
, including constructorOpt
, will be omitted from the
generated stack trace.
The constructorOpt
argument is useful for hiding implementation
details of error generation from the user. For instance:
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();
Optional
constructorOpt: FunctionStatic
prepare
Specialized error class for HTTP request failures
HttpError
is a custom error class that is thrown when an HTTP request fails and receives an error response from a remote server. Unlike the standard Error class, it carries detailed HTTP-specific information (method, path, status code, headers) that enables more sophisticated error handling and debugging in HTTP communication scenarios.This class is particularly useful for:
Author
Jeongho Nam - https://github.com/samchon