
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
@grimen/mybad
Advanced tools
js-mybad
My friendly error base class - for Node/JavaScript.
One in general always needs a application/library specific error base class, but the native errors are very limited in what meta/debugging information they can hold at time they are raised. For better debugging and error reporting/inspection this error base class allows to attach some additonal error context information that can be used to better understand the issue - without having to create custom error formatters, or run debugger.
Install using npm:
$ npm install @grimen/mybad
Install using yarn:
$ yarn add @grimen/mybad
Very basic example:
const mybad = require('@grimen/mybad')
class ToMuchError extends mybad.Error {}
function printMoney(stash) {
try {
if (typeof stash === 'string' && stash.length > 13) {
throw new RangeError(`Too much money to print: ${stash}`)
}
console.log(`PRINT ${stash.join('')}`)
} catch (error) {
throw new ToMuchError(error, {
message: 'Out of money printing ink...',
id: Date.now(), // well, should maybe be unique...
key: 'too_much',
code: 400,
details: {
stash,
},
})
}
}
let amount = ''
const range = (to) => {
return [...Array(to).keys()]
}
for (let dollar of range(42)) {
amount += '$'
try {
printMoney(amount)
} catch (error) {
process.env.ERROR_VERBOSE = false
process.env.ERROR_COLORS = false
console.log()
console.log('========================================================')
console.log(' error.toString({verbose: false})')
console.log('-----------------------------------------------------')
console.log()
console.log(error.toString({verbose: false}))
console.log()
console.log()
console.log('========================================================')
console.log(' error.toString({verbose: true})')
console.log('-----------------------------------------------------')
console.log()
console.log(error.toString({verbose: true}))
console.log()
console.log()
console.log('========================================================')
console.log(' error.toJSON()')
console.log('-----------------------------------------------------')
console.log()
console.log(error.toString({verbose: true}))
console.log()
throw error
}
}
Run this with optional environment variables COLORS
/ ERROR_COLORS
and/or VERBOSE
/ ERROR_VERBOSE
set too truthy or falsy values, so see various error info formatting in terminal. Also (NO_COLOR)[https://no-color.org]
is respected.
Something like this (imagine some colorized formatting):
PRINT $
PRINT $$
PRINT $$$
PRINT $$$$
PRINT $$$$$
PRINT $$$$$$
PRINT $$$$$$$
PRINT $$$$$$$$
PRINT $$$$$$$$$
PRINT $$$$$$$$$$
PRINT $$$$$$$$$$$
PRINT $$$$$$$$$$$$
PRINT $$$$$$$$$$$$$
========================================================
error.toString({verbose: false})
-----------------------------------------------------
Out of money printing ink...
========================================================
error.toString({verbose: true})
-----------------------------------------------------
Out of money printing ink... { stash: '$$$$$$$$$$$$$$' }
========================================================
error.toJSON()
-----------------------------------------------------
{
type: 'ToMuchError',
id: 1618487081556,
code: 400,
key: 'too_much',
message: 'Out of money printing ink...',
details: { stash: '$$$$$$$$$$$$$$' },
stack: [
{
file: '/Users/jonas/Dev/Private/js-mybad/examples/basic.js',
function: 'printMoney',
line: 23,
column: 19,
source: undefined
},
{
file: '/Users/jonas/Dev/Private/js-mybad/examples/basic.js',
function: 'Object.<anonymous>',
line: 57,
column: 9,
source: undefined
},
{
file: 'internal/modules/cjs/loader.js',
function: 'Module._compile',
line: 1063,
column: 30,
source: undefined
},
{
file: 'internal/modules/cjs/loader.js',
function: 'Object.Module._extensions..js',
line: 1092,
column: 10,
source: undefined
},
{
file: 'internal/modules/cjs/loader.js',
function: 'Module.load',
line: 928,
column: 32,
source: undefined
},
{
file: 'internal/modules/cjs/loader.js',
function: 'Function.Module._load',
line: 769,
column: 14,
source: undefined
},
{
file: 'internal/modules/run_main.js',
function: 'Function.executeUserEntryPoint [as runMain]',
line: 72,
column: 12,
source: undefined
},
{
file: '17',
function: 'internal/main/run_main_module.js',
line: 47,
column: undefined,
source: undefined
}
]
}
Clone down source code:
$ make install
Run colorful tests using jest:
$ make test
python-mybad
- "My friendly error base class - for Python"This project was mainly initiated - in lack of solid existing alternatives - to be used at our work at Markable.ai to have common code conventions between various programming environments where Node.js (for I/O heavy operations) is heavily used.
Released under the MIT license.
FAQs
My friendly error base class - for Node/JavaScript.
The npm package @grimen/mybad receives a total of 10 weekly downloads. As such, @grimen/mybad popularity was classified as not popular.
We found that @grimen/mybad demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.