GithubHelp home page GithubHelp logo

clean-stack's Introduction

clean-stack

Clean up error stack traces

Removes the mostly unhelpful internal Node.js entries.

Also works in Electron.

Install

npm install clean-stack

Usage

import cleanStack from 'clean-stack';

const error = new Error('Missing unicorn');

console.log(error.stack);
/*
Error: Missing unicorn
    at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
*/

console.log(cleanStack(error.stack));
/*
Error: Missing unicorn
    at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
*/

API

cleanStack(stack, options?)

Returns the cleaned stack or undefined if the given stack is undefined.

stack

Type: string | undefined

The stack property of an Error.

options

Type: object

pretty

Type: boolean
Default: false

Prettify the file paths in the stack:

/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15~/dev/clean-stack/unicorn.js:2:15

basePath

Type: string?

Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones. It will also transform absolute file URLs into relative paths.

Example with '/Users/sindresorhus/dev/clean-stack' as basePath:

/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15unicorn.js:2:15

pathFilter

Type: (path: string) => boolean

Remove the stack lines where the given function returns false. The function receives the path part of the stack line.

import cleanStack from 'clean-stack';

const error = new Error('Missing unicorn');

console.log(cleanStack(error.stack));
// Error: Missing unicorn
//     at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
//     at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/omit-me.js:1:16)

const pathFilter = path => !/omit-me/.test(path);

console.log(cleanStack(error.stack, {pathFilter}));
// Error: Missing unicorn
//     at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)

Related

clean-stack's People

Contributors

bendingbender avatar broguinn avatar coreyfarrell avatar fengkx avatar hyrious avatar jblew avatar levminer avatar mastilver avatar mooyoul avatar neezer avatar richienb avatar silverwind avatar sindresorhus avatar zavr-1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clean-stack's Issues

Normalize file URLs to paths?

Node 16 uses file:// URLs in stack traces, I wonder whether this module should attempt to normalize these to file system paths using fileURLToPath to shorten the output and keep it consistent with previous node versions.

Error: Some error message
    at myFunction (file:///path/to/file.js:356:45)
    at Array.map (<anonymous>)
    at myOtherFunction (file:///path/to/file.js:386:45)
    at runMicrotasks (<anonymous>)

Not directly related but runMicrotasks seems also something this module could hide.

Support relative paths

Currently, the pretty option can be specified to replace the path to the home directory with ~. I think it'd be nice to have an option to show relative paths, so they're even shorter. As-is, the leading part of every path is often identical.

This additional check in the step where the path is modified seems to do it, assuming options.relativePath is true.

		.map(line => {
			if (options.pretty) {
				return line.replace(extractPathRegex, (m, p1) => m.replace(p1, p1.replace(homeDir, '~')));
			}

			if (options.relativePath) {
				return line.replace(extractPathRegex, (m, p1) => m.replace(p1, path.relative(process.cwd(), p1)));
			}

			return line;
		})

Would you accept a PR to add this option? Any preferences for the implementation?

Commonjs module

Hello)
Great work with this module, I like it
I saw ECMAScript module for the last version, but can we have this like commonjs module?

basePath option

User could pass in a option basePath like /Users/sindresorhus/dev/clean-stack which would use path.relative to replace absolute paths like /Users/sindresorhus/dev/clean-stack/unicorn.js with relative ones in the stack trace:

> path.relative('/Users/sindresorhus/dev/clean-stack', '/Users/sindresorhus/dev/clean-stack/unicorn.js')
'unicorn.js'

Some kind of path shim may be required for browser compatibilty, thought I guess shimming can be delegated to the user too seeing that this module already depends on os.

Cannot use import statement outside of a module

Here is the result of one of my tests after installing the module:

 FAIL  tests/productsForYou.test.ts
   Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
      If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
      To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
      If you need a custom transformation specify a "transform" option in your config.
      If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/will/toptal/mylyka/node-core-libs/node_modules/clean-stack/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import os from 'os';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import cleanStack from 'clean-stack';

Detect and remove `node:*` stack frames

I have a class MySocket that extends net.Socket and it shows stack frames like below which I think should be filtered out by this module. I think this type of stack frame may be an addition on recent versions of Node.js, I do observe it on Node 16.

    at MySocket.emit (node:events:365:28)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.