http-micro
Version:
Micro-framework on top of node's http module
115 lines (114 loc) • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const httpError = require("http-errors");
exports.InnerErrorKey = "cause";
exports.HttpErrorStatusCodeKey = "statusCode";
function getErrorInfo(err) {
return err.stack || err.toString();
}
exports.getErrorInfo = getErrorInfo;
/**
* Check if the status is a 4xx or 5xx status code.
*/
function isHttpErrorStatusCode(code) {
return Number.isInteger(code) && code > 399 && code < 600;
}
exports.isHttpErrorStatusCode = isHttpErrorStatusCode;
/**
* Check if the status is a 4xx status code.
*/
function isHttpClientErrorStatusCode(code) {
return Number.isInteger(code) && code > 399 && code < 500;
}
exports.isHttpClientErrorStatusCode = isHttpClientErrorStatusCode;
/**
* Check if the 'statusCode' property of error object
* is a valid 4xx or 5xx status code. Return the code
* if it is, or else return 500.
*/
function getHttpErrorStatusCode(err) {
let errObj = err;
let status = errObj[exports.HttpErrorStatusCodeKey];
if (!isHttpErrorStatusCode(status)) {
return 500;
}
return status;
}
exports.getHttpErrorStatusCode = getHttpErrorStatusCode;
/**
* Check if an error object is a valid http error, by
* testing if 'statusCode' property of the error object
* is a valid 4xx or 5xx status code.
*/
function isHttpError(err) {
let errObj = err;
let status = errObj[exports.HttpErrorStatusCodeKey];
return isHttpErrorStatusCode(status);
}
exports.isHttpError = isHttpError;
/**
* Wrap an error into another error using the defacto
* 'cause' property.
*/
function wrapError(targetError, originalError, linkMessage = true) {
targetError[exports.InnerErrorKey] = originalError;
if (linkMessage)
targetError.message = originalError.message;
}
exports.wrapError = wrapError;
function intoHttpError(err, code, forceCode = false) {
let errObj;
if (isHttpError(err)) {
let e = err;
if (!forceCode || code === e.statusCode) {
errObj = e;
return errObj;
}
}
errObj = httpError(code);
wrapError(errObj, err);
return errObj;
}
exports.intoHttpError = intoHttpError;
function errorToResponse(err, res) {
let status = getHttpErrorStatusCode(err);
if (!res.headersSent) {
res.statusCode = status;
}
if (!res.finished) {
// TODO: if ((err as any).expose) print error.
res.end();
}
}
exports.errorToResponse = errorToResponse;
function makeNestedErrorIterable(err) {
return {
[Symbol.iterator]() {
let innerError = err;
return {
next: function () {
if (!innerError)
return { done: true, value: null };
let value = innerError;
innerError = innerError[exports.InnerErrorKey];
return { done: false, value };
}
};
}
};
}
exports.makeNestedErrorIterable = makeNestedErrorIterable;
function* recurseErrorInfo(err) {
let iter = makeNestedErrorIterable(err);
let i = -1;
for (let e of iter) {
if (i === -1) {
yield `http-micro error: ${getErrorInfo(e)}`;
}
else {
yield `-> cause[${i}]: ${getErrorInfo(e)}`;
}
i++;
}
}
exports.recurseErrorInfo = recurseErrorInfo;