Skip to content

Commit

Permalink
Merge branch 'main' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 committed Mar 10, 2024
2 parents cb0710a + ed2cfd7 commit a83be5c
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/automatic-versioning/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sliit-foss/automatic-versioning",
"version": "2.3.2",
"version": "2.3.4",
"description": "A script which will automatically increment your app package version in accordance with conventional commits",
"main": "dist/index.js",
"bin": "dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/automatic-versioning/src/types/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ const runner = (
if (!noCommit) {
const successMsg = `"CI: ${name} - ${
versionUpdate === "prerelease" ? versionUpdate : `${versionUpdate} release`
}"`;
}\n\n\nskip-checks: true"`;
await run("git add .").then(async () => {
await run(`git commit -m ${successMsg} --trailer "skip-checks:true" --no-verify`).then(() =>
await run(`git commit -m ${successMsg} --no-verify --cleanup=verbatim`).then(() =>
console.info(successMsg.green)
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sliit-foss/functions",
"version": "2.7.1",
"version": "2.9.1",
"description": "Just a small set of utility functions",
"main": "dist/index.js",
"types": "types/index.d.ts",
Expand Down
59 changes: 40 additions & 19 deletions packages/functions/src/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,52 @@ const logger = moduleLogger("tracer");

const _asyncHandler =
(fn, trace = false) =>
async (req, res, next) => {
let fnName;
try {
if (trace) {
fnName = _fnName(fn);
await _traced(fn.bind(this, req, res, next), {}, fnName);
} else {
await fn(req, res, next);
async (req, res, next) => {
let fnName;
try {
if (trace) {
fnName = _fnName(fn);
await _traced(fn.bind(this, req, res, next), {}, fnName);
} else {
await fn(req, res, next);
}
if (!res.headersSent) next();
} catch (err) {
if (!trace) {
fnName = fnName ?? _fnName(fn);
logger.error(`${fnName} execution failed - error: ${err.message} - stack: ${err.stack}`);
}
res.errorLogged = true;
if (!res.headersSent) next(err);
}
if (!res.headersSent) next();
} catch (err) {
if (!trace) {
fnName = fnName ?? _fnName(fn);
logger.error(`${fnName} execution failed - error: ${err.message} - stack: ${err.stack}`);
}
res.errorLogged = true;
if (!res.headersSent) next(err);
}
};
};

export const asyncHandler = (fn) => _asyncHandler(fn);

export const tracedAsyncHandler = (fn) => _asyncHandler(fn, true);

export const fallibleAsyncHandler = (fn) => async (req, res, next) => {
try {
await _traced(fn.bind(this, req, res, next), {}, _fnName(fn), null, true);
if (!res.headersSent) next();
} catch (err) {
res.errorLogged = true;
if (!res.headersSent) next(err);
}
};

export const plainAsyncHandler = (fn) => async (req, res, next) => {
try {
const result = fn(req, res, next)
if (result instanceof Promise) await result;
} catch (e) {
next(e)
}
}

export default {
asyncHandler,
tracedAsyncHandler
tracedAsyncHandler,
fallibleAsyncHandler,
plainAsyncHandler,
};
7 changes: 3 additions & 4 deletions packages/functions/src/traced.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { fnName as _fnName } from "./utils";

const logger = moduleLogger("tracer");

export const _traced = (fn, loggable = {}, fnName, layer) => {
export const _traced = (fn, loggable = {}, fnName, layer, fallible) => {
let startTime;
const disableTracing =
process.env.DISABLE_FUNCTION_TRACING === "true" || process.env.DISABLE_FUNCTION_TRACING === "1";
const disableTracing = process.env.DISABLE_FUNCTION_TRACING === "true" || process.env.DISABLE_FUNCTION_TRACING === "1";
if (!disableTracing) {
fnName = fnName ?? _fnName(fn, layer);
logger.info(`${fnName} execution initiated`, loggable);
Expand All @@ -25,7 +24,7 @@ export const _traced = (fn, loggable = {}, fnName, layer) => {
};
const failureLog = (err) => {
if (!disableTracing && !err.isLogged) {
logger.error(
logger[fallible ? "warn" : "error"](
`${fnName} execution failed - ${chalk.bold("error")}: ${err.message} - ${chalk.bold("stack")}: ${err.stack}`
);
err.isLogged = true;
Expand Down
3 changes: 2 additions & 1 deletion packages/functions/test/__mocks__/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const mockLogger = {
info: jest.fn().mockImplementation((msg) => console.info(msg)),
error: jest.fn().mockImplementation((msg) => console.error(msg))
error: jest.fn().mockImplementation((msg) => console.error(msg)),
warn: jest.fn().mockImplementation((msg) => console.warn(msg))
};

jest.mock("@sliit-foss/module-logger", () => ({
Expand Down
33 changes: 26 additions & 7 deletions packages/functions/test/async.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { mockLogger } = require("./__mocks__");

const { asyncHandler, tracedAsyncHandler } = require("../src");
const { asyncHandler, tracedAsyncHandler, fallibleAsyncHandler, plainAsyncHandler } = require("../src");
const { coloredFnName } = require("../src/utils");

beforeEach(() => {
Expand All @@ -17,22 +17,41 @@ describe("asyncHandler", () => {
return "test";
}
await asyncHandler(testFunction)(mockReq, mockRes, mockNext);
expect(mockLogger.info).not.toBeCalled();
expect(mockNext).toBeCalled();
expect(mockLogger.info).not.toHaveBeenCalled();
expect(mockNext).toHaveBeenCalled();
});
test("test async handler with error", async () => {
function testFunction() {
throw new Error("test error");
}
await asyncHandler(testFunction)(mockReq, mockRes, mockNext);
expect(mockLogger.error).toBeCalled();
expect(mockNext).toBeCalled();
expect(mockLogger.error).toHaveBeenCalled();
expect(mockNext).toHaveBeenCalled();
});
test("test traced async handler", async () => {
await tracedAsyncHandler(function testTracedFunction() {
return "test";
})(mockReq, mockRes, mockNext);
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("testTracedFunction")} execution initiated`, {});
expect(mockNext).toBeCalled();
expect(mockLogger.info).toHaveBeenCalledWith(`${coloredFnName("testTracedFunction")} execution initiated`, {});
expect(mockNext).toHaveBeenCalled();
});
test("test fallible async handler", async () => {
await fallibleAsyncHandler(function testTracedFunction() {
throw new Error("test error");
})(mockReq, mockRes, mockNext);
expect(mockLogger.warn).toHaveBeenCalled();
expect(mockNext).toHaveBeenCalled();
});
test("test plain async handler with async function", async () => {
await plainAsyncHandler(async () => {
throw new Error("test")
})(mockReq, mockRes, mockNext);
expect(mockNext).toHaveBeenCalled();
});
test("test plain async handler with normal function", async () => {
await plainAsyncHandler(() => {
throw new Error("test")
})(mockReq, mockRes, mockNext);
expect(mockNext).toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion packages/functions/test/traced.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("traced", () => {
});
test("test disabled tracing", () => {
process.env.DISABLE_FUNCTION_TRACING = "true";
const res = traced(() => _mockResult)();
const res = require("../src").traced(() => _mockResult)();
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).not.toBeCalled();
delete process.env.DISABLE_FUNCTION_TRACING;
Expand Down
16 changes: 15 additions & 1 deletion packages/functions/types/async.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ export function asyncHandler(fn: Function): (req: any, res: any, next: Function)
*/
export function tracedAsyncHandler(fn: Function): (req: any, res: any, next: Function) => Promise<void>;

/**
* @description Same as the `tracedAsyncHandler` but the log upon failure is a warning log
* @param fn The function to be invoked asynchronously
*/
export function fallibleAsyncHandler(fn: Function): (req: any, res: any, next: Function) => Promise<void>;

/**
* @description A more stripped down version of asyncHandler without any logs
* @param fn The function to be invoked asynchronously
*/
export function plainAsyncHandler(fn: Function): (req: any, res: any, next: Function) => Promise<void>;

export default {
asyncHandler,
tracedAsyncHandler
tracedAsyncHandler,
fallibleAsyncHandler,
plainAsyncHandler
};

0 comments on commit a83be5c

Please sign in to comment.