diff --git a/packages/automatic-versioning/package.json b/packages/automatic-versioning/package.json index 69fa82e..cd23e05 100644 --- a/packages/automatic-versioning/package.json +++ b/packages/automatic-versioning/package.json @@ -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", diff --git a/packages/automatic-versioning/src/types/default.js b/packages/automatic-versioning/src/types/default.js index 975fdb2..df7bf78 100644 --- a/packages/automatic-versioning/src/types/default.js +++ b/packages/automatic-versioning/src/types/default.js @@ -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) ); }); diff --git a/packages/functions/package.json b/packages/functions/package.json index e5b75a3..ddc01d0 100644 --- a/packages/functions/package.json +++ b/packages/functions/package.json @@ -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", diff --git a/packages/functions/src/async.js b/packages/functions/src/async.js index 7745efe..9e29e6a 100644 --- a/packages/functions/src/async.js +++ b/packages/functions/src/async.js @@ -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, }; diff --git a/packages/functions/src/traced.js b/packages/functions/src/traced.js index 7149d67..b6f8e31 100644 --- a/packages/functions/src/traced.js +++ b/packages/functions/src/traced.js @@ -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); @@ -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; diff --git a/packages/functions/test/__mocks__/logger.js b/packages/functions/test/__mocks__/logger.js index 00b640c..850d2fe 100644 --- a/packages/functions/test/__mocks__/logger.js +++ b/packages/functions/test/__mocks__/logger.js @@ -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", () => ({ diff --git a/packages/functions/test/async.test.js b/packages/functions/test/async.test.js index bb86f8b..1838092 100644 --- a/packages/functions/test/async.test.js +++ b/packages/functions/test/async.test.js @@ -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(() => { @@ -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(); }); }); diff --git a/packages/functions/test/traced.test.js b/packages/functions/test/traced.test.js index f8843ce..aab4f10 100644 --- a/packages/functions/test/traced.test.js +++ b/packages/functions/test/traced.test.js @@ -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; diff --git a/packages/functions/types/async.d.ts b/packages/functions/types/async.d.ts index ab95609..955bfc8 100644 --- a/packages/functions/types/async.d.ts +++ b/packages/functions/types/async.d.ts @@ -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; +/** + * @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; + +/** + * @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; + export default { asyncHandler, - tracedAsyncHandler + tracedAsyncHandler, + fallibleAsyncHandler, + plainAsyncHandler };