Skip to content

Commit 2d7e82a

Browse files
error
1 parent 0836ec6 commit 2d7e82a

File tree

2 files changed

+267
-0
lines changed

2 files changed

+267
-0
lines changed

Error:Debugger/Error/index.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
//************ javascript 错误 ***************
3+
// `<EvalError>` eval 执行 出错, 该错误类型已经不再在ES5中出现了,只是为了保证与以前代码兼容,才继续保留
4+
// eval('xxx'); //ReferenceError: xxx is not defined
5+
6+
// `<SyntaxError>` js 语法错误
7+
// const i-1; // SyntaxError: Missing initializer in const declaration
8+
9+
// `<RangeError>` 数组越界错误
10+
// const array = new Array(-1); // RangeError: Invalid array length
11+
12+
// `<ReferenceError>` 引用错误
13+
// const a = b; // ReferenceError: b is not defined
14+
15+
// `<TypeError>` 对象类型错误
16+
// const c = (null).c; // TypeError: Cannot read property 'c' of null
17+
18+
// `<URIError>` url 错误
19+
// decodeURI('%'); // URIError: URI malformed
20+
21+
22+
//****************** 操作系统错误 **************************
23+
// 由底层操作系触发的系统错误,例如试图打开不存在的文件
24+
// const fs = require('fs');
25+
// fs.openSync('x.js'); // Error: ENOENT: no such file or directory, open 'x.js'
26+
27+
// 权限不足
28+
// const os = require('os');
29+
// os.setPriority(-1); //SystemError [ERR_SYSTEM_ERROR]: A system error occurred: uv_os_setpriority returned EACCES (permission denied)
30+
31+
// 端口占用
32+
// const http = require('http');
33+
// http.createServer().listen(3000);
34+
// http.createServer().listen(3000); // Error: listen EADDRINUSE: address already in use :::3000
35+
36+
37+
// ******************* 用户自定义错误 ****************
38+
// throw new Error('CustomError'); // Error: CustomError
39+
40+
41+
// ******************* 断言错误 ****************
42+
// const assert = require('assert');
43+
// assert.strictEqual(1, 2, 'assert error'); // AssertionError [ERR_ASSERTION]: assert error
44+
45+
46+
47+
48+
// 错误捕获
49+
50+
// error first
51+
// const fs = require('fs');
52+
// fs.readFile('xxx.js', (err, data) => {
53+
// if (err) {
54+
// console.log(err); // { [Error: ENOENT: no such file or directory, open 'xxx.js'] errno: -2, code: 'ENOENT', syscall: 'open', path: 'xxx.js' }
55+
// return;
56+
// }
57+
// console.log(data);
58+
// });
59+
60+
// try catch
61+
// try {
62+
// a ++;
63+
// } catch (e) {
64+
// console.log(e); //ReferenceError: a is not defined
65+
// }
66+
67+
// // EventEmitter on error
68+
// const EventEmitter = require('events');
69+
//
70+
// const emitter = new EventEmitter();
71+
// emitter.on("error", error => {
72+
// console.log(error); // Error: Some Error
73+
// });
74+
// emitter.emit("error", new Error('Some Error'));
75+
76+
// // uncaughtException, 非常规手段
77+
// process.on("uncaughtException", (error) => {
78+
// console.log(error) // ReferenceError: i is not defined
79+
// });
80+
// i++;
81+
82+
// promise unhandledRejection(未监听 uncaughtException时有效)
83+
// process.on("unhandledRejection", (reason, p) => {
84+
// console.log(`${reason} ${p}`); // unhandledRejection [object Promise]
85+
// });
86+
// Promise.reject('unhandledRejection');
87+
88+
89+
// 异步导致堆栈丢失
90+
const throwError = () => {
91+
throw new Error();
92+
};
93+
94+
const doSth = () => {
95+
setImmediate(() => throwError());
96+
};
97+
98+
const main = () => {
99+
doSth();
100+
};
101+
102+
main();
103+
/**
104+
throw new Error();
105+
^
106+
107+
Error
108+
at throwError (/Users/wangjun/Desktop/practice/opensource/node-every-day/Error:Debugger/Error/index.js:91:11)
109+
at Immediate.setImmediate (/Users/wangjun/Desktop/practice/opensource/node-every-day/Error:Debugger/Error/index.js:95:24)
110+
at runCallback (timers.js:705:18)
111+
at tryOnImmediate (timers.js:676:5)
112+
at processImmediate (timers.js:658:5)
113+
* */
114+

