나는 반주 동안이 문제와 싸우고 있었는데 내 문제는 다음과 같습니다.
Node.js 0.10.12를 사용하여 다음과 같이 JSON 파일을 구문 분석합니다.
var invalidJson = '{ this is bad }';
try {
JSON.parse( invalidJson );
}
catch (exc) {
console.log(exc.stack);
throw exc;
}
출력 :
SyntaxError: Unexpected token t
at Object.parse (native)
at Object.<anonymous> (test.js:7:10)
...
at node.js:901:3
test.js:12
throw exc;
..And then a duplicate of 'SyntaxError: Unexpected token t..' because I re-throw the exception
이제 다음을 수행 할 때 :
JSON.parse( invalidJson );
시도하지 않고 {} catch {}
이 오류가 발생합니다.
undefined:1
{ this is bad }
^
SyntaxError: Unexpected token t
at Object.parse (native)
at Object.<anonymous> (test.js:17:6)
...
at node.js:901:3
이 오류 메시지는 JSON 구문 분석이 중단 된 위치를 정확히 알려줍니다. JSON 파일이 크면 이러한 세부 정보 없이는 오류를 찾는 것이 사실상 불가능합니다.
사용자 지정 try {} catch {} 예외 처리기에서이 설명 오류 메시지를 어떻게 전달할 수 있습니까?
감사!
다음과 같이해도
$ node --builtins-in-stack-traces test.js
그리고 test.js :
var invalidJson = '{ this is bad }';
//Make err.stack return an array of CallSite objects instead of string
Error.prepareStackTrace = function(_, stack) {
return stack;
};
try {
JSON.parse( invalidJson );
}
catch (exc) {
var asd = exc.stack;
}
asd.forEach( function(v){
console.log((v.getMethodName() || v.getFunctionName()) +
":" + v.getLineNumber());
});
출력은 다음과 같습니다.
MakeGenericError:118
MakeSyntaxError:324
parse:56
null:9
_compile:456
.js:474
load:356
_load:312
runMain:497
startup:119
자바 스크립트에서 가능하지 않는 것, 당신은 이후 JS로 JSON 줄 번호 정보를 노출 버그 파일을해야 할 때 그들은 분명히 액세스하는 ^---
것을 . JSON은 손으로 작성해서는 안되므로 사용중인 JSON 직렬 변환기가 어떻게 든 손상되지 않는 한 구문 오류가 흔하지 않아야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다