博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 不显示消息错误_如何编写不吸的错误消息
阅读量:2522 次
发布时间:2019-05-11

本文共 11202 字,大约阅读时间需要 37 分钟。

mysql 不显示消息错误

by Justin Fuller

贾斯汀·富勒(Justin Fuller)

如何编写不吸的错误消息 (How To Write Error Messages That Don’t Suck)

“A validation error occurred.” Yep. Thanks!

“发生验证错误。” 是的 谢谢!

The release is imminent; this is the last update that needs to be verified, and I get an error message that’s as useful as the close button on an elevator.

即将发布。 这是需要验证的最新更新,并且我收到一条错误消息,该消息与电梯上的关闭按钮一样有用。

It turns out that it is a validation error, kind of. The input I am giving is a duplicate. It’s valid, it just already exists!

事实证明,这一种验证错误。 我输入的内容是重复的。 有效,它已经存在!

Wouldn’t it be so helpful to know that?

知道那不是很有帮助吗?

In fact it would be helpful to be informed of several things when errors happen. I don’t need to know everything. The history of programming won’t help me here. The message should give me just enough info for me to get this error fixed so that I can finish my work, go home, and play with my kids.

实际上,在发生错误时通知几件事会很有帮助。 我不需要什么都知道 编程的历史不会对我有帮助。 该消息应该为我提供了足够的信息来解决此错误,以便我可以完成工作,回家和与孩子一起玩。

是什么导致错误? (What makes an Error?)

In JavaScript an error object always has the name, message, and stack properties. The name gives you an at-a-glance classification of the error. The stack tells you where it happened. The message? Well, according to some developers, you don’t need to worry about that! “The stack trace gives you everything you need.”

在JavaScript中,错误对象始终具有名称,消息和堆栈属性。 该名称为您提供了该错误的一览表。 堆栈告诉您发生的位置。 消息? 好吧,根据一些开发人员的说法,您不必为此担心! “堆栈跟踪为您提供了所需的一切。”

Please, don’t be one of those developers.

请不要成为这些开发人员之一。

有用的错误信息 (Useful Error Messages)

Raise your right hand, place your left hand on a copy of “Clean Code” and repeat after me.

举起右手,将左手放在“清洁代码”的副本上,然后跟着我重复。

“I swear to include enough detail in my error messages that future developers will easily be able to determine what went wrong and what they need to do to fix it.”

“我保证在错误消息中包含足够的细节,以便将来的开发人员可以轻松地确定出了什么问题以及需要采取哪些措施来解决它。”

发生了什么 (What Happened)

When a police officer pulls you over to give you a ticket, does it say “Bad Driving”? No! It says you were going 65 miles per hour in a 25 miles per hour school zone, you passed a stopped bus, and your car hasn’t been inspected in four years! Sure, you’re going to jail, but at least you know why.

当警务人员将您拉下车给您罚单时,它是否显示“不良驾驶”? 没有! 它说您在每小时25英里的学区中以每小时65英里的速度行驶,通过了一辆停下来的公共汽车,四年来没有对您的汽车进行检查! 当然,您将入狱,但至少您知道为什么

So the error message from earlier should not be, “A validation error occurred”, but instead:

因此,来自较早版本的错误消息不应为“发生验证错误”,而是:

Unable to save model "user" because the property "email" with value "JustinFuller@company.com" already exists.

Instead of a simple error that says, “Invalid option” use:

代替一个简单的错误提示“ Invalid option”,使用:

The option "update" is not valid. Valid options include "upsert", "get", and "delete".

These updated error messages attempt to help us understand the cause, giving us a start toward the solution.

这些更新的错误消息试图帮助我们了解原因,从而使我们着手解决方案。

它可能如何发生 (How It Might Have Happened)

Now that the error is describing what exactly went wrong, it’s time to help the poor soul who stumbled into this predicament begin to climb back out. Watch carefully. When done correctly it will seem like you’re reaching forward through time by anticipating what could have led to this unfortunate turn of events. You’ll be right there with that future developer — maybe yourself — telling them that everything is fine, that you’ll get through this together.

既然错误描述了确切的错误原因,是时候帮助跌入这种困境的可怜的灵魂开始退缩。 小心点 如果做得正确,看来您正在通过预测可能导致这种不幸的事件转变的时间而向前迈进。 您将与将来的开发人员(也许是您自己)在一起,告诉他们一切都很好,您将共同完成这项工作。

You’ll begin by explaining what happened.

您将从解释发生的事情开始。

For anything that has a prerequisite step, such as configuration or validation, you can suggest to verify that step has been completed. Don’t worry if your error messages get long. It’s better to provide too much information than not enough.

对于具有先决条件步骤的任何内容(例如配置或验证),您可以建议验证该步骤是否已完成。 如果您的错误消息很长,请不要担心。 最好提供太多的信息而不是提供太多的信息。

