How to print all rejections in NodeJS

Published: May 2, 2022 by nemanjan00

Did you forget to print error on rejection, in a huge project and are now having issues debugging it? I wrote a snippet of code to solve that.

const promiseProto = new global.Promise(() => {}).__proto__;

const originalCatch = promiseProto.catch; // grab original catch implementation

promiseProto.catch = function(...args) { // wrap around catch implementation, to print error
	const originalCallback = args[0];

	args[0] = (...errArgs) => {
		console.error(errArgs[0]);

		originalCallback(...errArgs);
	};

	return originalCatch.apply(this, args);
};

Promise.reject(new Error("Just a terrible error")).catch(() => {
	// forgoten rejection
});

Share

Latest Posts

Radio modulation
Radio modulation

RF is like jumping into a pool with cold water. At first, you are like WTF am I doing, but, then, you realize, you are already creating some waves…

Decoding CTCSS tone
How to print all rejections in NodeJS
How to print all rejections in NodeJS

Did you forget to print error on rejection, in a huge project and are now having issues debugging it? I wrote a snippet of code to solve that.