You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Readme.md 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # stack-trace
  2. Get v8 stack traces as an array of CallSite objects.
  3. ## Install
  4. ``` bash
  5. npm install stack-trace
  6. ```
  7. ## Usage
  8. The stack-trace module makes it easy for you to capture the current stack:
  9. ``` javascript
  10. var stackTrace = require('stack-trace');
  11. var trace = stackTrace.get();
  12. require('assert').strictEqual(trace[0].getFileName(), __filename);
  13. ```
  14. However, sometimes you have already popped the stack you are interested in,
  15. and all you have left is an `Error` object. This module can help:
  16. ``` javascript
  17. var stackTrace = require('stack-trace');
  18. var err = new Error('something went wrong');
  19. var trace = stackTrace.parse(err);
  20. require('assert').strictEqual(trace[0].getFileName(), __filename);
  21. ```
  22. Please note that parsing the `Error#stack` property is not perfect, only
  23. certain properties can be retrieved with it as noted in the API docs below.
  24. ## Long stack traces
  25. stack-trace works great with [long-stack-traces][], when parsing an `err.stack`
  26. that has crossed the event loop boundary, a `CallSite` object returning
  27. `'----------------------------------------'` for `getFileName()` is created.
  28. All other methods of the event loop boundary call site return `null`.
  29. [long-stack-traces]: https://github.com/tlrobinson/long-stack-traces
  30. ## API
  31. ### stackTrace.get([belowFn])
  32. Returns an array of `CallSite` objects, where element `0` is the current call
  33. site.
  34. When passing a function on the current stack as the `belowFn` parameter, the
  35. returned array will only include `CallSite` objects below this function.
  36. ### stackTrace.parse(err)
  37. Parses the `err.stack` property of an `Error` object into an array compatible
  38. with those returned by `stackTrace.get()`. However, only the following methods
  39. are implemented on the returned `CallSite` objects.
  40. * getTypeName
  41. * getFunctionName
  42. * getMethodName
  43. * getFileName
  44. * getLineNumber
  45. * getColumnNumber
  46. * isNative
  47. Note: Except `getFunctionName()`, all of the above methods return exactly the
  48. same values as you would get from `stackTrace.get()`. `getFunctionName()`
  49. is sometimes a little different, but still useful.
  50. ### CallSite
  51. The official v8 CallSite object API can be found [here][v8stackapi]. A quick
  52. excerpt:
  53. > A CallSite object defines the following methods:
  54. >
  55. > * **getThis**: returns the value of this
  56. > * **getTypeName**: returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property.
  57. > * **getFunction**: returns the current function
  58. > * **getFunctionName**: returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context.
  59. > * **getMethodName**: returns the name of the property of this or one of its prototypes that holds the current function
  60. > * **getFileName**: if this function was defined in a script returns the name of the script
  61. > * **getLineNumber**: if this function was defined in a script returns the current line number
  62. > * **getColumnNumber**: if this function was defined in a script returns the current column number
  63. > * **getEvalOrigin**: if this function was created using a call to eval returns a CallSite object representing the location where eval was called
  64. > * **isToplevel**: is this a toplevel invocation, that is, is this the global object?
  65. > * **isEval**: does this call take place in code defined by a call to eval?
  66. > * **isNative**: is this call in native V8 code?
  67. > * **isConstructor**: is this a constructor call?
  68. [v8stackapi]: http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
  69. ## License
  70. stack-trace is licensed under the MIT license.