1
0
Fork 0

Even cleaner SyntaxError stacktraces

Don't clutter stacktraces for Node compatibility at all. Just add lineNumber/columnNumber/fileName to the object.
These are non-standard but there is precedent for these names in Mozilla.
This commit is contained in:
Alan Plum 2017-01-27 05:53:39 +01:00
parent 88ac5777d6
commit c9b926916a
No known key found for this signature in database
GPG Key ID: 8ED72A9A323B6EFD
2 changed files with 24 additions and 17 deletions

View File

@ -83,17 +83,18 @@ exports.codeFrame = function (e, basePath, withColor = false) {
let err = e;
while (err) {
try {
if (err instanceof SyntaxError) {
const location = err.stack.split('\n')[0];
const [fileName, lineNo, colNo] = stackParser.extractLocation(location);
if (lineNo && colNo && (!basePath || fileName.indexOf(basePath) === 0)) {
ctx = {
fileName,
lineNumber: Number(lineNo),
columnNumber: Number(colNo)
};
break;
}
if (
err.fileName &&
err.lineNumber &&
err.columnNumber &&
(!basePath || err.fileName.indexOf(basePath) === 0)
) {
ctx = {
fileName: err.fileName,
lineNumber: Number(err.lineNumber),
columnNumber: Number(err.columnNumber)
};
break;
} else {
const stack = stackParser.parse(err);
for (const step of stack) {

View File

@ -412,8 +412,10 @@ static void JS_Parse(v8::FunctionCallbackInfo<v8::Value> const& args) {
if (tryCatch.HasCaught()) {
if (tryCatch.CanContinue()) {
v8::Local<v8::Object> exceptionObj = tryCatch.Exception().As<v8::Object>();
std::string stack = TRI_StringifyV8Exception(isolate, &tryCatch);
exceptionObj->Set(TRI_V8_ASCII_STRING("stack"), TRI_V8_STD_STRING(stack));
v8::Handle<v8::Message> message = tryCatch.Message();
exceptionObj->Set(TRI_V8_ASCII_STRING("lineNumber"), v8::Number::New(isolate, message->GetLineNumber()));
exceptionObj->Set(TRI_V8_ASCII_STRING("columnNumber"), v8::Number::New(isolate, message->GetStartColumn()));
exceptionObj->Set(TRI_V8_ASCII_STRING("fileName"), filename->ToString());
tryCatch.ReThrow();
return;
} else {
@ -479,8 +481,10 @@ static void JS_ParseFile(v8::FunctionCallbackInfo<v8::Value> const& args) {
if (tryCatch.HasCaught()) {
if (tryCatch.CanContinue()) {
v8::Local<v8::Object> exceptionObj = tryCatch.Exception().As<v8::Object>();
std::string stack = TRI_StringifyV8Exception(isolate, &tryCatch);
exceptionObj->Set(TRI_V8_ASCII_STRING("stack"), TRI_V8_STD_STRING(stack));
v8::Handle<v8::Message> message = tryCatch.Message();
exceptionObj->Set(TRI_V8_ASCII_STRING("lineNumber"), v8::Number::New(isolate, message->GetLineNumber()));
exceptionObj->Set(TRI_V8_ASCII_STRING("columnNumber"), v8::Number::New(isolate, message->GetStartColumn()));
exceptionObj->Set(TRI_V8_ASCII_STRING("fileName"), args[0]);
tryCatch.ReThrow();
return;
} else {
@ -1019,8 +1023,10 @@ static void JS_Execute(v8::FunctionCallbackInfo<v8::Value> const& args) {
if (tryCatch.CanContinue()) {
v8::Local<v8::Object> exceptionObj = tryCatch.Exception().As<v8::Object>();
std::string stack = TRI_StringifyV8Exception(isolate, &tryCatch);
exceptionObj->Set(TRI_V8_ASCII_STRING("stack"), TRI_V8_STD_STRING(stack));
v8::Handle<v8::Message> message = tryCatch.Message();
exceptionObj->Set(TRI_V8_ASCII_STRING("lineNumber"), v8::Number::New(isolate, message->GetLineNumber()));
exceptionObj->Set(TRI_V8_ASCII_STRING("columnNumber"), v8::Number::New(isolate, message->GetStartColumn()));
exceptionObj->Set(TRI_V8_ASCII_STRING("fileName"), filename->ToString());
tryCatch.ReThrow();
return;
} else {