Browse Source

Saner, yet still ugly, fix to displaying syntax errors

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
b57c1f51b9
  1. 22
      src/node.cc
  2. 1
      src/node.h
  3. 5
      src/node_script.cc

22
src/node.cc

@ -925,26 +925,24 @@ ssize_t DecodeWrite(char *buf,
return buflen; return buflen;
} }
// Extracts a C str from a V8 Utf8Value.
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<str conversion failed>";
}
static void ReportException(TryCatch &try_catch, bool show_line) { void DisplayExceptionLine (TryCatch &try_catch) {
HandleScope scope;
Handle<Message> message = try_catch.Message(); Handle<Message> message = try_catch.Message();
node::Stdio::DisableRawMode(STDIN_FILENO); node::Stdio::DisableRawMode(STDIN_FILENO);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (show_line && !message.IsEmpty()) { if (!message.IsEmpty()) {
// Print (filename):(line number): (message). // Print (filename):(line number): (message).
String::Utf8Value filename(message->GetScriptResourceName()); String::Utf8Value filename(message->GetScriptResourceName());
const char* filename_string = ToCString(filename); const char* filename_string = *filename;
int linenum = message->GetLineNumber(); int linenum = message->GetLineNumber();
fprintf(stderr, "%s:%i\n", filename_string, linenum); fprintf(stderr, "%s:%i\n", filename_string, linenum);
// Print line of source code. // Print line of source code.
String::Utf8Value sourceline(message->GetSourceLine()); String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = ToCString(sourceline); const char* sourceline_string = *sourceline;
// HACK HACK HACK // HACK HACK HACK
// //
@ -978,6 +976,14 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
}
static void ReportException(TryCatch &try_catch, bool show_line) {
HandleScope scope;
Handle<Message> message = try_catch.Message();
if (show_line) DisplayExceptionLine(try_catch);
String::Utf8Value trace(try_catch.StackTrace()); String::Utf8Value trace(try_catch.StackTrace());

1
src/node.h

@ -48,6 +48,7 @@ enum encoding {ASCII, UTF8, BASE64, BINARY};
enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v, enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v,
enum encoding _default = BINARY); enum encoding _default = BINARY);
void FatalException(v8::TryCatch &try_catch); void FatalException(v8::TryCatch &try_catch);
void DisplayExceptionLine(v8::TryCatch &try_catch); // hack
v8::Local<v8::Value> Encode(const void *buf, size_t len, v8::Local<v8::Value> Encode(const void *buf, size_t len,
enum encoding encoding = BINARY); enum encoding encoding = BINARY);

5
src/node_script.cc

@ -234,8 +234,9 @@ template <node::Script::EvalInputFlags iFlag,
script = oFlag == returnResult ? v8::Script::Compile(code, filename) script = oFlag == returnResult ? v8::Script::Compile(code, filename)
: v8::Script::New(code, filename); : v8::Script::New(code, filename);
if (script.IsEmpty()) { if (script.IsEmpty()) {
// FIXME HACK TO DISPLAY SYNTAX ERRORS. // FIXME UGLY HACK TO DISPLAY SYNTAX ERRORS.
FatalException(try_catch); DisplayExceptionLine(try_catch);
// Hack because I can't get a proper stacktrace on SyntaxError // Hack because I can't get a proper stacktrace on SyntaxError
return try_catch.ReThrow(); return try_catch.ReThrow();
} }

Loading…
Cancel
Save