成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

JavaScript錯誤處理和堆棧追蹤淺析

開發 前端
有時我們會忽略錯誤處理和堆棧追蹤的一些細節, 但是這些細節對于寫與測試或錯誤處理相關的庫來說是非常有用的。合理地處理堆棧信息能使你清除無用的數據, 而只專注于有用的數據. 同時, 當更好地理解 Errors 對象及其相關屬性之后, 能有助于你更充分地利用 Errors。

[[187726]]

有時我們會忽略錯誤處理和堆棧追蹤的一些細節, 但是這些細節對于寫與測試或錯誤處理相關的庫來說是非常有用的. 例如這周, 對于 Chai 就有一個非常棒的PR, 該PR極大地改善了我們處理堆棧的方式, 當用戶的斷言失敗的時候, 我們會給予更多的提示信息(幫助用戶進行定位).

合理地處理堆棧信息能使你清除無用的數據, 而只專注于有用的數據. 同時, 當更好地理解 Errors 對象及其相關屬性之后, 能有助于你更充分地利用 Errors.

(函數的)調用棧是怎么工作的

在談論錯誤之前, 先要了解下(函數的)調用棧的原理:

當有一個函數被調用的時候, 它就被壓入到堆棧的頂部, 該函數運行完成之后, 又會從堆棧的頂部被移除.

堆棧的數據結構就是后進先出, 以 LIFO (last in, first out) 著稱.

