Skip to content

Latest commit

 

History

History
64 lines (42 loc) · 2.51 KB

README.md

File metadata and controls

64 lines (42 loc) · 2.51 KB

@poppinss/exception

Create custom exceptions with error code, status, and the help description.


gh-workflow-image npm-image license-image

Introduction

The @poppinss/exception package provides with a base Exception class that can be used to create custom errors with support for defining the error status, error code, and help description.

import { Exception } from '@poppinss/exception'

class ResourceNotFound extends Exception {
  static code = 'E_RESOURCE_NOT_FOUND'
  static status = 404
  static message = 'Unable to find resource'
}

throw new ResourceNotFound()

Anonymous error classes

You can also create an anonymous exception class using the createError method. The return value is a class constructor that accepts an array of values to use for message interpolation.

The interpolation of error message is performed using the util.format method.

import { createError } from '@poppinss/exception'

const E_RESOURCE_NOT_FOUND = createError<[number]>(
  'Unable to find resource with id %d',
  'E_RESOURCE_NOT_FOUND'
)

const id = 1
throw new E_RESOURCE_NOT_FOUND([id])

Contributing

One of the primary goals of poppinss is to have a vibrant community of users and contributors who believes in the principles of the framework.

We encourage you to read the contribution guide before contributing to the framework.

Code of Conduct

In order to ensure that the poppinss community is welcoming to all, please review and abide by the Code of Conduct.

License

Poppinss exception is open-sourced software licensed under the MIT license.