HttpStatusEnhancer

File: http-status.enhancer.ts

Overview

The HttpStatusEnhancer class is designed to augment error objects with HTTP-specific metadata. This can be valuable for debugging, monitoring, and auditing HTTP-related issues within applications.

Properties

PropertyDescription

httpStatusCode

HTTP Status Code, default is -1.

url

URL where the error occurred, default is an empty string.

httpMethod

HTTP Method used (GET, POST, etc.), default is an empty string.

requestHeaders

HTTP request headers, default is an empty object.

responseHeaders

HTTP response headers, default is an empty object.

queryParams

Query parameters in the URL, default is an empty object.

requestBody

Body of the HTTP request, default is null.

responseBody

Body of the HTTP response, default is null.

clientIp

Client's IP address, default is an empty string.

latency

Latency in milliseconds, default is -1.

Methods

setHttpStatusCode(httpStatusCode: number)

Sets the HTTP status code and validates it against known status codes.

Usage Example:

const httpStatus = new HttpStatusEnhancer();
httpStatus.setHttpStatusCode(200);

setUrl(url: string)

Sets the URL where the error occurred and validates its format.

Usage Example:

httpStatus.setUrl("https://example.com");

setHttpMethod(httpMethod: string)

Sets the HTTP method used in the request and validates it against known methods.

Usage Example:

httpStatus.setHttpMethod("GET");

setRequestHeaders(headers: { [key: string]: any })

Sets the request headers.

Usage Example:

httpStatus.setRequestHeaders({ "Content-Type": "application/json" });

setResponseHeaders(headers: { [key: string]: any })

Sets the response headers.

Usage Example:

httpStatus.setResponseHeaders({ "Content-Type": "application/json" });

setQueryParams(params: { [key: string]: any })

Sets the query parameters.

Usage Example:

httpStatus.setQueryParams({ "page": 1, "limit": 10 });

setRequestBody(body: any)

Sets the body of the HTTP request.

Usage Example:

httpStatus.setRequestBody({ "key": "value" });

setResponseBody(body: any)

Sets the body of the HTTP response.

Usage Example:

httpStatus.setResponseBody({ "key": "value" });

setClientIp(ip: string)

Sets the client's IP address.

Usage Example:

httpStatus.setClientIp("192.168.0.1");

setLatency(latency: number)

Sets the latency of the request in milliseconds.

Usage Example:

httpStatus.setLatency(120);