Giter VIP home page Giter VIP logo

backbone-errors's Introduction

Backbone Errors

Advanced Error Handling for Backbone using Radio.

Travis Status Code Climate Score Coverage Dependency Status

Try, throw, and catch namespaced errors using Backbone.Radio with built-in promise handling.

Usage

Namespaced Errors

Backbone Errors introduces the concept of namespacing errors. Throwing an error with a name will attempt to be caught by a handler with the same name, otherwise it will use the "default" handler.

Errors.catch('default', (name) => {
  console.log(`Nobody caught the ${name}.`);
});

Errors.throw('baseball');
// >> Nobody caught the baseball.

Errors.catch('baseball', err => {
  console.log(`I caught the baseball.`);
});

Errors.try('baseball', () => {
  throw new Error();
});
// >> I caught the baseball.

Catching Errors

In order to setup a handler to catch an error use the Errors.catch method.

Errors.catch('football', err => {
  console.log('Caught it!');
});

Throwing Errors

There are two primary ways of throwing an error. The first is to call the Errors.throw method.

Errors.throw('football');
// >> Caught it!

The second is to use the Errors.try method which will execute a callback, and if that callback throws it will forward the error to the proper handler.

Errors.catch('hockey', err => {
  console.log(`Off go the ${err.message}`)
});

let isFighting = false;

function callback() {
  if (isFighting) {
    throw new Error('gloves');
  } else {
    return 'Start a fight!';
  }
}

Errors.try('hockey', callback);
// 'Start a fight!'

isFighting = true;

Errors.try('hockey', callback);
// >> Off go the gloves!

Removing Error Handlers

If you would like to stop catching errors. You use the stopCatching method.

Errors.catch('basketball', () => {
  console.log('Caught it.');
})

Errors.throw('basketball');
// >> Caught it.

Errors.stopCatching('basketball');

Errors.throw('basketball');
// >> Nobody caught the basketball.

Contibuting

Getting Started

Fork and clone this repo.

git clone [email protected]:thejameskyle/backbone-errors.git && cd backbone-errors

Make sure Node.js and npm are installed.

npm install

Running Tests

npm test

===

© 2014 James Kyle. Distributed under ISC license.

backbone-errors's People

Contributors

jamiebuilds avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.