
Security News
Potemkin Understanding in LLMs: New Study Reveals Flaws in AI Benchmarks
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
The 'err-code' npm package is a utility for creating error objects with custom error codes and properties. It simplifies the process of adding additional context to errors, making them more informative and easier to handle.
Creating an error with a custom code
This feature allows you to create an error object with a custom error code. The custom code can be used to identify the error type more easily.
const createError = require('err-code');
const error = createError(new Error('Something went wrong'), 'E_CUSTOM');
console.log(error.code); // 'E_CUSTOM'
Adding custom properties to an error
This feature allows you to add custom properties to the error object, providing additional context and information about the error.
const createError = require('err-code');
const error = createError(new Error('Something went wrong'), 'E_CUSTOM', { detail: 'Additional error details' });
console.log(error.detail); // 'Additional error details'
Handling errors with custom codes
This feature demonstrates how to handle errors based on their custom codes, allowing for more specific error handling logic.
const createError = require('err-code');
try {
throw createError(new Error('Something went wrong'), 'E_CUSTOM');
} catch (error) {
if (error.code === 'E_CUSTOM') {
console.error('Custom error occurred:', error.message);
}
}
The 'create-error' package provides a way to create custom error types with additional properties. It is similar to 'err-code' but focuses more on creating new error types rather than adding codes to existing errors.
The 'custom-error-generator' package allows you to generate custom error classes with additional properties. It is similar to 'err-code' but offers more flexibility in defining custom error classes.
The 'verror' package is used for creating and manipulating chained errors. It provides more advanced features for error handling and is more complex compared to 'err-code'.
Create new error instances with a code and additional properties.
$ npm install err-code
- NPM
$ bower install err-code
- bower
The browser file is named index.umd.js which supports CommonJS, AMD and globals (errCode).
I find myself doing this repeatedly:
var err = new Error('My message');
err.code = 'SOMECODE';
err.detail = 'Additional information about the error';
throw err;
Simple usage.
var errcode = require('err-code');
// fill error with message + code
throw errcode(new Error('My message'), 'ESOMECODE');
// fill error with message + code + props
throw errcode(new Error('My message'), 'ESOMECODE', { detail: 'Additional information about the error' });
// fill error with message + props
throw errcode(new Error('My message'), { detail: 'Additional information about the error' });
// You may also pass a string in the first argument and an error will be automatically created
// for you, though the stack trace will contain err-code in it.
// create error with message + code
throw errcode('My message', 'ESOMECODE');
// create error with message + code + props
throw errcode('My message', 'ESOMECODE', { detail: 'Additional information about the error' });
// create error with message + props
throw errcode('My message', { detail: 'Additional information about the error' });
$ npm test
Released under the MIT License.
FAQs
Create an error with a code
The npm package err-code receives a total of 18,299,358 weekly downloads. As such, err-code popularity was classified as popular.
We found that err-code demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Security News
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.