Capture JavaScript errors, rejected promises, and failed resources in a Cocos Creator web build and report them 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 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.