@svgfwjs/err
v1.0.0
Published
[ [README на русском](README_ru.md) ]
Downloads
2
Readme
JS Error with reduced Stack Trace
V8 JavaScript engine introduced stack trace into Error objects and gave ability to manipulate it with Error.captureStackTrace() static method. This gives opportunity for developing own API and libraries which has functions that throws Errors to cut off parts of the stack trace which points to the API/library and make focus on code that used its functions wrong.
This package of my personal framework provides function that creates and returns an Error instance and reduces the stack trace to function that throws that Error instance. It's part of my personal framework as core component that is used in other packages, however, You may use it in Your code or projects.
Usage
npm i @svgfwjs/errSyntax
err('Error Message.', callerFunction, ErrorType, optionsOrFileName, lineNumber)callerFunctionis the function that throws an error which is used for stack trace reductionErrorTypeis the type of error that was chosen to be thrown.
It should be any of Error objects that exists in JavaScript or it may be own custom Error.
optionsOrFileName- this argument is being passed toErrorconstructor alongside with'Error Message.'. It can be object that hascauseproperty/key or string that is path to the file.lineNumberis the number of the line in the file (which also is being passed toErrorconstructor)
The thing is: file name and line number parameters/arguments are mostly usable in certain web browsers. Node and other alike runtime enviroments doesn't use those parameters/arguments.
All parameters/arguments are optional, just like in Error constructor.
Back-end
import { err } from '@svgfwjs/err'
function yourCoolFunction() {
if ( somethingIncorrect )
throw err('You did it wrong!', yourCoolFunction)
}
yourCoolFunction()
// Will result as following:
// yourCoolFunction()
// ^
//
// Error: You did it wrong!
// at file:///.../yourCoolScript.js:8:1Front-end
<script src="https://svg.moe/js/err.js"></script>
<script>
function yourCoolFunction() {
if ( somethingIncorrect )
throw svgfwjs.err('You did it wrong!', yourCoolFunction)
}
yourCoolFunction()
// Will result as following:
// Uncaught Error: You did it wrong!
// at index.html:13:1
</script>As bundle-able module
import err from '@svgfwjs/err'