0.1.0 • Published 3 years ago

ts-condition-handler v0.1.0

Weekly downloads
17
License
MIT
Repository
-
Last release
3 years ago

ts-condition-handler

Execute the corresponding handler by customized definition in a declarative way.

Example

import { conditionHandler } from 'ts-condition-handler'

function main() {
  try {
    // do something that may throw an error in a specific type
    doSomething()
  } catch (err) {
    conditionHandler(error)
      .on(CustomerNotFound, handleCustomerNotFound)
      .on(CustomerCreditLimitExceeded, handleCustomerCreditLimitExceeded)
      .fallback(handleUnknownError)
  }
}

function doSomething() {
  throw new CustomerNotFound()
}

Test

yarn test