Giter Site home page Giter Site logo

catch-decorator's Introduction

Catch Decorator

npm npm bundle size NPM

ECMAScript/TypeScript Catch decorator for classes and functions. Based on @enkot/catch-decorator.

Install

npm install @magna_shogun/catch-decorator

yarn add @magna_shogun/catch-decorator

Usage

catch-decorator works with any ECMAScript/TypeScript class. If you use Babel, babel-plugin-transform-decorators-legacy is needed. If you use TypeScript, enable --experimentalDecorators flag.

Catch

Decorate a function to catch the specific type of error.

class Messenger {
  @Catch(ReferenceError, (err, ctx) => {...})
  getMessages() {
    throw new ReferenceError('ReferenceError here!');
    ...
  }
}

You can stack decorators to catch and handle different types of error.

class Messenger {
  @Catch(TypeError, (err, ctx) => {...})
  @Catch(ReferenceError, (err, ctx) => {...})
  getMessages() {
    if (conditionA) {
      throw new ReferenceError('ReferenceError here!');
      ...
    }
    if (conditionB) {
      throw new TypeError('TypeError here!');
      ...
    }   
  }
}

It also works for async functions.

class Messenger {
  @Catch(ServerError, (err, ctx) => {...})
  async getMessages() {
    return fetch(myRequest).then(response => { // can throw ServerError
      ...
    });
  }
}

CatchAll

You can catch any type of errors using the CatchAll decorator.

class Messenger {
  @CatchAll((err, ctx) => {...})
  getMessages() {
    // Both ReferenceError and TypeError will be catched
    if (conditionA) {
      throw new ReferenceError('ReferenceError here!');
      ...
    }
    if (conditionB) {
      throw new TypeError('TypeError here!');
      ...
    }   
  }
}

Class Decoration

If you want to apply the same decorator to all the functions of a class (excluding its constructor), you can add the decorator to the class.

class Messenger {
  @Catch(ReferenceError, (err, ctx) => {...})
  getMessages() {
    throw new ReferenceError('ReferenceError here!');
    ...
  }
  
  @Catch(ReferenceError, (err, ctx) => {...})
  getPosts() {
    throw new ReferenceError('ReferenceError here!');
    ...
  }
}

This can be re-written as follows.

@Catch(ReferenceError, (err, ctx) => {...})
class Messenger {
  getMessages() {
    throw new ReferenceError('ReferenceError here!');
    ...
  }
  
  getPosts() {
    throw new ReferenceError('ReferenceError here!');
    ...
  }
}

HandlerFunction

The callback for the Catch and CatchAll decorators is of type HandlerFunction.

type HandlerFunction = (error: Error, ctx: any) => void;

error is the caught error or exception.
ctx is the context (this) of the function were the error was thrown.

License

MIT

catch-decorator's People

Contributors

omirobarcelo avatar roerohan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.