Wednesday 20 November 2013

Why Throw When You Can Just Push?

I had previously launched a feature to .push your errors to Errorception if you liked. It also required you to throw your errors immediately after the push.

Turns out, many people don't like to throw their errors. There could be several reasons for not throwing your errors – for example, if you want to handle it gracefully, but still want it logged. Also, some frameworks such an Angular and Ember provide you with Error objects, but throwing them might not be what you want to do.

Now with Errorception, you don't need to throw error objects anymore. Simply .push them, and you are done!

I must hasten to add that I would consider this usage advanced. A throw, despite all its weirdness, is the best way to halt a problem immediately. If there's an error that you haven't anticipated, chances are you will want to stop your program's execution. If you are going to deal with errors properly, you don't need to throw them, but then you wouldn't have an error in the first place. This makes the feature only useful for a small set of cases. If you are not throwing your error, I hope you know what you are doing.

If your code was already throwing errors after pushing them, you won't have to change a thing. Don't worry – you won't get duplicate errors. In fact, this behaviour (push, with or without throw, without duplicates) will be supported forever, because of the reasons in the paragraph above.

3 comments:

  1. Hi Rakesh!

    This looks great. Am I missing something though?

    I put the following code in my consle

    _errs.push( new Error("Hello errorception"));

    It said it posted it, but it is not appearing in my log.

    obviously I won't be pushing "Hello Errorception" to my log in the real world, but I wanted to see how it looked with just a message like that before I started building something more interesting.

    ReplyDelete
    Replies
    1. Hi Patricia,

      This could be due to several reasons. The most likely cause is that you are calling _errs.push from the browser's console. In that case, the error does get POSTed to the server, but gets rejected on the server since it has no sensible debugging information in it (no line number / column number / script source that can be made sense of).

      If you weren't trying this from the console but from a proper script included on the page, chances are that the error has been disallowed in your settings (Settings > Posting Errors).

      Let me know if this helps. If not, feel free to contact me over mail, and gladly help you debug the problem more. I'm at rakeshpai at errorception.

      Delete
    2. Thanks Rakesh! That is exactly what was happening.

      Delete