Created
May 8, 2014 06:38
-
-
Save ishiduca/aa2c36b646c906a0998a to your computer and use it in GitHub Desktop.
CustomError(Error)の継承
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var test = require('tape') | |
test('01', function (t) { | |
function MyError (message) { | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(this, this.constructor) | |
} else { | |
this.stack = (new Error).stack || '' | |
} | |
this.name = this.constructor.name | |
this.message = message || this.name | |
return this | |
} | |
;(function (MyError) { | |
var F = function () {} | |
F.prototype = Error.prototype | |
MyError.prototype = new F | |
MyError.prototype.constructor = MyError | |
})(MyError) | |
var myError = new MyError('foo') | |
t.ok(myError instanceof Error, 'myError instanceof Error') | |
t.ok(myError instanceof MyError, 'myError instanceof MyError') | |
t.ok(myError.constructor === MyError, 'myError.constructor === MyError') | |
t.ok(myError.constructor !== Error, 'myError.constructor !== Error') | |
t.ok(myError.toString, myError.toString()) | |
try { | |
throw myError | |
} catch (err) { | |
t.ok(err, 'can catch myError') | |
t.ok(/^MyError.+?foo$/.test(err.toString()), '/^MyError foo$/.test(myError.toString())') | |
t.ok(err.stack, 'ok err.stack') | |
} | |
t.end() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment