@donation-alerts/api-call
Version:
Make calls to Donation Alerts API.
59 lines (58 loc) • 1.75 kB
JavaScript
import { CustomError } from '@donation-alerts/common';
/**
* Represents an HTTP-related error.
*
* @remarks
* This error is thrown whenever an HTTP request encounters a non-successful HTTP status code.
* It includes detailed information about the request, including the status code, status text,
* URL, HTTP method, and response body.
*
* The `HttpError` class masks lengthy non-JSON response bodies for readability
* in the error message.
*/
export class HttpError extends CustomError {
/** @internal */
constructor(_status, _statusText, _url, _method, _body, isJson) {
super(`Encountered HTTP status code ${_status}: ${_statusText}\n\nURL: ${_url}\nMethod: ${_method}\nBody:\n${!isJson && _body.length > 150 ? `${_body.slice(0, 147)}...` : _body}`);
this._status = _status;
this._statusText = _statusText;
this._url = _url;
this._method = _method;
this._body = _body;
}
/**
* The HTTP status code of the error.
*/
get status() {
return this._status;
}
/**
* The HTTP status text associated with the error.
*/
get statusText() {
return this._statusText;
}
/**
* The URL of the request that caused the error.
*/
get url() {
return this._url;
}
/**
* The HTTP method used in the request.
*/
get method() {
return this._method;
}
/**
* The response body of the request.
*
* @remarks
* If the response body was excessively long (and not JSON), it might be truncated
* in the error message for readability. Use this property to access the full
* response body if needed.
*/
get body() {
return this._body;
}
}