Unitech / Unitech/pm2

Caught exceptions still getting logged

Open
#5,467 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

What's going wrong?

How can I prevent caught exceptions from being logged in pm2? I wan't to do some error handling for a discord bot, by throwing a custom error for invalid command arguments, but they still get logged together with the unhandled errors.

How could we reproduce this issue?

This is a very simplyfied example that produces the same case as the real project and should work in any node environment.

function echo(ia) {
	ia.reply(ia.message)
}

function commandHandler(interaction) {
	switch (interaction.commandName) {
		case 'echo':
			echo(interaction)
			break

		default:
			throw new Error({ message: 'Unknown command', type: 'Bot Error', interaction })
	}
}

function errorHandler(err) {
	if (err.type === 'Bot Error') err.interaction.reply(err.message)
	else console.error(err)
}

process.on('uncaughtException', errorHandler)

// Start interaction without an actual bot
commandHandler({ commandName: 'asdf', message: 'Hello World!', reply: console.log })

Supporting information

Tested on pm2 version 5.2.2 on both windows and linux with Node version 18.2.0

$ pm2 report

--- PM2 report ----------------------------------------------------------------
Date                 : Mon Oct 17 2022 21:39:58 GMT+0200 (Central European Summer Time)    
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 5.2.2
node version         : 18.2.0
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\Florian\AppData\Roaming\npm\node_modules\pm2\lib\Daemon.js
argv0                : node
user                 : undefined
uid                  : N/A
gid                  : N/A
uptime               : 31min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 5.2.2
node version         : 18.2.0
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\Florian\AppData\Roaming\npm\node_modules\pm2\bin\pm2,report
argv0                : C:\Program Files\nodejs\node.exe
user                 : Florian
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : win32
type                 : Windows_NT
cpus                 : Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
cpus nb              : 12
freemem              : 7049306112
totalmem             : 17106386944
home                 : C:\Users\Florian
===============================================================================
--- PM2 list -----------------------------------------------
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ main               │ fork     │ 0    │ online    │ 0%       │ 61.8mb   │
│ 1  │ test3              │ fork     │ 13   │ online    │ 0%       │ 34.1mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
C:\Users\Florian\.pm2\pm2.log last 20 lines:
PM2        | 2022-10-17T21:32:11: PM2 log: App [test3:1] exited with code [1] via signal [SIGINT]
PM2        | 2022-10-17T21:32:11: PM2 log: pid=15852 msg=process killed
PM2        | 2022-10-17T21:33:51: PM2 log: App [test3:1] starting in -fork mode-
PM2        | 2022-10-17T21:33:51: PM2 log: App [test3:1] online
PM2        | 2022-10-17T21:33:54: PM2 log: App [test3:1] exited with code [0] via signal [SIGINT]
PM2        | 2022-10-17T21:33:54: PM2 log: App [test3:1] starting in -fork mode-
PM2        | 2022-10-17T21:33:54: PM2 log: App [test3:1] online
PM2        | 2022-10-17T21:33:57: PM2 log: App [test3:1] exited with code [0] via signal [SIGINT]
PM2        | 2022-10-17T21:33:57: PM2 log: App [test3:1] starting in -fork mode-
PM2        | 2022-10-17T21:33:57: PM2 log: App [test3:1] online
PM2        | 2022-10-17T21:33:58: PM2 log: Stopping app:test3 id:1
PM2        | 2022-10-17T21:33:58: PM2 log: App [test3:1] exited with code [1] via signal [SIGINT]
PM2        | 2022-10-17T21:33:58: PM2 log: pid=17120 msg=process killed
PM2        | 2022-10-17T21:34:46: PM2 log: App [test3:1] starting in -fork mode-
PM2        | 2022-10-17T21:34:46: PM2 log: App [test3:1] online
PM2        | 2022-10-17T21:35:10: PM2 log: Stopping app:test3 id:1
PM2        | 2022-10-17T21:35:10: PM2 log: App [test3:1] exited with code [1] via signal [SIGINT]
PM2        | 2022-10-17T21:35:10: PM2 log: pid=7992 msg=process killed
PM2        | 2022-10-17T21:35:10: PM2 log: App [test3:1] starting in -fork mode-
PM2        | 2022-10-17T21:35:10: PM2 log: App [test3:1] online

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal JavaScript reproduction using process.on('uncaughtException') and compare its behavior under PM2 with the daemon logs shown in the report. Trace PM2's process and error logging path; done means caught, intentionally handled exceptions are not logged while genuinely unhandled errors remain visible.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.