Error:Debugger/error.md

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Error
2+
3+
### 错误分类
4+
5+
标准 JavaScript 错误
6+
7+
```js
8+
//************ javascript 错误 ***************
9+
// `<EvalError>` eval 执行 出错, 该错误类型已经不再在ES5中出现了,只是为了保证与以前代码兼容,才继续保留
10+
eval('xxx'); //ReferenceError: xxx is not defined
11+
12+
// `<SyntaxError>` js 语法错误
13+
const i-1; // SyntaxError: Missing initializer in const declaration
14+
15+
// `<RangeError>` 数组越界错误
16+
const array = new Array(-1); // RangeError: Invalid array length
17+
18+
// `<ReferenceError>` 引用错误
19+
const a = b; // ReferenceError: b is not defined
20+
21+
// `<TypeError>` 对象类型错误
22+
const c = (null).c; // TypeError: Cannot read property 'c' of null
23+
24+
// `<URIError>` url 错误
25+
decodeURI('%'); // URIError: URI malformed
26+
```
27+
28+
系统错误
29+
30+
```js
31+
//****************** 操作系统错误 **************************
32+
// 由底层操作系触发的系统错误,例如试图打开不存在的文件
33+
const fs = require('fs');
34+
fs.openSync('x.js'); // Error: ENOENT: no such file or directory, open 'x.js'
35+
36+
// 权限不足
37+
const os = require('os');
38+
os.setPriority(-1); //SystemError [ERR_SYSTEM_ERROR]: A system error occurred: uv_os_setpriority returned EACCES (permission denied)
39+
40+
// 端口占用
41+
const http = require('http');
42+
http.createServer().listen(3000);
43+
http.createServer().listen(3000); // Error: listen EADDRINUSE: address already in use :::3000
44+
```
45+
46+
用户自定义错误
47+
48+
```js
49+
// ******************* 用户自定义错误 ****************
50+
throw new Error('CustomError'); // Error: CustomError
51+
```
52+
53+
断言错误
54+
55+
```js
56+
// ******************* 断言错误 ****************
57+
const assert = require('assert');
58+
assert.strictEqual(1, 2, 'assert error'); // AssertionError [ERR_ASSERTION]: assert error
59+
```
60+
61+
62+
### 错误捕获
63+
64+
error first(约定,非强制)
65+
66+
```js
67+
const fs = require('fs');
68+
fs.readFile('xxx.js', (err, data) => {
69+
if (err) {
70+
console.log(err); // { [Error: ENOENT: no such file or directory, open 'xxx.js'] errno: -2, code: 'ENOENT', syscall: 'open', path: 'xxx.js' }
71+
return;
72+
}
73+
console.log(data);
74+
});
75+
```
76+
77+
try catch
78+
79+
```js
80+
try {
81+
a ++;
82+
} catch (e) {
83+
console.log(e); //ReferenceError: a is not defined
84+
}
85+
```
86+
87+
EventEmitter on error
88+
89+
```js
90+
const EventEmitter = require('events');
91+
92+
const emitter = new EventEmitter();
93+
emitter.on("error", error => {
94+
console.log(error); // Error: Some Error
95+
});
96+
emitter.emit("error", new Error('Some Error'));
97+
```
98+
99+
uncaughtException(非常规手段,需正确使用)
100+
101+
```js
102+
// uncaughtException, 非常规手段
103+
process.on("uncaughtException", (error) => {
104+
console.log(error) // ReferenceError: i is not defined
105+
});
106+
i++;
107+
```
108+
109+
unhandledRejection(promise 未 catch,且未监听 uncaughtException)
110+
111+
```js
112+
process.on("unhandledRejection", (reason, p) => {
113+
console.log(`${reason} ${p}`); // unhandledRejection [object Promise]
114+
});
115+
Promise.reject('unhandledRejection');
116+
```
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+

0 commit comments

Comments
 (0)