inabahare / lewd2

A based file uploader
https://lewd.se
GNU General Public License v3.0
23 stars 3 forks source link

Terminate application upon critical unrecoverable errors #88

Open kattjevfel opened 3 years ago

kattjevfel commented 3 years ago

When encountering an error that cannot be recovered from without doing some manual fuckery or at the very least a restart of lewd, exit with a non-zero exit code to signal shit has hit the fan.

Example (permission denied for database, keeps trying anyway and service remains active):

Aug 01 23:50:37 main systemd[1]: Started lewd.se main application.
Aug 01 23:50:37 main node[385]: Serving files from: /var/www/lewd2/packages/frontend/dist
Aug 01 23:50:37 main node[385]: It's up and running in development mode on port 8080 :3
Aug 01 23:50:37 main node[385]: (node:385) UnhandledPromiseRejectionWarning: error: permission denied for table Users
Aug 01 23:50:37 main node[385]:     at Parser.parseErrorMessage (/var/www/lewd2/packages/main-site/node_modules/pg-protocol/dist/parser.js:278:15)
Aug 01 23:50:37 main node[385]:     at Parser.handlePacket (/var/www/lewd2/packages/main-site/node_modules/pg-protocol/dist/parser.js:126:29)
Aug 01 23:50:37 main node[385]:     at Parser.parse (/var/www/lewd2/packages/main-site/node_modules/pg-protocol/dist/parser.js:39:38)
Aug 01 23:50:37 main node[385]:     at Socket.<anonymous> (/var/www/lewd2/packages/main-site/node_modules/pg-protocol/dist/index.js:8:42)
Aug 01 23:50:37 main node[385]:     at Socket.emit (events.js:315:20)
Aug 01 23:50:37 main node[385]:     at addChunk (_stream_readable.js:295:12)
Aug 01 23:50:37 main node[385]:     at readableAddChunk (_stream_readable.js:271:9)
Aug 01 23:50:37 main node[385]:     at Socket.Readable.push (_stream_readable.js:212:10)
Aug 01 23:50:37 main node[385]:     at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
Aug 01 23:50:37 main node[385]: (node:385) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejection
Aug 01 23:50:37 main node[385]: (node:385) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Example 2 (Discord bot got an invalid token but still exits with code 0, marking the service as finished instead of failed):

Aug 01 23:50:37 main systemd[1]: Started lewd.se Discord bot.
Aug 01 23:50:37 main node[384]: (node:384) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
Aug 01 23:50:37 main node[384]:     at WebSocketManager.connect (/var/www/lewd2/packages/discord/node_modules/discord.js/src/client/websocket/WebSocketManager.js:135:26)
Aug 01 23:50:37 main node[384]:     at Client.login (/var/www/lewd2/packages/discord/node_modules/discord.js/src/client/Client.js:221:21)
Aug 01 23:50:37 main node[384]: (node:384) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejection
Aug 01 23:50:37 main node[384]: (node:384) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Aug 01 23:50:37 main systemd[1]: lewd2-discord.service: Succeeded.
inabahare commented 3 years ago

This is only the case for running in development mode as I've set it up so it does a more graceful death (which messes with debugging tools :v). In production more the following will happen

inaba@japan ~/Programming/Lewd/packages/discord (master*) $ npm run start

> lewd-discord@1.0.0 start /home/inaba/Programming/Lewd/packages/discord
> node ./dist/index.js

<discord.js>
Reason:  Error [TOKEN_INVALID]: An invalid token was provided.
    at WebSocketManager.connect (/home/inaba/Programming/Lewd/packages/discord/node_modules/discord.js/src/client/websocket/WebSocketManager.js:135:26)
    at Client.login (/home/inaba/Programming/Lewd/packages/discord/node_modules/discord.js/src/client/Client.js:221:21) {
  [Symbol(code)]: 'TOKEN_INVALID'
}
--------------------------
Error [TOKEN_INVALID]: An invalid token was provided.
    at WebSocketManager.connect (/home/inaba/Programming/Lewd/packages/discord/node_modules/discord.js/src/client/websocket/WebSocketManager.js:135:26)
    at Client.login (/home/inaba/Programming/Lewd/packages/discord/node_modules/discord.js/src/client/Client.js:221:21)
--------------------------
Promise {
  <rejected> Error [TOKEN_INVALID]: An invalid token was provided.
      at WebSocketManager.connect (/home/inaba/Programming/Lewd/packages/discord/node_modules/discord.js/src/client/websocket/WebSocketManager.js:135:26)
      at Client.login (/home/inaba/Programming/Lewd/packages/discord/node_modules/discord.js/src/client/Client.js:221:21) {
    [Symbol(code)]: 'TOKEN_INVALID'
  }
}
</discord.js>
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! lewd-discord@1.0.0 start: `node ./dist/index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the lewd-discord@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/inaba/.npm/_logs/2020-08-02T08_42_34_847Z-debug.log
inaba@japan ~/Programming/Lewd/packages/discord (master*) $ echo $?      
1
inabahare commented 3 years ago

Also TIL when node crashes because of an exception the return code is 0 :v