Skip to content

Commit

Permalink
Feat(functions): added fallible async handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 authored Mar 9, 2024
1 parent a8f25d3 commit e42f71e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
47 changes: 29 additions & 18 deletions packages/functions/src/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,40 @@ 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)
Expand All @@ -42,5 +52,6 @@ export const plainAsyncHandler = (fn) => async (req, res, next) => {
export default {
asyncHandler,
tracedAsyncHandler,
fallibleAsyncHandler,
plainAsyncHandler,
};
8 changes: 4 additions & 4 deletions packages/functions/src/traced.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { fnName as _fnName } from "./utils";

const logger = moduleLogger("tracer");

export const _traced = (fn, loggable = {}, fnName, layer) => {
const disableTracing = process.env.DISABLE_FUNCTION_TRACING === "true" || process.env.DISABLE_FUNCTION_TRACING === "1";

export const _traced = (fn, loggable = {}, fnName, layer, fallible) => {
let startTime;
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 +25,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
9 changes: 8 additions & 1 deletion 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, plainAsyncHandler } = require("../src");
const { asyncHandler, tracedAsyncHandler, fallibleAsyncHandler, plainAsyncHandler } = require("../src");
const { coloredFnName } = require("../src/utils");

beforeEach(() => {
Expand Down Expand Up @@ -35,6 +35,13 @@ describe("asyncHandler", () => {
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")
Expand Down

0 comments on commit e42f71e

Please sign in to comment.