1.0.2 • Published 5 years ago

single-use-file v1.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

single-use-file

A simple library which writes a file, and then deletes it when read.

travis npm standard

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.

1.0.2

5 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago