Custom error classes

by Nathan Perry

HTML

<div class="output"></div>

<table class="test-output" style="display: none;">
    <tr>
        <th class="test">Test</th>
        <th class="result">Result</th>
        <th class="comments">Comments</th>
    </tr>
</table>

SCSS

.output {
    p {
        padding: 0;
        margin: 0;
    }
}

span.fail {
    color: red;
}

span.pass {
    color: green;
}

table {
    border: 1px solid black;
    border-collapse: collapse;
    width: 100%;
    tr {
        border: 1px solid gray;
    }
    td, th {
        border: 1px solid gray;
        margin: 0;
        padding-top: 2px;
        padding-bottom: 2px;
        padding-left: 4px;
        padding-right: 4px;
        width: 32%
    }
}

JavaScript

/// Write a log message
var l = function(message) {
	console.log.apply(console, arguments);
	var args = Array.prototype.slice.call(arguments);
    if (args.length === 0) { args = ["&nbsp;"]; }
	$('.output').append('<p>'+args.join(' ')+'</p>');
};

/// Print result of a test in the table
var t = function(test, result, expected, comments){
	//$('table.test-output').append('<tr><td>'+test+'</td><td>'+(result || '')+'</td><td>'+(comments || '')+'</td></tr>');
    $('.output').append(
        '<table>'+
          '<tr>'+
            '<td>'+test+'</td>'+
            '<td><span class="'+(expected === undefined ? '' : (result === expected ? 'pass' : 'fail'))+'">'+
            (result == null ? 'null' : result)+
            '</span></td>'+
            '<td>'+(comments == null ? '' : comments)+'</td>'+
          '</tr>'+
        '</table>');
};

l('start', '0');

var testError = function(name, Err){
    l();
	// l(name, 'instanceof Error =>', Err instanceof Error);
	var e = new Err('Custom message for '+name);
    l('e = new '+name+'()');
	t('e instanceof '+name, e instanceof Err, true);
    t('e instanceof Error', e instanceof Error, true);
    t('e.name', e.name, name);
    t('e.message', e.message, 'Custom message for '+name);
    t('!!e.stack', !!e.stack, true);
};

l();
l('From <a href="http://stackoverflow.com/a/871646/761771">here</a>');

function MyError1(message) {
	this.name = "MyError1";
	this.message = (message || "");
}
MyError1.prototype = Error.prototype;

testError('MyError1', MyError1);

function MyError2(message) {
	this.message = (message || "");
}
MyError2.prototype = new Error();

testError('MyError2', MyError2);

function MyError2_5(message) {
	this.name = "MyError2_5";
	this.message = (message || "");
}
MyError2_5.prototype = new Error();

testError('MyError2_5', MyError2_5);

l();
l('From <a href="http://stackoverflow.com/a/17891099/761771">here</a>');

function MyError3() {
    var temp = Error.apply(this, arguments);
    temp.name = this.name =...