javascript 例外から fileName、Message、LineNumber などを取得する必要があります。次のコードを試しました。
try {
alertt("dddd");
} catch (e) {
console.log("ExceptionType: "+ e.name);
console.log("Message: "+ e.message);
console.log("Line No: "+ e.lineNumber);
}
Mozilla Firefox で次の結果が得られました
ExceptionType: ReferenceError
メッセージ: alertt が定義されてい
ません 行番号: 4
しかし、同じコードがGoogle Chrome、Internet Explorerで次の結果をもたらしました
ExceptionType: ReferenceError
メッセージ: alertt が定義されてい
ません 行番号: undefined
行番号を与えていません。この問題を解決するには?行番号を取得する別の方法はありますか?
試してみましたe.stackスタックトレースを文字列として返します。Google Chromeで次の出力が得られました
ReferenceError: alertt is not defined
at message (http://localhost/ems-test/js/test.js:4:4)
at HTMLDocument.<anonymous> (http://localhost/ems-test/js/test.js:14:2)
at c (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26036)
at Object.p.fireWith [as resolveWith] (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26840)
at Function.x.extend.ready (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:3305)
at HTMLDocument.q (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:717)
そしてfirefoxはこの結果を出しました
message@http://localhost/ems-test/js/test.js:4
@http://localhost/ems-test/js/test.js:14
x.Callbacks/c@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
.ready@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
q@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
どちらも文字列型の結果です。オブジェクトではありません。したがって、この巨大な文字列から行番号を抽出する必要があります。しかし、問題は両方の結果が同じではないことです。1 つは 1 行目に行番号を表示し、もう 1 つは 2 行目に行番号を表示します。したがって、この巨大な文字列から行番号を抽出するのは困難です。
スタックトレースをオブジェクトとして取得する方法はありますか?