Get reason why "Cannot create ActiveX Object error is thrown - javascript

Is there any way of catching a more detailed reason why the 'Cannot Create ActiveX object' is thrown in JavaScript? I have code of the form
try
{
var obj = new ActiveXObject("MyComponent");
....
}
catch(error)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("C:\\Logs\\MyLog.txt", 2, true);
fh.WriteLine(error.message);
fh.Close();
}
If creating 'MyComponent' fails, the code catches the exception and writing to file etc all works but the file simply contains a message saying "Cannot create ActiveX object" which not is very useful. It is akin to saying "Something went wrong". Is there any way to get more detailed reason why the exception was thrown?
Thanks

Related

How to throw a javascript error during runtime via browser (Chrome)?

My objective: Test out my error handling functionality.
Temporary solution: Have a custom route: /error, which contains code which purposefully produces fatal error.
var a = undefined;
a.b.c // Breaks.
The above works, but I can't use it to test production site as the page is not required.
I was looking for a way to test it via the browser. I tried simply adding"
throw new Error("Custom error thrown here") to the console. That doesn't actually break it during runtime.
I tried adding a break point and adding the same code: throw new Error("Custom error thrown here"). That didn't work either.
Any other easier ways to do this rather than the above?
I was looking for a way where I can do it via browser only.
Thanks.
You did not clearly mention how and where the error should be thrown. I will assume that you can use a modified copy of your JavaScript file to throw errors. The modified file will reside on your computer and only be used when you're using Chrome developer tools. This feature is called Local Overrides. The steps are as follows:
Open the webpage
Open Chrome developer tools for that webpage
In Sources panel go to Overrides tab
Click Select folder for overrides and choose a folder on your computer
A warning appears on the webpage which reads "DevTools requests full access to ..." which you must allow
In Sources panel go to Page tab
Locate the file in which you need to inject the "throw error" code
Right click and choose Save for overrides
Now you can edit the copy of the file on your computer or from within developer tools. Insert the code that produces the error at the desired location. When you reload the page with developer tools open, Chrome will load the local copy of the JavaScript file and throw the error. The error thrown that way will contain the context from where it originated e.g. call stack. If the developer tools are closed then live copy will be used.
If I got your question right, this is How you can do it from the console:
var script_tag = document.createElement('script');
script_tag.type = 'text/javascript';
script_tag.text = 'throw new Error("Custom error thrown here")';
document.body.appendChild(script_tag);
Or if you want you can trigger it on click:
var script_tag = document.createElement('script');
script_tag.type = 'text/javascript';
script_tag.text = 'window.document.onclick = function() { throw new Error("Custom error thrown here")}';
document.body.appendChild(script_tag);
And then you click anywhere on the page, to throw the error;
I would use the exec function which actually takes string and runs the code within at compile time.
exec('a.b.c')
You won't be able to throw an error inside your application from the console, since you are out of scope of the app.
Having said that, one slightly awkward way you could do this is by adding a breakpoint at the start of the javascript file.
Reload the page and your app will pause at the breakpoint - you can then modify the code as you need - like adding a throw new Error("something...") - and save your edits.
Then allow the code to run and you will see your error.
A downside is if you reload the changes will be gone, but I believe it's as close as you can get to modifying code at runtime.
Add this code to your production code
window.addEventListener('err', () => {
throw new Error('break it');
})
and when you want to create an error simply
dispatchEvent(new Event('err'))
in the console
You can use a global variable, which is accessible from your app and from debug console.
if (window.shouldThrow) {
throw new Error("Custom error thrown here");
}
This way you can turn on/off the exception throwing using the window.shouldThrow variable.
Try this way to catch error detail on run time
try
{
var a = undefined;
a.b.c // Breaks.
}
catch ( e )
{
alert("Error: " + e.description );
}

How do I determine what exception was thrown?

I work on a script that is added to hundreds of different websites in ways that I can't always predict, particularly when code is loaded in an iframe. In my code I've got a try/catch block setup to handle the exceptions. It's something like this:
var vals = {};
try {
vals.hostname = window.top.location.hostname; // will throw DOMException if loaded in iframe
< more code which might throw other exceptions >
} catch (e) {
if (e instanceof DOMException) {
vals.hostname = window.location.hostname;
} else {
<do something different>
}
}
e instanceof DOMException works awesome on Chrome, but it turns out that on Firefox and Safari, e instanceof DOMException is false on those browsers. I've done a lot of searching and for some reason can't seem to find anybody who explains how to check the exception type in a browser agnostic way.
Edit: Okay, Chrome is throwing a DOMException, but Firefox (and presumably Safari) are just throwing a generic "Error":
Error: Permission denied to access property "hostname"
I think that means I can't do anything with the specific error on other browsers.

