Closed
Bug 166436
Opened 22 years ago
Closed 3 years ago
JS API needs way to create Error object with caller's filename/lineno
Categories
(Core :: JavaScript Engine, enhancement)
Core
JavaScript Engine
Tracking
()
RESOLVED
WORKSFORME
People
(Reporter: f.braem, Unassigned)
Details
Attachments
(1 file)
558 bytes,
text/html
|
Details |
When you create an Error object, you can't set the linenumber and filename property. It would be nice to be able to retrieve the filename and the linenumber of the currently executing script. This would be very useful for error reporting when exceptions are used. Maybe this can be added as API's in the Exception API's. Or maybe the constructor of the Error object can fill in these properties automatically.
Comment 1•22 years ago
|
||
> When you create an Error object, you can't set the linenumber > and filename property. On the face of it, I have to mark this bug invalid, because we CAN set them via the 2nd, 3rd parameters to the Error constructor. Be careful about case, however: the property names are called |fileName| and |lineNumber|, not |filename|, |linenumber|. See: bug 50447 "JS Exceptions should have filename/linenumber properties" especially http://bugzilla.mozilla.org/show_bug.cgi?id=50447#c12 This may also be of interest: bug 119719 "Custom JS errors: filename/linenumber should default to location of throw" Here is a small test script, which I will put below as an attachment - <script> var HR = '<hr>'; var LF = '<br>'; var LF2 = LF + LF; var ERR = new Error('A CUSTOM ERROR', '(FileName)', 999); debugThis('We have created a new Error object in global scope:' + LF, ERR); document.write(LF2 + HR); try { throw ERR; } catch (e) { debugThis( 'We have caught the error in a try...catch block: ' + LF, e ) } function debugThis(msg, e) { msg += LF + 'e.message = ' + e.message; msg += LF + 'e.fileName = ' + e.fileName; msg += LF + 'e.lineNumber = ' + e.lineNumber; document.write(msg); } </script>
Status: UNCONFIRMED → RESOLVED
Closed: 22 years ago
Resolution: --- → INVALID
Comment 2•22 years ago
|
||
Comment 3•22 years ago
|
||
Marking Verified. Franky: please reopen this bug if I have misunderstood your request -
Status: RESOLVED → VERIFIED
Reporter | ||
Comment 4•22 years ago
|
||
Maybe the requirement wasn't clear enough: I know that there are properties on the Error object to set the filename and linenumber. The problem is that it is not possible to set the filename and linenumber when I use the SpiderMonkey API in C++. When I use JS_ReportError I get everything what I want, but I want to use the exception mechanism and throw an Error object. Or even throw an own object which prototype is Error. It would be nice that there was an API available to get the current linenumber and filename. Franky.
Status: VERIFIED → UNCONFIRMED
Resolution: INVALID → ---
Comment 5•22 years ago
|
||
OK, thanks - Reassigning to Kenton; cc'ing Brendan, rogerl, rginda, shaver
Assignee: rogerl → khanson
Status: UNCONFIRMED → NEW
Ever confirmed: true
Comment 6•22 years ago
|
||
I think this bug is mis-summarized, and I'm resummarizing it. The jsdbgapi.h API already provides all the hooks, but they're for debuggers and other such tools, not for general API users. OTOH, a way to create an Error object from C would be helpful, to (a) relieve the overhead of looking up "Error" and calling its value, and (b) get the top-most scripted caller's filename and lineno. /be
Summary: SpiderMonkey API: Add helper functions for retrieving filename, linenumber, ... → JS API needs way to create Error object with caller's filename/lineno
Updated•19 years ago
|
Assignee: khanson → general
QA Contact: pschwartau → general
Comment 7•16 years ago
|
||
If I'm not confused there's a lot of bang for a smallish buck fixing this one - it's not uncommon for a native function to want to throw an exception, and there isn't really a satisfying way to do it with jsapi.h right now, at least not that I've found. A simple function JS_ThrowException(ctx, "bad stuff") that did the equivalent of: throw new Error("bad stuff") (including having the stack trace and file/line automatically filled in) would be hugely useful. JS_SetPendingException() does not create the Error object or set file and line, while JS_ReportErrorNumber() has bug #215173 and is not really a very convenient API to begin with. Here is an example of what people are coming up with in the meantime: http://egachine.berlios.de/embedding-sm-best-practice/embedding-sm-best-practice.html#error-handling (scroll down to "Example 4, macro to report errors by throwing an Error exception object", basically compile "throw new Error()" to a function then call it)
Comment 8•16 years ago
|
||
There's a not-as-crazy example here: http://developer.mozilla.org/en/docs/JSAPI_Phrasebook#throw Obviously we need to do a better job of documenting what's already there. A new JS_ThrowException API might be a good idea, too. Some possible use cases to address: * setting the fileName and lineNumber (see this bug's summary) * throwing a TypeError, as opposed to a plain Error * user-defined exception constructors * error message localization
Assignee | ||
Updated•10 years ago
|
Assignee: general → nobody
Comment 9•8 years ago
|
||
Fwiw, it's possible to do this with JS_ReportErrorNumberUC and company. That's what the DOM bindings in Firefox use to throw various subclasses of Error... The filename and linenumber gets set automatically by the engine. It allows throwing a TypeError. It probably allows localization, via the function callback you pass in. I agree the API for it is not so intuitive.
Comment 10•3 years ago
|
||
https://searchfox.org/mozilla-central/source/js/public/ErrorReport.h and https://searchfox.org/mozilla-central/source/js/public/Exception.h seem to provide the necessary functions for creating error objects with specific file names, line numbers, stacks etc. Therefore resolving as WFM.
Status: NEW → RESOLVED
Closed: 22 years ago → 3 years ago
Resolution: --- → WORKSFORME
You need to log in
before you can comment on or make changes to this bug.
Description
•