ciplogic.com
Live with grace. Write superb software.

A very used paradigm when it come with dealing with errors is the try/catch construct.

In JavaScript you can throw anything as an exception, and in your catch statement you will receive that object instance that you threw. This is pretty cool, because when you receive the catched object, it might contain valuable information on what actually went wrong. In most cases though people just pass a string with the error message.

I've seen advanced programmers actually throwing the message itself as the thrown object, in code that I will simplify as this:

1
2
3
4
5
6
7
try {
    console.log("computer reports heating at engine 1.");
    throw "oh man, catastrophic failure at engine 1.";
} catch (e) {
    console.log("last words were:", e);
    console.log('the time backtrack saving device took note of the missing ship.');
}

Output:

1
2
3
computer reports heating at engine 1.
last words were: oh man, catastrophic failure at engine 1.
the time backtrack saving device took note of the missing ship.

Unfortunately this is a problem, since we got the error message, and it looks good in our 7 lines of code. In reality if our program was a bit bigger, our message was now meaningless, since we would need to find from where the thing was thrown. If the code that can cause the catastrophic failure is called from multiple sites, this only adds to the complexity.

This is why you always use the Exception object. The Exception object is part of standard JavaScript, and in all browsers, Chrome, Firefox, and IE from version 10 onwards (no one uses IE9 on their space ship anyway) has an extra property, named stack, that gives you the stack trace:

1
2
3
4
5
6
7
8
try {
    console.log("computer reports heating at engine 1.");
    throw new Error("oh man, catastrophic failure at engine 1.");
} catch (e) {
    console.log(e.stack);
    console.error("the pilot warning message was:", e);
    console.log('the time backtrack saving device went back in time and send a bot to fix the engine.');
}

This gives the output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
computer reports heating at engine 1.
Error: oh man, catastrophic failure at engine 1.
   at window.onload (http://fiddle.jshell.net/_display/:26:5)
the pilot warning message was: Error: oh man, catastrophic failure at engine 1.
   "the pilot warning message was:"
   {
      [functions]: ,
      description: "oh man, catastrophic failure at engine 1.",
      message: "oh man, catastrophic failure at engine 1.",
      name: "Error",
      stack: "Error: oh man, catastrophic failure at engine 1.
   at window.onload (http://fiddle.jshell.net/_display/:26:5)"
   }
   
the time backtrack saving device went back in time and send a bot to fix the engine.

So please always use Exception objects and avoid space ship tragedies.

Do you remember that there was a time in Windows when all the UIs used to have the three magic buttons: 'Ok', 'Apply' and 'Cancel'?

  • Ok would apply the settings, and close the dialog,
  • Apply would apply the settings, and leave the dialog open and
  • Cancel would close the dialog.

My brother used to tell me, man, it's easy to implement this, since 'Ok' becomes only:

1
2
3
4
    public void onOkClicked() {
    	this.onApplyClicked();
        this.onCancelClicked();
    }

And here it is (almost) the same reminiscence in Eclipse (org.eclipse.jface.preference.PreferencePage).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    /**
     * Performs special processing when this page's Apply button has been pressed.
     * <p>
     * This is a framework hook method for sublcasses to do special things when
     * the Apply button has been pressed.
     * The default implementation of this framework method simply calls
     * <code>performOk</code> to simulate the pressing of the page's OK button.
     * </p>
     
     * @see #performOk
     */
    protected void performApply() {
        performOk();
    }

Oh man, how times have changed... oh wait.

PS. If you want to apply the changes, and keep the dialog open, because you want your users to be able to preview the changes, just make those buttons: 'Apply' and 'Close'.

So here I was, minding my own business, and trying to write some JavaScript documentation, when IDEA tells me in their very thin documentation, that they support JsDoc.

Good, let's see it:

Sourceforge? Was it written by dinosaurs? I remember dinosaurs used to use Sourceforge and put their stuff there. T-Rexes typing with their tiny hands, were the main committers using it, but since poor T-Rexes went away, no one replaced them. Come back T-Rex. We miss you.

Fine let's see the Rhino-based JSDoc.

Notice the vibrant community of 2 committers, and yet starred by 841.

And then finally:

Wow!!

 

Let those numbers sink in: 51 contributors, last commit 17 hours ago, 2593 people starred it.

 

Wow!

You can literally feel the history of this project, and the fantastic boom that had it now, which tell you in turn what happened with JavaScript since 2010 till now.

 

And still has Rhino support in case you missed it from v2. Completely unrelated quote:

Let it go! Let it go!

Elsa (Frozen)

I'm not sure of the context where she said these. Might have been unrelated to JavaScript, and in relation to some past history and ice and snow of hers.

I see that some people try to access my host, even if they are not me.

Crazy, right?

Here is a small statistic in less than a year, on how many attackers fail2ban managed to ban, when trying to bruteforce this website via ssh:

1
2
[root@ciplogic ~]# iptables -L -n | grep REJECT | wc -l
1633

Awesome!

So if you don't have it yet, and run some linux, definitelly install it.

If you use CentOS just do:

1
[root@ciplogic ~]# yum install fail2ban

Happy admining.

WHY!? How?!, I don't even...

Disqus Comments

comments powered by Disqus

Germanium

The one to rule them all. The browsers that is.

SharpKnight

SharpKnight is an Android chess game.

MagicGroup

MagicGroup is an eclipse plugin.