Coder Social home page Coder Social logo

dipakchavda2912 / custom-error-test Goto Github PK

View Code? Open in Web Editor NEW

This project forked from onury/custom-error-test

0.0 1.0 0.0 164 KB

Compare and test various custom error implementations.

Home Page: https://onury.io/custom-error-test

License: MIT License

JavaScript 75.43% HTML 24.57%

custom-error-test's Introduction

Custom Errors in JavaScript

Custom errors are useful when you want to track code with context-specific information. There are several ways to define a custom error in JavaScript. But since inheritance is not a first-citizen feature in JS (until ES2015); there's also so much discussion on how to implement a proper custom error.

See this SO Question, this and this answers, that leads to this discussion (with @mbrowne) and finally, to this module you're viewing.

This module tests various implementations for custom errors, widely used by developers; such as methods making use of Error.call(), Object.create(), new Error(), Object.setPrototypeOf(), extends (ES2015) and util.inherits (Node). See /errors directory for included implementations.

What we expect from a custom error implementation is:

  • e.constructor.name MUST be set to custom error name.
  • E.prototype MUST inherit from Error.prototype.
  • e.message MUST be set.
  • e MUST be an instance of Error.
  • e MUST be an instance of CustomError.
  • e.stack MUST be set and should have line-tracable info.
  • e.tostring() MUST return error name and/or message.
  • ({}).toString.call(e) SHOULD output "error".
  • and some more...

Test

Run npm start which will output test results to console for the current Node.js environment. And will also generate an HTML file, which will display results for the used browser.

Better, you can directly view this page for testing your current browser.

Screenshot Capture of a test on Chrome 55 browser.

CustomError: The One

So; with some research, discussions and these tests I think this is the one closest to ES2015's extends Error. If you think this is a bit over-kill, this should be quite enough.

License

MIT.

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.