Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
errors-utils
Advanced tools
Custom errors utilities
npm install errors-utils
import { assertAs, createNamespaceError } from 'errors-utils'
import { name, version } from '../package.json' // use metadata from package.json
const LibError = createNamespaceError('LIB', { package: name, version })
const input = 'any'
try {
assertAs(typeof input === 'string', LibError, 120, 'Input must be string')
assertAs(input === 'foo', LibError, 123, 'Input must be foo')
} catch (error) {
console.log(error.toString()) // '[LIB123] Input must be foo'
throw new LibError(10, 'Input validation failed', error) // Wrap thrown error
}
import { StackError, assertAs, createNamespaceError } from 'errors-utils'
function createAssert(ErrorClass: typeof StackError) {
// Our assert function will use the provided Error class and default message
function assert(condition: boolean, code: string | number, message = 'Assertion failed') {
return assertAs(condition, ErrorClass, code, message)
}
assert.equal = (a, b, code = 11, msg = `${a} must be equal to ${b}`) => {
return assert(a === b, code, msg)
}
assert.notEqual = (a, b, code = 12, msg = `${a} must not be equal to ${b}`) => {
return assert(a !== b, code, msg)
}
// ...
return assert
}
const LibError = createNamespaceError('LIB')
const assert = createAssert(LibError)
assert.equal(a, b)
import { assertAs, createNamespaceError } from 'errors-utils'
// ProtocolError is used for clients interactions
class ProtocolError extends createNamespaceError('PTL') {
toAPI(response) { ... }
}
const PROTOCOL_VERSION = 2
function assertProtocolVersion(version: number) {
return assertAs(version === PROTOCOL_VERSION, ProtocolError, 1, `Invalid protocol version: expected ${PROTOCOL_VERSION}, got ${version}`)
}
function handleAPICall(request, response) {
try {
assertProtocolVersion(request.body.version)
} catch (error) {
if (error instanceof ProtocolError) {
return error.toAPI(response)
}
}
}
// InternalError is used for platform interactions
class InternalError extends createNamespaceError('INT') {
// Attach logger to instance
logger: Logger = myLogger
log(level = 'critical') {
this.logger.log({ level, code: this.code, message: this.message })
}
}
function assertValidService(service) {
return assertAs(service instanceof Service, InternalError, 123, 'Invalid service provided')
}
function checkConfig(config) {
try {
assertValidService(config.myService)
...
} catch (err) {
if (error instanceof InternalError) {
error.log()
}
}
}
type StackErrorJSON = {
code: string
message: string
metadata: Record<string, unknown>
name: string
stack: Array<StackErrorJSON>
}
Extends the built-in Error
class
Casts an Error
to a StackError
, using the given code
. Calling this function with an instance of StackError
will return the input unchanged.
Arguments
error: Error
code?: string = 'SE0'
Returns StackError
Arguments
json: StackErrorJSON
Returns StackError
Arguments
code: string
message: string
wrapError?: Error
Returns string
Returns string
Returns Array<StackError>
based on the parentError
provided in constructor
Returns Record<string, unknown>
Returns Array<StackError>
of all the errors in the stack
Serializes the error to JSON. By default the errorStack
is included on a single level, setting the withStack
argument to false
will serialize the error only, discarding the stack.
Arguments
withStack: boolean = true
Returns StackErrorJSON
Asserts the given condition
is true or throws an Error with the given message
.
Arguments
condition: boolean
message?: string = 'Assertion error'
Asserts the given condition
is true or throws an Error using the given ErrorClass
and associated arguments.
Arguments
condition: boolean
ErrorClass: typeof StackError
ErrorClass
argumentsFactory for an Error class extending StackError
with a given namespace
and optional metadata
.
Arguments
namespace: string
metadata?: Record<string, unknown>
Returns class NamespaceError extends StackError
The NamespaceError
class constructor uses the following arguments:
code: string | number
message: string
wrapError?: Error
Apache-2.0 OR MIT
FAQs
Custom errors utilities
The npm package errors-utils receives a total of 128 weekly downloads. As such, errors-utils popularity was classified as not popular.
We found that errors-utils 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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.