Extend Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the extend error javascript workings and policies of this site About Us Learn more about extended error status has been encountered check logs(252) Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
Extended Error Text 'error In Device Matching'
Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Extended Error Has Occurred Importing Security Policy
Join them; it only takes a minute: Sign up What's a good way to extend Error in JavaScript? up vote 198 down vote favorite 70 I want to throw some things in my JS code and I want them to be instanceof Error, but I also want to have them be something else. In Python, typically, one would subclass extended error when mapping a network drive Exception. What's the appropriate thing to do in JS? javascript exception error-handling share|improve this question edited Apr 19 '11 at 1:56 asked Sep 5 '09 at 0:54 Josh Gibson 5,916154860 18 I know it's been awhile, but can you pick an answer? –qodeninja Sep 23 '14 at 18:01 1 Josh, please accept whichever answer solves the problem best. –QPaysTaxes May 4 '15 at 15:01 add a comment| 16 Answers 16 active oldest votes up vote 129 down vote The only standard field Error object has is the message property. (See MDN, or EcmaScript Language Specification, section 15.11) Everything else is platform specific. Mosts environments set the stack property, but fileName and lineNumber are practically useless to be used in inheritance. So, the minimalistic approach is: function MyError(message) { this.name = 'MyError'; this.message = message; this.stack = (new Error()).stack; } MyError.prototype = new Error; // <-- remove this if you do not // want MyError to be instanceof Error You could sniff the stack, unshift unwanted elements from it and extract information
a GitHub account Sign in Create a gist now Instantly share code, notes, and snippets. Star 153 Fork 23 justmoon/custom-error.js forked from subfuzion/error.md Last active Oct 7, 2016 Embed What would you
Extended Error 32
like to do? Embed Embed this gist in your website. Embed Share Copy sharable extended error information revocation status URL for this gist. Share Clone via HTTPS Clone with Git or checkout with SVN using the repository's web address. extended error 183 HTTPS Learn more about clone URLs Download ZIP Code Revisions 7 Stars 153 Forks 23 Creating custom Error classes in Node.js Raw custom-error.js 'use strict'; module.exports = function CustomError(message, extra) { Error.captureStackTrace(this, this.constructor); this.name http://stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript = this.constructor.name; this.message = message; this.extra = extra; }; require('util').inherits(module.exports, Error); Raw error.md It's nice to be able to distinguish error types by classes. But it's a bit tricky to correctly create a custom error class in Node.js, so here is an example. The example also shows how to add an extra parameter called extra that will be stored as a property on the error. Usage var https://gist.github.com/justmoon/15511f92e5216fa2624b CustomError = require('./errors/custom-error'); function doSomethingBad() { throw new CustomError('It went bad!', 42); } Features Name appears once - less editing if you have to create lots of custom error classes Easy to subclass - just change the last line to inherit from another custom error class you created Correct stack trace - no extra stack frames, no double capturing of the stack trace Anti-patterns These are some things that I've seen in other proposed solutions that you should avoid. Error.call(this) - creates another error object (wasting a bunch of time) and doesn't touch this at all Error.captureStackTrace(this, arguments.callee); - works, but arguments.callee is deprecated, so don't use it this.stack = (new Error).stack - this... I don't even... Raw test-error.js // Mini test suite for our custom error var assert = require('assert'); var CustomError = require('./errors/custom-error'); function doSomethingBad() { throw new CustomError('It went bad!', 42); } try { doSomethingBad(); } catch (err) { // The name property should be set to the error's name assert(err.name = 'CustomError'); // The error should be an instance of its class assert(err instanceof CustomError); // The error should be an instance of builtin Error assert(err instanceof Error); // The error should be recognized by Node.js' u
by Alex R. Young on January 30th, 2014. Featured node errors The Art of Error Posted by Alex R. Young on January 30th, 2014. Error was originally a character in the http://dailyjs.com/2014/01/30/exception-error/ hit video game, "Zelda". I like to define a lot of objects that inherit from Error. I find it helps me to track down issues -- post-mortem -- but also to clearly handle expected errors. Sometimes writing error handling code feels like a chore, but it shouldn't be an afterthought. Well-designed and well-tested errors will help you maintain projects, but also help users figure out what to extended error do when things go wrong. When it comes to using Error, I've found two bad practices that should be avoided: new Error is used instead of a subclass. Error is avoided altogether because "exceptions are bad". Let's look at how to avoid these issues and use errors properly. Subclassing Error Subclassing errors is easy with Object.create or util.inherits (in Node). Here's how you do it in Node: var extend error javascript assert = require('assert'); var util = require('util'); function NotFound(message) { Error.call(this); this.message = message; } util.inherits(NotFound, Error); var error = new NotFound('/bitcoin-wallet not found'); assert(error.message); assert(error instanceof NotFound); assert(error instanceof Error); assert.equal(error instanceof RangeError, false); The assertions check that the expected property was set (message), and error is an instance of NotFound, Error, but not RangeError. If you were using this with Express, you could set other properties to make the error more useful. This is great when passing errors to next() in routes. When dealing with errors at the HTTP layer, I like to include a status code: function NotFound(message) { Error.call(this); this.message = message; this.statusCode = 404; } Now you could have error handling middleware that handles errors in a more DRY fashion: app.use(function(err, req, res, next) { console.error(err.stack); if (!err.statusCode || err.statusCode === 500) { emails.error({ err: err, req: req }); } res.send(err.statusCode || 500, err.message); }); This will send the HTTP status code to the browser, if available. It also only emails errors when the statusCode is 500 or not set. I took this from production code that generates emails when unusual things happen, and I don't want to get notified about general errors like 401, 403, an