"Permission denied" error while using VisibleOnScreen property on an web element in test complete tool

I am unable to use "VisibleOnScreen" property on an Web element in test complete tool.Error is inconsistent, sometimes it works fine.
Error:Permission denied
Error location:
Eg:-
var obj = Sys.Browser("*").Page("*").FindChildByXPath(xpath);
if(obj!=null) {
if(obj.VisibleOnScreen) {
Log.Message("Object is visible on Screen");
}
}

How to output a message in Googles Blockly?

I'm having a bit of fun on https://blockly-games.appspot.com and have got to the last level where you can write full blown javascript (new Date().getTime(); is very handy).
Despite having the console open in Chrome (ctrl-shift-j) I can't seem to write anything to it with console.log("test"); from within the game. When I do I see:
// Starting battle with 2 players. compressed.js:420
// [Player] throws an error: Unknown identifier: console compressed.js:423
// undefined compressed.js:423
// [Player] dies. compressed.js:416
Yet if I type console.log("hmm"); in the console I properly see:
// console.log("hmm");
// hmm VM1038:2
// undefined
Same story if I try to use alert("test").
I want to debug, but the only forms of output I've found are manipulating the duck and throwing Unknown identifiers. Is there any way out of this sandbox?
Update: How do I print debug messages in the Google Chrome JavaScript Console? has some ideas for restoring console but they don't seem to work in this case.
Update: just to be clear this is what chrome looks like when experiencing this error.
Found a kludgey workaround:
throw "\nYour message here.";
Displays:
// [Player] throws an error: Unknown identifier: compressed.js:423
// Your message here.
// undefined compressed.js:423
// [Player] dies. compressed.js:416
Killing yourself just to log something may seem harsh but now you can write some snazzy asserts.
function assertTrue(test, message) {
if (!test) {
throw message;
}
}
It works, but Lord knows I wish there was a better way.

Get selected message data in Thunderbird extension

I need to get some e-mail message data in my Thunderbird extension. I found this example on MDN (https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIMsgMessageService):
var content = "";
var MessageURI = GetFirstSelectedMessage();
var MsgService = messenger.messageServiceFromURI(MessageURI);
var MsgStream = Components.classes["#mozilla.org/network/sync-stream-listener;1"].createInstance();
var consumer = MsgStream.QueryInterface(Components.interfaces.nsIInputStream);
var ScriptInput = Components.classes["#mozilla.org/scriptableinputstream;1"].createInstance();
var ScriptInputStream = ScriptInput.QueryInterface(Components.interfaces.nsIScriptableInputStream);
ScriptInputStream.init(consumer);
try {
MsgService.streamMessage(MessageURI, MsgStream, msgWindow, null, false, null);
} catch (ex) {
alert("error: "+ex)
}
ScriptInputStream .available();
while (ScriptInputStream .available()) {
content = content + ScriptInputStream .read(512);
}
alert(content);
However, when I run it I get the following error:
Timestamp: 2013.06.21. 14:47:21
Error: ReferenceError: GetFirstSelectedMessage is not defined
Source File: chrome://edus_extension/content/messengerOverlay.js
Line: 90
What is this 'GetFirstSelectedMessage' function and how can I get message URI without using it?
This documentation looks fairly outdated. I would suggest:
using gFolderDisplay.selectedMessage (try typing top.opener.gFolderDisplay.selectedMessage in the Error Console),
reading some recent code that uses Services and MailServices so as to simplify your code.
That being said, I don't know what you're trying to achieve but:
you'd certainly be better off using a wrapper such as MsgHdrToMimeMessage (self-reference: http://blog.xulforum.org/index.php?post/2011/01/03/An-overview-of-Thunderbird-Conversations)
if you absolutely, absolutely need to get the raw contents of the message, http://mxr.mozilla.org/comm-central/source/mailnews/db/gloda/modules/mimemsg.js#223 has an example on how to do that (it's the implementation of the said MsgHdrToMimeMessage; by simplifying it, you should be able to get directly the raw data of the message).
Good luck with that, once you get a working sample, please add it to the MDN wiki!
Cheers,
jonathan

Categories

Resources