Capture window errors, unhandled promise rejections, and failed resources in a Cocos Creator Web build, then report useful diagnostics to a server.
After a game is released, bugs can be invisible until a player reports them—and even then, the report may not reveal where the problem occurred. I added global error listeners and sent diagnostics to a server, which also uncovered errors that players never reported.
Register Global Error and Promise Rejection Listeners
Different error types require different listeners. This implementation applies only to web builds; native builds will not report through these browser APIs.
Replace the reporting calls with your own API. Also rate-limit and deduplicate reports: a global error can fire repeatedly and overwhelm both the game and the server. This example avoids reporting the same line, column, and message repeatedly.