I’ll add more detail to one of the earlier examples:

我将为较早的示例之一添加更多细节:

The option “update” is not valid. Valid options include “upsert”, “get”, and “delete”. If you expected “update” to be an option, you must first export it from the file: "./src/controllers/index.js".

Now you are anticipating how this might have happened: the developer probably just forgot to export the new option. The error becomes a reminder of that step. You’ve now shown two possible causes of the error; the first is a possible typo (here are the valid options) and the second is a configuration error (here’s where it should be exported).

现在,您正在预期这可能是怎么发生的:开发人员可能只是忘记了导出新选项。 该错误提示该步骤。 现在,您已经显示了两种可能的错误原因; 第一个是可能的错字(这里是有效的选项),第二个是配置错误(这里是应将其导出)。

The React library does an excellent job of anticipating how errors might have occurred. They don’t address every edge case, but they do give helpful hints for the most common errors. For example, you can’t use the function reactDom.renderToNodeString() in the browser because node streams don’t exist there. So React gives you a suggestion of how it happened and how to fix it:

React库在预测错误如何发生方面做得非常出色。 它们并没有解决所有极端情况,但确实为最常见的错误提供了有用的提示。 例如,您不能在浏览器中使用函数reactDom.renderToNodeString() ,因为那里不存在节点流。 因此,React为您提供了有关它如何发生以及如何解决的建议:

ReactDOMServer.renderToNodeStream(): The streaming API is not available in the browser. Use ReactDOMServer.renderToString() instead.

There could be other ways for that error to occur, but they guess that the most common reason is that renderToNodeStream was called in the browser.

可能有其他方式发生该错误,但他们猜测最常见的原因是在浏览器中调用了renderToNodeStream

相关资料 (Relevant Data)

While writing error messages you must remember that applications rarely do only one thing at a time. So when errors occur it’s a challenge to find the state of the application at that time. For this reason it’s very important to capture the relevant data and relay it back to the developer so that they can pinpoint the cause.

在编写错误消息时,您必须记住,应用程序很少一次只做一件事。 因此,当发生错误时,在那个时候查找应用程序的状态是一个挑战。 因此,捕获相关数据并将其转发给开发人员非常重要,这样他们才能查明原因。

In the first example I included the phrase:

在第一个示例中,我添加了短语:

The property “email” with value “JustinFuller@company.com” already exists.

This is very useful, but could be impractical. It may take too much effort or time to create natural language errors for every variation of data, or in some cases we may simply be passing on a failure outside of our own control, so the only option left is to give a good description and include as much relevant data that is safe to print.

这是非常有用的,但可能不切实际。 对于每种数据变化,创建自然语言错误可能会花费过多的精力或时间,或者在某些情况下,我们可能只是将失败传递给我们自己的控制之外,因此剩下的唯一选择是给出良好的描述并包括安全打印尽可能多的相关数据。

The choice of what data is safe to print is tricky: if you choose exactly what properties to include then you end up modifying the list every time there is a new property, or, worse, you forget and it doesn’t show up when needed; on the other hand you can remove properties that are known to be unsafe, but here you risk adding a new property and forgetting to exclude it, causing leaked sensitive data. You have to use your judgement and consider your company rules. Does your software handle highly valuable or personal data that should not be written to an non-encrypted destination? Thoughtlessly logging out every object that causes an error is liable to get you fired from some jobs, while at others it’s the standard operating procedure. So please, use common sense and be careful!

选择哪种数据可以安全打印是很棘手的:如果您确切选择要包含的属性,则每次有新属性时最终都要修改列表,或者更糟的是,您忘记了该列表,并且在需要时它不会显示; 另一方面,您可以删除已知不安全的属性,但是在这里您可能会冒险添加新属性而忘记将其排除,从而导致敏感数据泄漏。 您必须运用自己的判断力并考虑公司规则。 您的软件是否处理不应写入非加密目标的极有价值的个人数据? 粗心地注销导致错误的每个对象都可能使您从某些工作中被解雇,而在另一些工作中,这是标准的操作过程。 因此,请使用常识并当心!

意外错误 (Unexpected Errors)

There are two ways to include relevant data when you don’t know exactly which property or action caused the error.

当您不完全知道哪个属性或操作导致错误时,有两种方法可以包含相关数据。

The first should be used when you intend for the error to be read by humans, you put the data right in the message: An error was received: “Duplicate entry found for user.email”, while upserting user: { “email”: “justinfuller@company.com” }. This error style has its drawbacks, like the entire object being placed into the error message that could be sent somewhere unintended. If, however, you know it’s safe then this style has the advantage of giving complete details about the situation.

