Why is my custom Error object converted to a String by the Express router?
In fact what's happening is that the string showing up is not your error object at all - it's another error being thrown inside the guts of Express (an "illegal access" exception). The error is happening when Express is comparing the error object to a string.
The root cause is apparently setting the "type" property on your custom Error object. This is apparently a no-no with V8 - it causes toString() to thrown the "illegal access" exception. More details here: https://code.google.com/p/v8/issues/detail?id=2397
Try this. Note that APIError
is now a named function:
var util = require('util');function APIError(type) { Error.call(this, { message: type, constructorOpt: APIError }); this.name = 'APIError';}util.inherits(APIError, Error);APIError.prototype.name = 'APIError';
Also, have a look here.