ellabeauty
ellabeauty

Reputation: 2599

Error handling in javascript

I was wondering how to handle errors inside a script that is meant to be used as a "parser". Basically I need a way to handle parsing errors.

Since the text that this script parses should be provided in the HTML, I guess throwing the error in the javascript console is not an option, right? The user of the script should not be aware of this script, he just needs to know the syntax required for a certain HTML attribute in order to change the way the element behaves. Think of it like markdown., the only difference is that my parser will not generate any html, text, etc., it just hides or shows certain input elements if they meet conditions provided in the text to be parsed.

Should I stick with a simple browser alert message?

Appending an error message in the document (near the element that contains the text to be parsed) is not really an option, because the point of this script is to modify behaviour of certain input elements, and not to append text or something like that. Besides, it would produce inconsistent styling..

Upvotes: 2

Views: 336

Answers (5)

Christoph
Christoph

Reputation: 51181

You could use an Alert Box but I would prefer adding an error-<div> to the dom which contains the error message.

something like:

css:

#error{
   display:none;
}

html:

<div id="error"></div>           // <- show error here
<textarea id="input"></textarea> // <- user inputs the text

javascript:

try {
    // Code to run
} catch ( e ) {
    // Write the error to your #error-div and display it
}

optionally, you could log the error to console, when it exists.

if (window.console) console.log('hello world');

Upvotes: 1

Cyril N.
Cyril N.

Reputation: 39859

Your question seems to be waiting two types of answer :

  1. what happen if your parser fails to parse
  2. how to show the error to your user

For 1, I would use the try/catch mechanism and everything that goes in the catch would be displayed back to the user (2.) using some html, like adding after/before the input, a list of errors.

Upvotes: 1

CR Drost
CR Drost

Reputation: 9807

For a really good example you might try putting invalid JSON into http://jsonlint.com/ . Setting a border around the text input to be red and then including a text box containing the location of the parse error is a pretty slick graphical way to say "something was wrong with this input, here's what was wrong with it." It helps also that JSONLint includes line numbers, so that when it says "error on line x" that means something; you can just go there. But it depends rather strongly on who your users are going to be.

One issue with alert boxes is that it tends to royally suck for copying and it tends to tie up the rest of the page. An inline alert in the HTML will let me browse the textarea until I find the offending line, and fix it.

It also depends on your audience. For certain business end-users, putting up an error box is just going to frustrate them and have them huffily call your phone line saying that your product doesn't work -- providing details should be kept minimal, "This is not a valid X." On the other hand, for developers, you might want to tell them every single thing which is a little questionable, the way that Crockford's JSLint does.

Upvotes: 2

Aman
Aman

Reputation: 1382

you can use try catch like:

<script type="text/javascript">

try {
    // Code to run
    [break;]
} catch ( e ) {
    // Code to run if an exception occurs
    [break;]
}[ finally {
    // Code that is always executed regardless of 
    // an exception occurring
}]

</script>

More dtails are here: Javascript

create function which would parse your code and put it inside try catch:- hope this helps you

Upvotes: 1

Rafael Marques
Rafael Marques

Reputation: 825

I don't understand what you want but if you want to write something to the javascript console just use console.log("Error!"); I hope this helps you.

Upvotes: 2

Related Questions