Question
Is there a way to trigger a custom Javascript error message? In Chrome, I have found console.warn[error,log]
for sending messages to the browser. However, this only works in Chrome (and possibly Firefox). This line itself triggers an error in Internet explorer, though.
I'd like to be able to trigger errors that violate the rules of the Javascript function. For example, a function that takes an object as a value expects the object to have an attribute of "dialog". If that attribute doesn't exist, I want to trigger an error message "Modal Window: No dialog id specified."
Context
I am building a PHP class and Javascript class that works together so that programmers of our application can easily create dialog boxes that will be uniform and reliable.
Example Code
try {
if (!params || !params['dialog']) {
throw "NoDID";
}
}
catch (error) {
if (error == "NoDID") {
//com.MySITE.js.warn("Modal Window: No dialog id specified.");
//Trigger custom error message here.
}
}
I'd like the message to go to the javascript window for easy developer debugging. Just doing "throw" results in "Uncaught Custom Error Message". I don't want the user to see this in anything on the page. I want it available in the js log for developer use.
Question
Is there a way to trigger a custom Javascript error message? In Chrome, I have found console.warn[error,log]
for sending messages to the browser. However, this only works in Chrome (and possibly Firefox). This line itself triggers an error in Internet explorer, though.
I'd like to be able to trigger errors that violate the rules of the Javascript function. For example, a function that takes an object as a value expects the object to have an attribute of "dialog". If that attribute doesn't exist, I want to trigger an error message "Modal Window: No dialog id specified."
Context
I am building a PHP class and Javascript class that works together so that programmers of our application can easily create dialog boxes that will be uniform and reliable.
Example Code
try {
if (!params || !params['dialog']) {
throw "NoDID";
}
}
catch (error) {
if (error == "NoDID") {
//com.MySITE.js.warn("Modal Window: No dialog id specified.");
//Trigger custom error message here.
}
}
I'd like the message to go to the javascript window for easy developer debugging. Just doing "throw" results in "Uncaught Custom Error Message". I don't want the user to see this in anything on the page. I want it available in the js log for developer use.
Share Improve this question edited Sep 17, 2017 at 2:27 ReinstateMonica3167040 7969 silver badges30 bronze badges asked Mar 30, 2012 at 16:47 teynonteynon 8,29810 gold badges71 silver badges109 bronze badges2 Answers
Reset to default 14Use throw
:
throw "Deliberate Error!";
if (console) console.warn(...
(Or you could use console.error, console.log, console.trace, etc.). Alternatively you could also do:
console && console.warn(...
Or, if you don't like checking for console everywhere you could, at the start of your code, do:
window.console = console || {warn: function(){}};
In other words, if the browser doesn't have a console, make a fake one with a fake "warn" method that does nothing. If you do this, you can do console.warn throughout your code without worrying about whether the browser has a console or not.
Yet another approach would be to make your own warn method:
function warn(message) {
console && console.warn(message);
}
and then you could just do warn("something went wrong").
Of course, if you want to actually find out when an IE user gets a user, you'll need to instead make your own warn message, which would have to make an AJAX request to somewhere on the server (and that somewhere would then send you an email or something).