例如:

  1. function c() { 
  2.  
  3.     console.log('c'); 
  4.  
  5.  
  6.   
  7.  
  8. function b() { 
  9.  
  10.     console.log('b'); 
  11.  
  12.     c(); 
  13.  
  14.  
  15.   
  16.  
  17. function a() { 
  18.  
  19.     console.log('a'); 
  20.  
  21.     b(); 
  22.  
  23.  
  24.   
  25.  
  26. a();  

在上述的示例中, 當函數 a 運行時, 其會被添加到堆棧的頂部. 然后, 當函數 b 在函數 a 的內部被調用時, 函數 b 會被壓入到堆棧的頂部. 當函數 c 在函數 b 的內部被調用時也會被壓入到堆棧的頂部.

當函數 c 運行時, 堆棧中就包含了 a, b 和 c(按此順序).

當函數 c 運行完畢之后, 就會從堆棧的頂部被移除, 然后函數調用的控制流就回到函數 b. 函數 b 運行完之后, 也會從堆棧的頂部被移除, 然后函數調用的控制流就回到函數 a. ***, 函數 a 運行完成之后也會從堆棧的頂部被移除.

為了更好地在demo中演示堆棧的行為, 可以使用 console.trace() 在控制臺輸出當前的堆棧數據. 同時, 你要以從上至下的順序閱讀輸出的堆棧數據.

  1. function c() { 
  2.  
  3.     console.log('c'); 
  4.  
  5.     console.trace(); 
  6.  
  7.  
  8.   
  9.  
  10. function b() { 
  11.  
  12.     console.log('b'); 
  13.  
  14.     c(); 
  15.  
  16.  
  17.   
  18.  
  19. function a() { 
  20.  
  21.     console.log('a'); 
  22.  
  23.     b(); 
  24.  
  25.  
  26.   
  27. a();  

在 Node 的 REPL 模式中運行上述代碼會得到如下輸出:

  1. Trace 
  2.  
  3.     at c (repl:3:9) 
  4.  
  5.     at b (repl:3:1) 
  6.  
  7.     at a (repl:3:1) 
  8.  
  9.     at repl:1:1 // <-- For now feel free to ignore anything below this point, these are Node's internals 
  10.  
  11.     at realRunInThisContextScript (vm.js:22:35) 
  12.  
  13.     at sigintHandlersWrap (vm.js:98:12) 
  14.  
  15.     at ContextifyScript.Script.runInThisContext (vm.js:24:12) 
  16.  
  17.     at REPLServer.defaultEval (repl.js:313:29) 
  18.  
  19.     at bound (domain.js:280:14) 
  20.  
  21.     at REPLServer.runBound [as eval] (domain.js:293:12)  

正如所看到的, 當從函數 c 中輸出時, 堆棧中包含了函數 a, b 以及c.

如果在函數 c 運行完成之后, 在函數 b 中輸出當前的堆棧數據, 就會看到函數 c 已經從堆棧的頂部被移除, 此時堆棧中僅包括函數 a 和 b.

  1. function c() { 
  2.  
  3.     console.log('c'); 
  4.  
  5.  
  6.   
  7.  
  8. function b() { 
  9.  
  10.     console.log('b'); 
  11.  
  12.     c(); 
  13.  
  14.     console.trace(); 
  15.  
  16.  
  17.   
  18.  
  19. function a() { 
  20.  
  21.     console.log('a'); 
  22.  
  23.     b(); 
  24.  
  25.  

正如所看到的, 函數 c 運行完成之后, 已經從堆棧的頂部被移除.

  1. Trace 
  2.  
  3.     at b (repl:4:9) 
  4.  
  5.     at a (repl:3:1) 
  6.  
  7.     at repl:1:1  // <-- For now feel free to ignore anything below this point, these are Node's internals 
  8.  
  9.     at realRunInThisContextScript (vm.js:22:35) 
  10.  
  11.     at sigintHandlersWrap (vm.js:98:12) 
  12.  
  13.     at ContextifyScript.Script.runInThisContext (vm.js:24:12) 
  14.  
  15.     at REPLServer.defaultEval (repl.js:313:29) 
  16.  
  17.     at bound (domain.js:280:14) 
  18.  
  19.     at REPLServer.runBound [as eval] (domain.js:293:12) 
  20.  
  21.     at REPLServer.onLine (repl.js:513:10)  

Error對象和錯誤處理

當程序運行出現錯誤時, 通常會拋出一個 Error 對象. Error 對象可以作為用戶自定義錯誤對象繼承的原型.

Error.prototype 對象包含如下屬性:

  • constructor–指向實例的構造函數
  • message–錯誤信息
  • name–錯誤的名字(類型)

上述是 Error.prototype 的標準屬性, 此外, 不同的運行環境都有其特定的屬性. 在例如 Node, Firefox, Chrome, Edge, IE 10+, Opera 以及 Safari 6+ 這樣的環境中, Error 對象具備 stack 屬性, 該屬性包含了錯誤的堆棧軌跡. 一個錯誤實例的堆棧軌跡包含了自構造函數之后的所有堆棧結構.

如果想了解更多關于 Error 對象的特定屬性, 可以閱讀 MDN 上的這篇文章.

為了拋出一個錯誤, 必須使用 throw 關鍵字. 為了 catch 一個拋出的錯誤, 必須使用 try...catch 包含可能跑出錯誤的代碼. Catch的參數是被跑出的錯誤實例.

如 Java 一樣, JavaScript 也允許在 try/catch 之后使用 finally 關鍵字. 在處理完錯誤之后, 可以在finally 語句塊作一些清除工作.

在語法上, 你可以使用 try 語句塊而其后不必跟著 catch 語句塊, 但必須跟著 finally 語句塊. 這意味著有三種不同的 try 語句形式:

  • try...catch
  • try...finally
  • try...catch...finally

Try語句內還可以在嵌入 try 語句:

  1. try { 
  2.  
  3.     try { 
  4.  
  5.         throw new Error('Nested error.'); // The error thrown here will be caught by its own `catch` clause 
  6.  
  7.     } catch (nestedErr) { 
  8.  
  9.         console.log('Nested catch'); // This runs 
  10.  
  11.     } 
  12.  
  13. } catch (err) { 
  14.  
  15.     console.log('This will not run.'); 
  16.  
  17.  

也可以在 catch 或 finally 中嵌入 try 語句:

  1. try { 
  2.  
  3.     console.log('The try block is running...'); 
  4.  
  5. } finally { 
  6.  
  7.     try { 
  8.  
  9.         throw new Error('Error inside finally.'); 
  10.  
  11.     } catch (err) { 
  12.  
  13.         console.log('Caught an error inside the finally block.'); 
  14.  
  15.     } 
  16.  
  17.  

需要重點說明一下的是在拋出錯誤時, 可以只拋出一個簡單值而不是 Error 對象. 盡管這看起來看酷并且是允許的, 但這并不是一個推薦的做法, 尤其是對于一些需要處理他人代碼的庫和框架的開發者, 因為沒有標準可以參考, 也無法得知會從用戶那里得到什么. 你不能信任用戶會拋出 Error 對象, 因為他們可能不會這么做, 而是簡單的拋出一個字符串或者數值. 這也意味著很難去處理堆棧信息和其它元信息.

例如:

  1. function runWithoutThrowing(func) { 
  2.  
  3.     try { 
  4.  
  5.         func(); 
  6.  
  7.     } catch (e) { 
  8.  
  9.         console.log('There was an error, but I will not throw it.'); 
  10.  
  11.         console.log('The error\'s message was: ' + e.message) 
  12.  
  13.     } 
  14.  
  15.  
  16.   
  17.  
  18. function funcThatThrowsError() { 
  19.  
  20.     throw new TypeError('I am a TypeError.'); 
  21.  
  22.  
  23.   
  24.  
  25. runWithoutThrowing(funcThatThrowsError);  

如果用戶傳遞給函數 runWithoutThrowing 的參數拋出了一個錯誤對象, 上面的代碼能正常捕獲錯誤. 然后, 如果是拋出一個字符串, 就會碰到一些問題了:

  1. function runWithoutThrowing(func) { 
  2.  
  3.     try { 
  4.  
  5.         func(); 
  6.  
  7.     } catch (e) { 
  8.  
  9.         console.log('There was an error, but I will not throw it.'); 
  10.  
  11.         console.log('The error\'s message was: ' + e.message) 
  12.  
  13.     } 
  14.  
  15.  
  16.   
  17.  
  18. function funcThatThrowsString() { 
  19.  
  20.     throw 'I am a String.'
  21.  
  22.  
  23.   
  24.  
  25. runWithoutThrowing(funcThatThrowsString);  

現在第二個 console.log 會輸出undefined. 這看起來不是很重要, 但如果你需要確保 Error 對象有一個特定的屬性或者用另一種方式來處理 Error 對象的特定屬性(例如 Chai的throws斷言的做法), 你就得做大量的工作來確保程序的正確運行.

同時, 如果拋出的不是 Error 對象, 也就獲取不到 stack 屬性.

Errors 也可以被作為其它對象, 你也不必拋出它們, 這也是為什么大多數回調函數把 Errors 作為***個參數的原因. 例如:

  1. const fs = require('fs'); 
  2.  
  3.   
  4.  
  5. fs.readdir('/example/i-do-not-exist'function callback(err, dirs) { 
  6.  
  7.     if (err instanceof Error) { 
  8.  
  9.         // `readdir` will throw an error because that directory does not exist 
  10.  
  11.         // We will now be able to use the error object passed by it in our callback function 
  12.  
  13.         console.log('Error Message: ' + err.message); 
  14.  
  15.         console.log('See? We can use Errors without using try statements.'); 
  16.  
  17.     } else { 
  18.  
  19.         console.log(dirs); 
  20.  
  21.     } 
  22.  
  23. });  

***, Error 對象也可以用于 rejected promise, 這使得很容易處理 rejected promise:

  1. new Promise(function(resolve, reject) { 
  2.  
  3.     reject(new Error('The promise was rejected.')); 
  4.  
  5. }).then(function() { 
  6.  
  7.     console.log('I am an error.'); 
  8.  
  9. }).catch(function(err) { 
  10.  
  11.     if (err instanceof Error) { 
  12.  
  13.         console.log('The promise was rejected with an error.'); 
  14.  
  15.         console.log('Error Message: ' + err.message); 
  16.  
  17.     } 
  18.  
  19. });  

處理堆棧

這一節是針對支持 Error.captureStackTrace的運行環境, 例如Nodejs.

Error.captureStackTrace 的***個參數是 object, 第二個可選參數是一個 function.Error.captureStackTrace 會捕獲堆棧信息, 并在***個參數中創建 stack 屬性來存儲捕獲到的堆棧信息. 如果提供了第二個參數, 該函數將作為堆棧調用的終點. 因此, 捕獲到的堆棧信息將只顯示該函數調用之前的信息.

用下面的兩個demo來解釋一下. ***個, 僅將捕獲到的堆棧信息存于一個普通的對象之中:

  1. const myObj = {}; 
  2.  
  3.   
  4.  
  5. function c() { 
  6.  
  7.  
  8.   
  9.  
  10. function b() { 
  11.  
  12.     // Here we will store the current stack trace into myObj 
  13.  
  14.     Error.captureStackTrace(myObj); 
  15.  
  16.     c(); 
  17.  
  18.  
  19.   
  20.  
  21. function a() { 
  22.  
  23.     b(); 
  24.  
  25.  
  26.   
  27.  
  28. // First we will call these functions 
  29.  
  30. a(); 
  31.  
  32.   
  33.  
  34. // Now let's see what is the stack trace stored into myObj.stack 
  35.  
  36. console.log(myObj.stack); 
  37.  
  38.   
  39.  
  40. // This will print the following stack to the console: 
  41.  
  42. //    at b (repl:3:7) <-- Since it was called inside B, the B call is the last entry in the stack 
  43.  
  44. //    at a (repl:2:1) 
  45.  
  46. //    at repl:1:1 <-- Node internals below this line 
  47.  
  48. //    at realRunInThisContextScript (vm.js:22:35) 
  49.  
  50. //    at sigintHandlersWrap (vm.js:98:12) 
  51.  
  52. //    at ContextifyScript.Script.runInThisContext (vm.js:24:12) 
  53.  
  54. //    at REPLServer.defaultEval (repl.js:313:29) 
  55.  
  56. //    at bound (domain.js:280:14) 
  57.  
  58. //    at REPLServer.runBound [as eval] (domain.js:293:12) 
  59.  
  60. //    at REPLServer.onLine (repl.js:513:10)  

從上面的示例可以看出, 首先調用函數 a(被壓入堆棧), 然后在 a 里面調用函數 b(被壓入堆棧且在a之上), 然后在 b 中捕獲到當前的堆棧信息, 并將其存儲到 myObj 中. 所以, 在控制臺輸出的堆棧信息中僅包含了 a和 b 的調用信息.

現在, 我們給 Error.captureStackTrace 傳遞一個函數作為第二個參數, 看下輸出信息:

  1. const myObj = {}; 
  2.  
  3.   
  4.  
  5. function d() { 
  6.  
  7.     // Here we will store the current stack trace into myObj 
  8.  
  9.     // This time we will hide all the frames after `b` and `b` itself 
  10.  
  11.     Error.captureStackTrace(myObj, b); 
  12.  
  13.  
  14.   
  15.  
  16. function c() { 
  17.  
  18.     d(); 
  19.  
  20.  
  21.   
  22.  
  23. function b() { 
  24.  
  25.     c(); 
  26.  
  27.  
  28.   
  29.  
  30. function a() { 
  31.  
  32.     b(); 
  33.  
  34.  
  35.   
  36.  
  37. // First we will call these functions 
  38.  
  39. a(); 
  40.  
  41.   
  42.  
  43. // Now let's see what is the stack trace stored into myObj.stack 
  44.  
  45. console.log(myObj.stack); 
  46.  
  47.   
  48.  
  49. // This will print the following stack to the console: 
  50.  
  51. //    at a (repl:2:1) <-- As you can see here we only get frames before `b` was called 
  52.  
  53. //    at repl:1:1 <-- Node internals below this line 
  54.  
  55. //    at realRunInThisContextScript (vm.js:22:35) 
  56.  
  57. //    at sigintHandlersWrap (vm.js:98:12) 
  58.  
  59. //    at ContextifyScript.Script.runInThisContext (vm.js:24:12) 
  60.  
  61. //    at REPLServer.defaultEval (repl.js:313:29) 
  62.  
  63. //    at bound (domain.js:280:14) 
  64.  
  65. //    at REPLServer.runBound [as eval] (domain.js:293:12) 
  66.  
  67. //    at REPLServer.onLine (repl.js:513:10) 
  68.  
  69. //    at emitOne (events.js:101:20)  

當將函數 b 作為第二個參數傳給 Error.captureStackTraceFunction 時, 輸出的堆棧就只包含了函數 b 調用之前的信息(盡管 Error.captureStackTraceFunction 是在函數 d 中調用的), 這也就是為什么只在控制臺輸出了 a. 這樣處理方式的好處就是用來隱藏一些與用戶無關的內部實現細節. 

責任編輯:龐桂玉 來源: 前端大全
相關推薦

2017-03-08 08:57:04

JavaScript錯誤堆棧

2017-04-25 15:30:23

堆棧函數JavaScript

2022-11-16 08:41:43

2024-03-27 08:18:02

Spring映射HTML

2023-11-08 15:04:55

事務GORM

2021-04-14 07:08:14

Nodejs錯誤處理

2021-05-11 10:01:54

avaScript錯誤處理

2020-09-15 08:28:17

JavaScript錯誤處理

2020-09-14 08:35:36

JavaScript編程開發

2014-11-17 10:05:12

Go語言

2009-08-05 16:04:50

2010-06-01 16:14:04

2021-04-29 09:02:44

語言Go 處理

2023-10-28 16:30:19

Golang開發

2023-10-26 15:49:53

Go日志

2022-05-06 08:00:51

Golang編程語言Java

2014-07-30 09:56:41

iPhoneiPad

2016-08-19 10:41:42

Swift 2錯誤

2023-12-26 22:05:53

并發代碼goroutines

2015-08-19 14:11:56

SQL Server錯誤處理
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲网址 | 国产一区| 国产精品久久久久久吹潮 | 亚洲精品乱码久久久久久按摩观 | 国产精品1区2区 | 毛片99 | 日韩中文字幕一区二区 | 日韩精品一区二区三区 | 久久久久久久久久爱 | 精品国产精品三级精品av网址 | 国产欧美一区二区三区日本久久久 | av色站| 亚洲色欲色欲www | 国产乱码精品一区二区三区中文 | 日韩免费视频一区二区 | 剑来高清在线观看 | 日韩欧美视频 | 人妖videosex高潮另类 | 午夜精品一区二区三区在线视频 | 精品国产一区二区久久 | 在线日韩 | 91久久久www播放日本观看 | 亚洲精品视频在线观看免费 | 蜜桃一区 | 日韩av在线中文字幕 | 久久33| 奇米av | 亚洲精品久久久久中文字幕二区 | 欧美日韩一区二区三区视频 | 国产在线一区二区三区 | 国产高清精品在线 | 久久精品欧美一区二区三区不卡 | 中文视频在线 | 日本在线免费看最新的电影 | 亚洲精品黑人 | 午夜精 | www四虎com| 女人av | 青草视频在线 | 久久国产精品-国产精品 | 欧美精品1区2区 |