簡體   English   中英

Javascript 控制台記錄擴展異常

[英]Javascript console logging extended exceptions

有些庫會引發擴展錯誤,例如 GraphQl Apollo 給出了一個。 嘗試將其直接記錄到控制台中只會給出隱藏詳細信息的“服務器錯誤”之類的一般標題。

有沒有辦法像其他對象一樣將錯誤對象打印到控制台中,讓您瀏覽它? 以下代碼是問題代碼的再現。

 class RError extends Error { constructor({name, message, cause}) { super(); this.name = name; this.message = message; this.cause = cause; } } function fail() { throw new RError({ name: 'BAR', message: 'I messed up.' }); } function failFurther() { try { fail(); } catch (err) { throw new RError({ name: 'FOO', message: 'Something went wrong.', cause: err }); } } try { failFurther(); } catch (err) { // Prints "FOO: Something went wrong" and stacktrace, hiding the cause console.error(err); // It's still available if you know the path console.error(err.cause); }

console.error()基本上是為了顯示錯誤信息。 如果想要其他一切,只需使用其他方法.log().dir().table() ...

 class RError extends Error { constructor({name, message, cause}) { super(); this.name = name; this.message = message; this.cause = cause; } } function fail() { throw new RError({ name: 'BAR', message: 'I messed up.' }); } function failFurther() { try { fail(); } catch (err) { throw new RError({ name: 'FOO', message: 'Something went wrong.', cause: err }); } } try { failFurther(); } catch (err) { console.log(err); console.dir(err); console.table(err); // Not implemented in Stack Overflow snippets }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM