Giter VIP home page Giter VIP logo

ember-exex's Introduction

Ember-exex: Exceptional Exceptions for ambitious applications

  _____           _                                       
 | ____|_ __ ___ | |__   ___ _ __       _____  _______  __
 |  _| | '_ ` _ \| '_ \ / _ \ '__|____ / _ \ \/ / _ \ \/ /
 | |___| | | | | | |_) |  __/ | |_____|  __/>  <  __/>  < 
 |_____|_| |_| |_|_.__/ \___|_|        \___/_/\_\___/_/\_\
                                                               

Ember Exceptional Exceptions: Customizable error classes and error re-throwing with original error included

Build Status Ember Observer Score npm version

Why ember exex

When building advanced javascript application full featured error handling is required, but unfortunately it is not provided in javascript out of the box. Taste flavor of Java like exceptions in javascript:

  • Custom error classes
  • Re-throwing of an error with additional context and original error
  • Additonal tooling

Console example

alt tag

Compatiblity

Build Status

Install

ember install ember-exex

Multiple error classes with inheritance

import {defineError} from 'ember-exex/error';

const ApplicationError = defineError({
    name: 'ApplicationError', 
    message: 'General application error'
});

const ServiceError = defineError({
    name: 'ServiceError', 
    message: 'Service error', 
    extends: ApplicationError
});

const UserInterfaceError = defineError({
    name: 'UserInterfaceError', 
    message: 'Service error', 
    extends: ApplicationError
});

try {
    throw new UserInterfaceError();
} catch (e) {

    console.log(e instanceof UserInterfaceError); // true
    console.log(e instanceof ApplicationError); // true
    console.log(e instanceof Error); // true
    console.log(e instanceof ServiceError); // false
    
    if (e instanceof UserInterfaceError) {
        resolveUserInterfaceError(e);
    } else if (e instanceof ServiceError) {
        resolveServiceError(e);
    } else if (e instanceof ApplicationError) {
        resolveGenericApplicationError(e);
    } else if (e instanceof Error) {
        resolveGenericError(e);
    }
}

Re-throwing error with wrapped catched error

import {defineError} from 'ember-exex/error';

const DatabaseError = defineError({
    name: 'DatabaseError', 
    message: 'Database error', 
    extends: ApplicationError
});
      
const UserInterfaceError = defineError({
    name: 'UserInterfaceError', 
    message: 'Service error', 
    extends: ApplicationError
});

try {
    throw new DatabaseError('Database IO error')
} catch (e) {
    throw new UserInterfaceError('Cannot render user interface')
        .withPreviousError(e);
}

Wrapped error is included as string in error.stack and stored as property on wrapping error error.previous

Parametrized error messages

import {defineError} from 'ember-exex/error';

const DatabaseError = defineError({
    name: 'DatabaseError', 
    message: "Database IO error at table '{table}' in '{db}'"
});

try {
    throw new DatabaseError({params: {db: 'mydb', table: 'posts'}});
} catch (e) {
    console.log(e.message); // Database IO error at 'posts' in 'mydb'
}

Extending errors

import {defineError} from 'ember-exex/error';

const ServiceError = defineError({
     name: 'ServiceError', 
     resolve: function() {
         GlobalExceptionManager.log(this);
     }
});

try {
    throw new ServiceError();
} catch (e) {
    if (e instanceof ServiceError) {
        e.resolve();
    } else {
        // do something else
    }
}

ember-exex's People

Contributors

janmisek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ember-exex's Issues

Q: add a property

Hi,

I'm trying to add an option to some Errors but looking at your code the options are not stored inside the instance.

Also, the 'ember way' of using reopen does not work as these errors are not an ember object.

MaintenanceError.reopen({
  useRouter: true, /* would navigate away from the UI where the issue happened */
  useNotification: true, /* use a notification service */
});

So my question is a JS one. How would I add a property to an error definition?

EmberError thrown usually throw during assertion does not contain standard stack trace (at least at chrome)

Error message in stacktrace is very useful during debugging.

# console.log(new Error('message').stack);

route.js:7 Error: message
    at Class.model (route.js:7)
    at Class.deserialize (ember.debug.js:27151)
    at applyHook (ember.debug.js:56576)
    at C.runSharedModelHook (ember.debug.js:57193)
    at C.getModel (ember.debug.js:57392)
    at ember.debug.js:56430
    at tryCatch (ember.debug.js:59057)
    at invokeCallback (ember.debug.js:59072)
    at publish (ember.debug.js:59040)
    at ember.debug.js:38885

VS

# console.log(new Ember.Error('message').stack);

Error
    at Class.model (route.js:6)
    at Class.deserialize (ember.debug.js:27151)
    at applyHook (ember.debug.js:56576)
    at C.runSharedModelHook (ember.debug.js:57193)
    at C.getModel (ember.debug.js:57392)
    at ember.debug.js:56430
    at tryCatch (ember.debug.js:59057)
    at invokeCallback (ember.debug.js:59072)
    at publish (ember.debug.js:59040)
    at ember.debug.js:38885

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.