1 coherent.defineError= function( errorName )
2 {
3 constructor= function( message )
4 {
5 this.message= message;
6 }
7
8 constructor.prototype= new Error;
9 constructor.prototype.constructor= constructor;
10 constructor.prototype.name= errorName;
11 return constructor;
12 }
13
14 var InvalidArgumentError=
coherent.defineError( "InvalidArgumentError" );
When I create a new InvalidArgumentError instance, the stack indicates
that the error was created on line 8 rather than where ever it was in
the actual code.
What's the proper way to do this?
--
Jeff Watkins
http://metrocat.org
Computers, they're just a fad.
There's 2 ways of doing this:
constructor= function( message ) {
this.stack = (new Error).stack
}
Or if you dont mind it not being portable with other JS implementations
constructor.prototype = Error.__proto__
I *think* something like that should work.
Ash
On Jan 3, 2008, at 6:27 PM, Jeff Watkins wrote:
> Unfortunately, the first way leads to the stack listing the
> constructor as the place where the error was thrown. And the second
> way just simply didn't seem to work – the custom error didn't seem
> to work correctly.
>
> It's not the end of the world, because I really need these tests to
> run in the browser and the stack doesn't work there unless you're
> running FF.
>
> On 3 Jan, 2008, at 4:26 AM, Ash Berlin wrote:
>
>> There's 2 ways of doing this:
>>
>> constructor= function( message ) {
>> this.stack = (new Error).stack
>> }
>>
>> Or if you dont mind it not being portable with other JS
>> implementations
>>
>> constructor.prototype = Error.__proto__
>>
>> I *think* something like that should work.
Okay then...
metis:~ ash$ cat test.js
var coherent = {};
coherent.defineError = function(errorName)
{
constructor = function() {
stack = (new Error).stack.split("\n").slice(1);
[undef, undef, this.fileName, this.lineNumber] =
/^(.*?)@(.*?):(.*?)$/.exec( stack[1] );
this.stack = stack.join("\n");
};
constructor.prototype = {
__proto__: Error.prototype,
name: errorName,
};
return constructor;
}
var InvalidArgumentError =
coherent.defineError( "InvalidArgumentError" );
var err = (new InvalidArgumentError('abc'));
var err2 = new Error("an error");
print(err.lineNumber);
print(err2.lineNumber);
metis:~ ash$ ./code/js/mozilla/dist/bin/js -f test.js
20
21
That might do what you want....