การติดตามสแต็กข้อยกเว้นของ Javascript


90

ใน Firefox ฉันสามารถติดตามสแต็กของข้อยกเว้นได้โดยใช้exception.stack.

มีวิธีรับสิ่งนั้นในเบราว์เซอร์อื่นด้วยหรือไม่?

แก้ไข:ฉันต้องการบันทึกการติดตามสแต็กโดยอัตโนมัติ (ถ้าเป็นไปได้) และไม่ดีบักในเวลานั้น (เช่นฉันรู้วิธีรับสแต็กแทร็กในดีบักเกอร์)

คำตอบ:


74

Place this line where you want to print the stack trace:

console.log(new Error().stack);

Note: tested by me on Chrome 24 and Firefox 18

May be worth taking a look at this tool as well.


2
Sweet - I use a combination of onerror to get the line number and then wrap the offending line with a try catch and your suggestion to alert the stacktrace back to the user. ` window.onerror = function(message, url, lineNumber) { alert('message: ' + message + ' - url: ' + url + ' - ln: ' + lineNumber); return true; }; ` Then wrap it to print out the stacktrace. ` try { // Some code that is causing the exception. } catch(e) { alert(new Error().stack); } `
Philip Murphy

อย่างไรก็ตามการตายจะทำให้ฉันได้บรรทัดแรกของสแต็กเท่านั้น ผมทำอะไรผิดหรือเปล่า?? เช่น: function foo () {fdasmkl} ลอง {foo ()} catch (e) {err = new Error (e) .stack; console.log (err)} = >>> Error: ReferenceError: fdasmkl is not defined at <anonymous>:6:7แทน"Error: ReferenceError: fdasmkl is not defined at eval (eval at <anonymous> (:6:1), <anonymous>:1:1) at <anonymous>:6:1"
chitzui

ไม่เป็นไรพบปัญหา เนื่องจากเป็นข้อผิดพลาดอยู่แล้วฉันสามารถทำได้: function foo() { fdasmkl } try { foo() } catch(e) { console.log(e.stack) }ทันที
chitzui

23

ขณะนี้ Webkit มีฟังก์ชันที่มีสแต็กเทรซ:

Web Inspector: การทำความเข้าใจ Stack Tracesโพสต์โดย Yury Semikhatsky ในวันพุธที่ 20 เมษายน 2554 เวลา 7:32 น. (webkit.org)

จากโพสต์นั้น:


1
สะดวกกว่าการสร้างข้อยกเว้นเพียงเพื่อดูสแต็ก
Alex Churchill

ลิงก์เสีย แต่ฉันคิดว่ามีสำเนาที่นี่pjh0718.blogspot.com/2016/02/…
Stephen Turner

2

หากคุณต้องการติดตามสตริงกองฉันไปกับคำตอบของ insin: stacktrace.js หากคุณต้องการเข้าถึงส่วนต่างๆของ stacktrace (หมายเลขบรรทัดชื่อไฟล์ ฯลฯ ) stackinfoซึ่งจริงๆแล้วใช้ stacktrace.js ภายใต้ประทุน


0

ไม่จริงอย่างน้อยก็ไม่ง่าย

ใน IE คุณสามารถดีบักกระบวนการเบราว์เซอร์ด้วย MS Script Debugger (ซึ่งด้วยเหตุผลบางประการคือส่วนประกอบของ Office) หรือ Visual Studio จากนั้นคุณจะเห็นสแตกบนเบรกพอยต์


0

คุณต้องลองTraceKitไลบรารีโอเพนซอร์สที่ดีซึ่งพยายามสร้างสแต็กเทรซสำหรับข้อยกเว้น JavaScript ที่ไม่ได้จัดการในเบราว์เซอร์หลักทั้งหมด คุณสามารถอ่านเพิ่มเติมเกี่ยวกับ Tracekit

https://github.com/csnover/TraceKit/

PS : หากคุณกำลังมองหาบริการ (โดยใช้ Tracekit) ที่มี UI ที่ดีคุณสามารถลองAtatus

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.