41

megasniff: Debugging promise-chains for humans

 5 years ago
source link: https://www.tuicool.com/articles/hit/ieyiai7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Megasniff

Better debugging for chained functions (promises, streams, etc.). Inspired by supersniff , but with these improvements in mind:

  • Support for custom loggers (like bunyan , winston , or other services)
  • No forced tag
  • Using error levels for errors

Things to do:

  • Custom configureable log levels
  • Decorator functions (e.g. error => error.message )

Motivation

Can be found here . It shall be a readable replacement for:

getData()
  .then(data => {
    console.info(data)
    return data
  })
  .then(transformData)

Installation

npm install megasniff

Examples

const megasniff = require('megasniff')

const reveersedList = new Promise(resolve => resolve([3, 1, 2]))
  .then(megasniff)
  .then(list => list.sort())
  .then(megasniff)
  .then(list => list.reverse())
  .then(megasniff)

This yields in the console:

[ 3, 1, 2 ]
[ 1, 2, 3 ]
[ 3, 2, 1 ]
const megasniff = require('megasniff')

new Promise(resolve => resolve('Good'))
  .then(megasniff.config({ suffix: `Value at ${new Date()}:` }))

This yields on stdout:

Good Value at Wed Oct 17 2018 15:47:26 GMT+0200 (Central European Summer Time)
const megasniff = require('megasniff')
const logger = console

new Promise((resolve, reject) => reject(new Error('Bad')))
  .catch(megasniff.config({ suffix: '.. We are doomed' }))
  .catch(error => error)

This yields on stderr:

Error: Bad ... We are doomed

API

Default Usage:

const megasniff = require('megasniff')

const sortedList = new Promise(resolve => resolve([3, 1, 2]))
  .then(megasniff)
  .then(list => list.sort())
  .then(megasniff)

megasniff is a function that logs and returns the passed value. The value is logged either to stdout or sterr depending on the passed value.

Configuration:

const megasniff = require('megasniff')
const logger = console
new Promise(resolve => resolve('Good Software'))
  .then(megasniff.config({ prefix: `[megasniff]`, logger }))

megasniff.config takes an object as argument. Megasniff can be configured with these optional parameters:

key value log External logger logger Other key for external logger prefix Will be prefixed the logged value suffix Will be suffixed the logged value

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK