Docs Home / mongosh / Snippets / Reference

Error Handlers错误处理程序

Warning

This feature is experimental. MongoDB does not provide support for Snippets. This feature may be changed or removed at any time without prior notice.此功能是实验性的。MongoDB不支持代码段。此功能可能随时更改或删除,恕不另行通知。

Bugs are not expected, however should you encounter one, please open an issue in the GitHub repository for this project.预计不会出现错误,但如果您遇到错误,请在GitHub存储库中为此项目打开一个问题。

Error handlers allow you to specify regular expressions to catch runtime errors and display custom error messages.错误处理程序允许您指定正则表达式来捕获运行时错误并显示自定义错误消息。

To use an error matcher, add a line to your package.json file like the highlighted line in this excerpt from a snippet in the community GitHub repository.要使用错误匹配器,请在package.json文件中添加一行,如社区GitHub存储库中代码段摘录中突出显示的行。

...
"description": "mongo compatibility script for mongosh",
"main": "index.js",
"errorMatchers": "error-matchers.js",
"license": "SSPL",
...

For an example of error matching code, see the mongocompat snippet.有关错误匹配代码的示例,请参阅mongocompat代码片段。