Docs HomeMongoDB Shell

Error Handlers错误处理程序

Warning

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

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代码段。