O
opensource junkie
I'm developing my first major ajax system, and in doing so I'm trying to implement a library of custom exceptions similar to ones that I've written inother languages. However, I don't think I'm doing it right, because when my custom exceptions are thrown, they don't retain any information beyond that which I manually set (specifically, I'd like to see the line of code that generated the error).
For example:
throw new Error("foo");
generates an exception with a line number that shows up in my firefox errorconsole. On the other hand, the following code:
function BadServerResponseException() {
this.name = 'BadServerResponseException';
this.message = 'The response provided by the server was invalid';
}
throw new BadServerResponseException();
only retains the information that I specifically provide it. Of course this makes perfect sense; I can hardly expect the system to read my mind and give me what I want without explicit instructions. However, I am unsure of what code will make it record the line number.
I tried a couple methods of inheritance, thinking that perhaps an exceptionthat inherits from the Error "class" would be given the necessary information. I did eventually find a method which works:
function BadServerResponseException() {
var that = new Error();
that.name = 'BadServerResponseException';
that.message = 'The response provided by the server was invalid';
return that;
}
however it records the line number of the "new Error()" call in my exceptions.js file instead of the "new BadServerResponseException()" call at the point where the error occurred.
Unfortunately I'm still in the process of understanding all the intricaciesof prototypical inheritance in javascript, so I'm wondering of someone else might be able to shed some light on the situation. Any help on this would be greatly appreciated,
-- Nate
For example:
throw new Error("foo");
generates an exception with a line number that shows up in my firefox errorconsole. On the other hand, the following code:
function BadServerResponseException() {
this.name = 'BadServerResponseException';
this.message = 'The response provided by the server was invalid';
}
throw new BadServerResponseException();
only retains the information that I specifically provide it. Of course this makes perfect sense; I can hardly expect the system to read my mind and give me what I want without explicit instructions. However, I am unsure of what code will make it record the line number.
I tried a couple methods of inheritance, thinking that perhaps an exceptionthat inherits from the Error "class" would be given the necessary information. I did eventually find a method which works:
function BadServerResponseException() {
var that = new Error();
that.name = 'BadServerResponseException';
that.message = 'The response provided by the server was invalid';
return that;
}
however it records the line number of the "new Error()" call in my exceptions.js file instead of the "new BadServerResponseException()" call at the point where the error occurred.
Unfortunately I'm still in the process of understanding all the intricaciesof prototypical inheritance in javascript, so I'm wondering of someone else might be able to shed some light on the situation. Any help on this would be greatly appreciated,
-- Nate