简体   繁体   English

JavaScript中的错误处理

[英]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? 由于应该在HTML中提供此脚本解析的文本,所以我猜不是在javascript控制台中抛出错误,对吗? 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. 脚本用户不应该知道此脚本,他只需要了解某个HTML属性所需的语法即可更改元素的行为方式。 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. ,唯一的区别是我的解析器不会生成任何html,文本等,它仅隐藏或显示某些输入元素(如果它们符合要解析的文本中提供的条件)。

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.. 此外,它还会产生不一致的样式。

I don't understand what you want but if you want to write something to the javascript console just use console.log("Error!"); 我不明白您想要什么,但是如果您想向javascript控制台中写入内容,请使用console.log("Error!"); I hope this helps you. 我希望这可以帮助你。

For a really good example you might try putting invalid JSON into http://jsonlint.com/ . 对于一个非常好的示例,您可以尝试将无效的JSON放入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; JSONLint包含行号也很有帮助,因此当它说“ x线上的错误”时,它意味着一些东西; 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. HTML中的内联警报将允许我浏览文本区域,直到找到有问题的行并进行修复。

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." 对于某些企业最终用户而言,设置错误框只会让他们感到沮丧,并让他们苦苦地打电话给您的电话,说您的产品不起作用-提供的详细信息应尽量少,“这不是有效的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. 另一方面,对于开发人员来说,您可能希望将Crockford的JSLint的方式告诉他们每一个有点可疑的事情。

you can use try catch like: 您可以使用try catch,例如:

<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 更多细节在这里: Javascript

create function which would parse your code and put it inside try catch:- hope this helps you 创建将解析您的代码并将其放入try catch的函数:-希望这对您有所帮助

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. 对于1,我将使用try/catch机制,并且使用某些html将catch中的所有内容显示回给用户(2.),例如在输入之后/之前添加错误列表。

You could use an Alert Box but I would prefer adding an error- <div> to the dom which contains the error message. 您可以使用警报框,但我更喜欢在包含错误消息的dom中添加错误- <div>

something like: 就像是:

css: CSS:

#error{
   display:none;
}

html: HTML:

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

javascript: 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM