Socket
Socket
Sign inDemoInstall

baseerr

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baseerr

A solid BaseError class that supports custom properties and wrapping errors


Version published
Weekly downloads
220
increased by12.24%
Maintainers
1
Weekly downloads
 
Created
Source

BaseErr

A solid BaseError class that supports custom properties and wrapping errors

Installation

npm i --save baseerr

Usage

Create a custom error class
import BaseError from 'baseerr'

class CustomError extends BaseError {}

const err = new CustomError('boom')
console.log(err.name // 'CustomError'
console.log(err.stack)
// CustomError: boom
//     anonymous (filename:5:17)
Wrap an error with a custom error class
import BaseError from 'baseerr'

class CustomError extends BaseError {}

try {
  throw new Error('pow')
} catch (_err) {
  const err = CustomError.wrap('caught error', _err)
  console.log(err.stack)
  // CustomError: caught error
  //     anonymous (filename:8:17)
  // ----
  // Error: pow
  //     anonymous (filename:6:9)
}
Wrap an error with a custom error class in promise chain
import BaseError from 'baseerr'

class CustomError extends BaseError {}

Promise.reject(new Error('pow')).catch((err) =>
  CustomError.wrapAndThrow('caught error', err),
)
// rejects with:
// CustomError: caught error
//     anonymous (filename:6:3)
// ----
// Error: pow
//     anonymous (filename:5:16)
Create custom error instance with data properties
import BaseError from 'baseerr'

class CustomError extends BaseError {}

const err = new CustomError('boom', { foo: 10, bar: 20 })
console.log(err.foo) // 10
console.log(err.bar) // 20
console.log(err.stack)
// CustomError: boom
//     anonymous (filename:5:17)
// {
//   "foo": 10,
//   "bar": 20
// }

// TypeScripters use BaseError.create if you want to access extended properties with proper typing:
const err = CustomError.create('boom', { foo: 10, bar: 20 })
console.log(err.foo) // 10
console.log(err.bar) // 20
Create custom api client with robust error handling
import BaseError from 'baseerr'

class FetchError extends BaseError {}
class ResponseError extends BaseError {}
class ApiError extends BaseError {}

class ApiClient {
  getData() {
    const url = 'https://localhost:3000'
    try {
      const res = await Promise.race([
        timeout(2000).then(() => {
          throw new TimeoutError('request timed out', { statusCode: 504, url })
        }),
        fetch(url).catch(
          FetchError.wrapAndThrow('network error', { statusCode: 503, url }),
        ),
      ])
      if (res.statusCode !== 200) {
        throw new ResponseError('status: ${res.statusCode}', {
          statusCode: res.statusCode,
          url,
        })
      }
      return await res.json()
    } catch (err) {
      throw ApiError.wrap(err, { url, statusCode: err.statusCode || 500 })
      // ApiError: boom
      //     anonymous (filename:row:col)
      // {
      //   "url": 'https://localhost:3000',
      //   "statusCode": 504
      // }
      // ----
      // TimedoutError: request timed out
      //     anonymous (filename:row:col)
      // {
      //   "url": 'https://localhost:3000',
      //   "statusCode": 504
      // }
    }
  }
}
Checkout the tests for more examples..

License

MIT

Keywords

FAQs

Package last updated on 17 Apr 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc