Front-end code anomaly monitoring practice preface
- How JS handles exceptions
- Report the way
- Abnormal monitoring Reports common problems
JS exception handling
- The current code block will be pushed into the task queue as a task, and the JS thread will constantly extract the task execution from the task queue.
- When an exception occurs during the execution of a task and the exception is not caught and handled, it is continuously thrown out of the call stack until the execution of the current task is terminated.
- The JS thread will continue to fetch the next task from the task queue and continue to execute it.
> error console.log(‘ never executed ‘); </
><
> console.log(‘ I continue ‘)</
>
Try-catch exception processing
Example: runtime error
{ error
}
(e) {
.
(
);
.
(e); }
Example: syntax error
{
error
;
}
(e) {
.
(
);
.
(e); }
Example: Asynchronous error
{
(()
{ error
})}
(e) {
.
(
);
.
(e); }
Window. onerror exception handling
.
(msg, url, row, col, error) {
.
(
);
.
({ msg, url, row, col, error })
; }; error;
.
(msg, url, row, col, error) {
.
(
);
.
({ msg, url, row, col, error })
; };
(()
{ error; });
- For global capture like onError, it is best to write it at the beginning of all JS scripts, because you cannot guarantee that the code you are writing will not be wrong. If you write it later, it will not be caught by onError.
- In addition, onError is an error that cannot catch network exceptions.
> window.onerror = function (MSG, url, row, col, error) {console.log(‘ I know the async error ‘); console.log({ msg, url, row, col, error }) return true; }; </
><
=
>
>window.addEventListener(‘error’, (MSG, url, row, col, error) => {console.log(‘ I know 404 is wrong ‘); console.log( msg, url, row, col, error ); return true; }, true); </
><
=
=
>
Promise error
.
(
, (msg, url, row, col, error)
{
.
(
);
.
( msg, url, row, col, error ); },
);
.
(
);
((resolve, reject)
{
(
); });
((resolve)
{
(a); }).
(()
{
});
.
(
.
(e){ e.
(a)
.
(
);
.
(e.reason);
; });
.
(
);
((resolve, reject)
{
(
); });
((resolve)
{
(a); }).
(()
{
});
Abnormal Reporting Mode
- Send data through Ajax
- Dynamically create the form of an IMG tag
(error) {
reportUrl
;
().src
reportUrl
error; }
Monitoring Reports common problems
Git clone github.com/happylindz/… Blog /code/jserror/ NPM installScript Error
script src
script
Cross-source Resource Sharing Mechanism (CORS)
> window.onerror = function (MSG, url, row, col, error) {console.log(‘ I know the error, I know the error ‘); console.log({ msg, url, row, col, error }) return true; }; </
><
=
></
>// http://localhost:8081/test.jssetTimeout(() => { console.log(error); });
(
);
(
);
(
);
(a); app.
(
()); app.
(
(
)(path.
(
.
))); app.
(
, ()
{
.
(
)});
.
(msg, url, row, col, error) {
.
(
);
.
({ msg, url, row, col, error })
; };
(data) {
.
(data); }
;
.
(
); script.src
url;
.
.
(script);
.
(
); script.crossOrigin
; script.src
url;
.
.
(script);
({ url
, dataType
.
(data)
{
.
(data); }})
.createElement
(
() {
.createElement.
(
);
(type) {
(type);
(type
) { result.crossOrigin
; }
result; }}) ();
.
(msg, url, row, col, error) {
.
(
);
.
({ msg, url, row, col, error })
; }; $.
({ url
, dataType
.
(data)
{
.
(data); }})
Window. onError Whether to catch iframe errors
=
=
></
><
> window.frames[0]. Onerror = function (MSG, url, row, col, error) {console.log(‘ I know iframe error ‘); console.log({ msg, url, row, col, error }) return true; }; </
>
How does compressed code locate script exceptions
How to collect too much exception information
(data) {
(
.
(a)
) {
(data)
}}
Refer to the article
-
Script Error volume optimization – Monitors Script error reporting
-
Collect and monitor front-end code exception logs
-
Front-end Magic Hall – Exceptions are more than just try/catch