当您打算让人类读取错误时,应该使用第一个,您将数据放在消息中: An error was received: “Duplicate entry found for user.email”, while upserting user: { “email”: “justinfuller@company.com” }. 这种错误样式有其缺点,例如将整个对象放入错误消息中,而该错误消息可能会意外发送到某个地方。 但是,如果您知道它是安全的,则此样式的优点是可以提供有关情况的完整详细信息。

In other cases you may not want the data to leak to a log file or an API response. You can provide a reference ID, a time stamp that can be manually or automatically referenced to data later, or some other property that will allow the developer to track down the pesky data-point that caused an error.

在其他情况下,您可能不希望数据泄漏到日志文件或API响应中。 您可以提供参考ID,可以在以后手动或自动参考数据的时间戳,或提供其他一些属性,使开发人员可以跟踪引起错误的讨厌数据点。

预期错误 (Expected Errors)

I’ll be the first to admit that I’m prone to making simple mistakes. I type “upswert” instead of “upsert”; I type “npm tes” instead of “npm test”. So it’s really refreshing when I get an error message saying:

我将是第一个承认我容易犯简单错误的人。 我键入“ upswert”而不是“ upsert”; 我键入“ npm tes”而不是“ npm test”。 因此,当我收到一条错误消息:

Unknown command, "npm tes". Did you mean npm test?

When the developers prepped for this, they obviously gazed into the future and saw that someone would make that typo — or maybe they just know that humans are prone to silly errors .

当开发人员为此做好准备时,他们显然盯着未来,看到有人会打错字,或者他们只是知道人类很容易犯傻错误。

Whenever there’s a step in a process that can go predictably wrong you have an opportunity to prepare clear guidance on what went wrong and how to fix it.

只要流程中的某个步骤可能出现可预见的错误,您就有机会针对错误的原因以及如何解决问题准备清晰的指导。

解决问题的步骤 (Steps To Fix The Issue)

For some errors it will be possible to give a solution to the error instead of just reporting that it happened. Some times it will be easy, like earlier when we showed the correct options after accidentally typing “update” instead of “upsert”. Other times you’ll need to put in more effort to give the user enough information to correct their mistake, like if you’ve detected a recursive dependency, and you need to tell them where the loop is and what they must do to remove it.

对于某些错误,有可能为错误提供解决方案,而不仅仅是报告它已发生。 有时候,这很容易,例如前面提到的,我们在不小心输入“ update”而不是“ upsert”之后显示了正确的选项。 其他时候,您需要付出更多的努力来为用户提供足够的信息来纠正他们的错误,例如,如果您检测到递归依赖项,则需要告诉他们循环在哪里以及如何删除循环。

不会出错的错误 (An error that doesn’t suck)

So, do you want to provide helpful error messages? In the next error you write, try to include a full description of what happened, how it might have happened, any relevant data that is safe to include, and any steps that might help resolve the problem.

因此,您想提供有用的错误消息吗? 在您编写的下一个错误中,尝试包括对发生的情况,可能发生的情况,可以安全包括的所有相关数据以及可能有助于解决问题的所有步骤的完整说明。

Hi, I’m Justin Fuller. I’m so glad you read my post! I need to let you know that everything I’ve written here is my own opinion and is not intended to represent my employer in any way. All code samples are my own, and are completely unrelated to Bank Of America’s code.

嗨,我叫Justin Fuller。 我很高兴您阅读我的帖子! 我需要让您知道,我在这里写的所有内容都是我自己的观点,并不以任何方式代表我的雇主。 所有代码示例都是我自己的,并且与美国银行的代码完全无关。

I’d also love to hear from you, please feel free to connect with me on , , or . Thanks again for reading!

我也希望收到您的 ,请随时通过 , 或与我联系。 再次感谢您的阅读!

翻译自:

mysql 不显示消息错误

转载地址:http://ubewd.baihongyu.com/

你可能感兴趣的文章
windows系统下安装MySQL
查看>>
错误提示总结
查看>>
实验二+070+胡阳洋
查看>>
Linux IPC实践(3) --具名FIFO
查看>>
Qt之模拟时钟
查看>>
第一次接触安卓--记于2015.8.21
查看>>
(转)在分层架构下寻找java web漏洞
查看>>
mac下多线程实现处理
查看>>
C++ ifstream ofstream
查看>>
跟初学者学习IbatisNet第四篇
查看>>
seL4环境配置
查看>>
Git报错:insufficient permission for adding an object to repository database .git/objects
查看>>
ajax跨域,携带cookie
查看>>
BZOJ 1600: [Usaco2008 Oct]建造栅栏( dp )
查看>>
nginx 高并发配置参数(转载)
查看>>
洛谷 CF937A Olympiad
查看>>
Codeforces Round #445 C. Petya and Catacombs【思维/题意】
查看>>
用MATLAB同时作多幅图
查看>>
python中map的排序以及取出map中取最大最小值
查看>>
ROR 第一章 从零到部署--第一个程序
查看>>