Uncategorized

Irritate your developers

Nothing motivates a developer more than being irritated by something.

It’s why we’re hackers in the first place. I remember my first real programming experience being using the “Freeze Frame” cartridge on the Commodore 64 so I could change the number of lives I had in a game. The irritation of not being able to finish the game drew me into hacking. Most programmers have similar stories – of how something irritated them enough to start hacking on it.

We can use this to our advantage in our day to day work. One excellent example I remember hearing when I was in the Perl community (I think it was from @chromatic, but I don’t recall exactly) was at the end of each day, write a test that fails based on what you’re working on. You’ll come in next morning and want/need to fix that test.

My most recent examples of irritating myself have been when trying out new external systems that implement webhooks. When I implemented Stripe payments for EmailItIn I had no idea which webhooks I might need to pay attention to. So rather than pour over the documentation for hours until I figured it out, I just set it up to irritate me – every call from Stripe webhooks emails me the entire JSON structure they send. Over time this becomes irritating fairly quickly, and so like any good hacker I start to notice patterns and things I’m interested in and not interested in, and I can setup code to filter out irrelevant webhooks, and write code that deals with things like “subscription_cancelled”.

Another way you can use this to your advantage is to email every single error (or in Node, every call to console.error()) on your site. This might scare a lot of people, but if you’re seeing errors often enough to irritate you, that is something that needs fixed, fast. And if you don’t do this those errors often get lost in your logs (you do keep logs, right?). At Ideal Candidate we email all developers when any of the following conditions occur:

  • A console.error() call – we shim this to provide a stack trace in the email
  • A server side exception which brings the server down
  • A client side exception occurs – this does a POST back to our server which triggers the email

Does this amount of emails scare you? If it does you probably have far too many errors occurring in your application. Give it a try – you might find a lot of issues that you didn’t even know you had.

Here’s our shims for console.error and catching exceptions:

    app.post('/js_error', js_error_middleware, function (req, res) {
        // Client side error occurred
        mailer.send_error(req.session.js_error, req.body.error, function (err) {
            if (err) {
                console.error(err);
                return res.send(500);
            }
            res.send(200);
        });
    });

    process.on('uncaughtException', function (err) {
        var err;
        if (err.stack) {
            err = err.message + '\n' + err.stack;
            console.error("Uncaught Exception: ", err.stack);
        }
        else {
            err = JSON.stringify(err);
            console.error("Uncaught exception (no stack): ", JSON.stringify(err));
        }
        // Send email here...
        mailer.send_error(err, function (err) {
            if (err) console.error(err);
            process.exit(-1);
        })
    });

    var old_console_error = console.error;
    console.error = function () {
        var args = Array.prototype.slice.call(arguments, 0);
        var e = new Error(util.format.apply(util, args));
        var err_string = e.message + '\n' + e.stack;
        mailer.send_error(err_string, function (err) {
            if (err) console.error(err);
        });
        old_console_error.apply(console, args);
    }

And here’s the client side code:

window.onerror = (message, filename, lineno, colno, error) ->
    if arguments.length > 3
        console.log("Got full error: ", message, " ", error.stack);
        $.post('/js_error', { url: window.location.href, error: error.stack });
    else
        console.log("Got short error: ", message);
        $.post('/js_error', { url: window.location.href, error: message + " at " + filename + " line " + lineno});
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s