single-use-file
v1.0.2
Published
A simple library which writes a file, and then deletes it when read.
Downloads
5
Readme
single-use-file
A simple library which writes a file, and then deletes it when read.
Why?
When your node application gets an uncaught exception, you should log it and shut down. The idea is to do something safe since your app is crashing and additional errors might cause it to hang or obscure the root cause of the crash. One safe thing is to write the exception to a file and then crash. When your app starts back up and connects to everything, it can then read the exception from that file and report the error properly.
Installation:
npm i single-use-file --save
Basic Example
'use strict'
const singleUseFile = require('single-use-file')
process.on('uncaughtException', (err) => {
console.error(`uncaughtException: ${err}`)
singleUseFile.write(err)
.then(() => process.exit(1))
.catch((writeErr) => {
console.error(`Could not write the crash file, error: ${writeErr}`)
process.exit(1)
})
})
function startup() {
singleUseFile.read()
.then((contents) => {
if (contents) {
console.log(`Crash file found: ${JSON.stringify(contents)}`)
}
})
.catch((err) => console.error(`Could not read crash file, error: ${err}`))
}
startup()
This example is available here. Run it twice, once to write the crash file and next to read it.