@httpc/kit
Version:
httpc toolbox for building function-based API with minimal code and end-to-end type safety
58 lines (57 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.catchLogAndThrowUnauthorized = exports.catchNotFoundThrowNotFound = exports.catchNotFoundThrowForbidden = exports.catchNotFoundThrowUnauthorized = exports.catchNotFoundThrows = exports.catchNotFound = exports.catchError = void 0;
const server_1 = require("@httpc/server");
const logging_1 = require("../logging");
function catchError(error, func) {
return (err) => {
if ((0, server_1.isErrorOf)(typeof error === "string" ? error : undefined, typeof error === "string" ? undefined : error, err)) {
return func?.(err);
}
throw err;
};
}
exports.catchError = catchError;
function catchNotFound(func) {
return (err) => {
if ((0, server_1.isErrorOf)("not_found", server_1.NotFoundError, err)) {
return func(err);
}
throw err;
};
}
exports.catchNotFound = catchNotFound;
function catchNotFoundThrows(error) {
return (err) => {
if ((0, server_1.isErrorOf)("not_found", server_1.NotFoundError, err)) {
throw error();
}
throw err;
};
}
exports.catchNotFoundThrows = catchNotFoundThrows;
function catchNotFoundThrowUnauthorized(message) {
return catchNotFoundThrows(() => new server_1.UnauthorizedError(message));
}
exports.catchNotFoundThrowUnauthorized = catchNotFoundThrowUnauthorized;
function catchNotFoundThrowForbidden(message) {
return catchNotFoundThrows(() => new server_1.ForbiddenError(message));
}
exports.catchNotFoundThrowForbidden = catchNotFoundThrowForbidden;
function catchNotFoundThrowNotFound(message) {
return catchNotFoundThrows(() => new server_1.NotFoundError(message));
}
exports.catchNotFoundThrowNotFound = catchNotFoundThrowNotFound;
function catchLogAndThrowUnauthorized(log, message) {
return (err) => {
const logger = (0, logging_1.useLogger)();
if (log) {
logger.error(log, err);
}
else {
logger.error(err);
}
throw new server_1.UnauthorizedError(message);
};
}
exports.catchLogAndThrowUnauthorized = catchLogAndThrowUnauthorized;