Temporal Blog 09月30日 19:17
如何设计友好的错误信息
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

设计友好的错误信息对于提升开发者体验至关重要。文章强调了错误信息应简洁、具体且易于理解,避免使用技术术语和模糊的表达。建议使用动词开头,明确指出问题所在,并提供可能的解决方案或调试步骤。同时,文章还提出了几个设计原则,如面向用户而非维护者、提供错误原因和解决方法、确保错误信息可搜索、记录错误以及避免冗长。遵循这些原则可以显著减少开发者的挫败感,提高问题解决效率。

🔍错误信息应简洁、具体且易于理解,避免使用技术术语和模糊的表达,以便用户能够快速识别问题。

🛠️建议使用动词开头,明确指出问题所在,并提供可能的解决方案或调试步骤,帮助用户进行下一步操作。

👤错误信息应面向用户而非维护者,使用用户熟悉的配置和值,避免内部变量和函数名称,确保信息对用户有实际帮助。

💡提供错误原因和解决方法,例如,如果可能,错误信息本身可以建议如何修复问题,或指出用户需要查找的调试信息。

🔗错误信息应可搜索,使用独特、可读的ID,方便用户在问题追踪器、文档网站或搜索引擎中快速找到相关信息。

📈错误信息应被记录下来,频繁出现的错误可能是更深层次问题的信号,例如API设计不良,需要进一步调查和改进。

📝错误信息不应过于冗长,避免一次性提供过多信息,以免用户在大量错误中迷失。关键信息应突出,并提供指向更详细文档的链接。

There is nothing more frustrating than a cryptic error message.

When something goes wrong, chances are you already knew it - all a cryptic error message does is confirm your suspicion and tell you that you're gonna need to read through a bunch of code to figure out your error.

As framework and library designers, we can and should do better for our users by carefully following a few design principles.

Why We Should Care#

Who among us hasn't thrown up their hands at an unhelpful Error: NullPointerException or undefined is not a function? Of course, this is a straw man - unanticipated errors always leak internals. We should do our best to handle these errors as much as possible!

The frustrating part comes from errors that were anticipated but obviously not given any thought:

This is particularly aggravating because it compounds an already unhappy situation with an even worse debugging and bug reporting experience.

Thoughtful error message design is one of the less glamorous, but most important, parts of Developer Exception Engineering. Most framework developers instinctively write error messages that make sense to them, but a few minutes of extra effort can save DAYS of debugging for everyone else (including maintainers!).

We must acknowledge two things:

    there is immense information asymmetry between the framework developer and the framework user (developers know the codebase far better than users) error messages are part of your developer experience too

You might be familiar with the old adage "If you fail to plan, you plan to fail." Here, we have a slight twist: If you fail to plan to fail, you are planning to fail your users when they need you most. Touting great developer experience with lousy error message design is like selling a beautiful car with seatbelts that don't work when there's a crash.

Error Message Style Guide#

At Temporal, our current style guide reflects our engineering roots in Uber and Golang with some tactical recommendations:

    Use errors.New for simple messages and fmt.Errorf for formatted messages. Make sure you're using the standard errors go package. Error messages start with lowercase and no punctuation at the end: unable to open file instead of Unable to open file.. Error messages should be actionable, e.g. no such directory instead of loading service config. Use unable to... instead of failed to..., can't..., or could not.... Messages should reflect the logical action, not merely a function name: load service config instead of LoadServiceConfig. This message should add actionable, meaningful context to the existing error message.

That said, as we evolve our developer experience philosophy, we are looking toward higher level design principles that help guide successful outcomes.

Error Message Design Principles#

We are still in the early stages of pinning down our beliefs in what makes for great error messages, but here are some candidate principles running through my mind:

    Errors should be written for USERS, not maintainers: Your error should make sense from a user point of view. For example, you should cite user-supplied configs and values familiar to users, not the internal variable and function names familiar to maintainers. Errors should suggest WHY they happened, or even better - HOW to fix them: As a maintainer, you probably have a high amount of context as to the possible causes of an error - take the time to write down some hints for users. If there's a high probability you know what's wrong, the error message itself can suggest how to fix it. But even without that certainty, think about what the user is likely to need to log out to debug the error, and offer it in the error message itself. Where relevant, Errors should display what was expected vs what was received. Errors should be searchable: Your users are most likely going to paste your errors into the search bars of your issue tracker, docs site, or Google. Consider the "SEO" of your errors. Sometimes offering a unique, human readable ID for each error can help narrow things down very quickly! Errors should be logged: Errors that happen far more often than they ought to are a signal of a deeper issue. If a user code or configuration error happens too often, it isn't the user's fault - you have bad API design. If inside a terminal environment, you should offer some text formatting to make your errors visually distinct from normal log messages. Last but not least, Errors should not be too verbose: You might be tempted to dump every piece of information under the sun, but this can be very annoying when scrolling through reams of errors. Most users don't read entire errors - make your words count, and link out to docs for fuller context. Errors are not docs.

Ultimately, the same high level principle applies to error messages as they do for anything involving human computer interaction: Don't Make Me Think!

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

错误信息设计 开发者体验 API设计 调试 用户体验
相关文章