Permalink
Browse files

core, refactor: more powerful error system (#378)

  • Loading branch information...
ngot authored and xicilion committed Nov 13, 2017
1 parent f5d12cd commit b41dfd973e13d5f445eb91e95976820105463b46
View
@@ -8,29 +8,31 @@
#ifndef TEXTCOLOR_H_
#define TEXTCOLOR_H_
#include <exlib/include/qstring.h>
namespace fibjs {
#define COLOR_RESET "\x1b[0m"
#define COLOR_BLACK "\x1b[0;30m" /* Black */
#define COLOR_RED "\x1b[0;31m" /* Red */
#define COLOR_GREEN "\x1b[0;32m" /* Green */
#define COLOR_YELLOW "\x1b[0;33m" /* Yellow */
#define COLOR_BLUE "\x1b[0;34m" /* Blue */
#define COLOR_MAGENTA "\x1b[0;35m" /* Magenta */
#define COLOR_CYAN "\x1b[0;36m" /* Cyan */
#define COLOR_WHITE "\x1b[0;37m" /* White */
#define COLOR_NORMAL "\x1b[0;39m" /* Normal */
#define COLOR_GREY "\x1B[90m" /* Grey */
extern exlib::string COLOR_RESET;
extern exlib::string COLOR_BLACK;
extern exlib::string COLOR_RED;
extern exlib::string COLOR_GREEN;
extern exlib::string COLOR_YELLOW;
extern exlib::string COLOR_BLUE;
extern exlib::string COLOR_MAGENTA;
extern exlib::string COLOR_CYAN;
extern exlib::string COLOR_WHITE;
extern exlib::string COLOR_NORMAL;
extern exlib::string COLOR_GREY;
#define COLOR_LIGHTRED "\x1b[1;31m" /* Red */
#define COLOR_LIGHTGREEN "\x1b[1;32m" /* Green */
#define COLOR_LIGHTYELLOW "\x1b[1;33m" /* Yellow */
#define COLOR_LIGHTBLUE "\x1b[1;34m" /* Blue */
#define COLOR_LIGHTMAGENTA "\x1b[1;35m" /* Magenta */
#define COLOR_LIGHTCYAN "\x1b[1;36m" /* Cyan */
#define COLOR_LIGHTWHITE "\x1b[1;37m" /* White */
extern exlib::string COLOR_LIGHTRED;
extern exlib::string COLOR_LIGHTGREEN;
extern exlib::string COLOR_LIGHTYELLOW;
extern exlib::string COLOR_LIGHTBLUE;
extern exlib::string COLOR_LIGHTMAGENTA;
extern exlib::string COLOR_LIGHTCYAN;
extern exlib::string COLOR_LIGHTWHITE;
#define COLOR_TITLE "\x1B[1;39m"
extern exlib::string COLOR_TITLE;
}
#endif /* STAT_H_ */
#endif /* STAT_H_ */
View
@@ -1177,6 +1177,11 @@ inline exlib::string niceSize(intptr_t sz)
void flushLog();
// Converts a V8 value to a C string.
const auto ToCString = [](const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
};
class save_method_name {
public:
save_method_name(const char* name);
View
@@ -30,6 +30,7 @@ void init_aio();
void init_fs();
void init_fiber();
void init_signal();
void init_color();
void options(int32_t& pos, char* argv[]);
result_t ifZipFile(exlib::string filename, bool& retVal);
@@ -60,6 +61,7 @@ void init()
init_fs();
init_fiber();
init_signal();
init_color();
srand((unsigned int)time(0));
View
@@ -115,61 +115,87 @@ v8::Local<v8::Value> ThrowResult(result_t hr)
return isolate->m_isolate->ThrowException(e);
}
inline const char* ToCString(const v8::String::Utf8Value& value)
{
return *value ? *value : "<string conversion failed>";
}
exlib::string GetException(TryCatch& try_catch, result_t hr)
{
Isolate* isolate = Isolate::current();
v8::HandleScope handle_scope(isolate->m_isolate);
v8::Local<v8::Context> context = isolate->m_isolate->GetCurrentContext();
if (try_catch.HasCaught()) {
v8::String::Utf8Value exception(try_catch.Exception());
v8::Local<v8::Value> err = try_catch.Exception();
v8::Local<v8::Object> err_obj = err.As<v8::Object>();
v8::String::Utf8Value exception(err_obj);
v8::Local<v8::Message> message = try_catch.Message();
if (message.IsEmpty())
return ToCString(exception);
else {
v8::Local<v8::Value> trace_value = try_catch.StackTrace();
if (!IsEmpty(trace_value)) {
v8::String::Utf8Value stack_trace(trace_value);
const char* s = ToCString(stack_trace);
const char* s1 = qstrchr(s, '\n');
while (s1 && qstrcmp(s1 + 1, " at ", 7))
s1 = qstrchr(s1 + 1, '\n');
if (s1)
return exlib::string(ToCString(exception)) + s1;
exlib::string strError;
v8::String::Utf8Value filename(message->GetScriptResourceName());
strError.append(ToCString(filename));
int32_t lineNumber = message->GetLineNumber();
if (lineNumber > 0) {
char numStr[32];
strError.append(1, ':');
sprintf(numStr, "%d", lineNumber);
strError.append(numStr);
strError.append(1, ':');
sprintf(numStr, "%d", message->GetStartColumn() + 1);
strError.append(numStr);
}
strError.append("\n");
v8::Local<v8::String> sourceline;
if (message->GetSourceLine(context).ToLocal(&sourceline)) {
// Print line of source code.
v8::String::Utf8Value sourcelinevalue(isolate->m_isolate, sourceline);
const char* sourceline_string = ToCString(sourcelinevalue);
strError.append(sourceline_string);
strError.append("\n");
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn(context).FromJust();
for (int i = 0; i < start; i++) {
if (sourceline_string[i] == '\0') {
break;
}
strError.append((sourceline_string[i] == '\t') ? "\t" : " ");
}
int end = message->GetEndColumn(context).FromJust();
for (int i = start; i < end; i++) {
strError.append("^");
}
strError.append("\n");
}
exlib::string strError;
v8::String::Utf8Value filename(message->GetScriptResourceName());
if (err_obj->IsNativeError()) {
v8::Local<v8::Value> stack_trace_string;
try_catch.StackTrace(context).ToLocal(&stack_trace_string);
v8::String::Utf8Value stack_trace(isolate->m_isolate,
stack_trace_string.As<v8::String>());
strError.append(ToCString(stack_trace));
} else {
v8::Local<v8::Value> message;
v8::Local<v8::Value> name;
if (qstrcmp(ToCString(exception), "SyntaxError: ", 13)) {
strError.append(ToCString(exception));
strError.append("\n at ");
} else {
strError.append((ToCString(exception) + 13));
strError.append("\n at ");
if (err->IsObject()) {
message = err_obj->Get(isolate->NewString("message"));
name = err_obj->Get(isolate->NewString("name"));
}
strError.append(ToCString(filename));
int32_t lineNumber = message->GetLineNumber();
if (lineNumber > 0) {
char numStr[32];
strError.append(1, ':');
sprintf(numStr, "%d", lineNumber);
strError.append(numStr);
strError.append(1, ':');
sprintf(numStr, "%d", message->GetStartColumn() + 1);
strError.append(numStr);
if (message.IsEmpty() || message->IsUndefined() || name.IsEmpty() || name->IsUndefined()) {
// Not an error object. Just print as-is.
v8::String::Utf8Value message(err);
strError.append(*message ? *message : "<toString() threw exception>");
strError.append("\n");
} else {
v8::String::Utf8Value message_string(isolate->m_isolate, message);
v8::String::Utf8Value name_string(isolate->m_isolate, name);
strError.append(ToCString(name_string));
strError.append(": ");
strError.append(ToCString(message_string));
}
return strError;
}
return strError;
} else if (hr < 0)
return getResultMessage(hr);
@@ -184,32 +210,7 @@ result_t throwSyntaxError(TryCatch& try_catch)
if (message.IsEmpty())
ThrowError(ToCString(exception));
else {
exlib::string strError;
v8::String::Utf8Value filename(message->GetScriptResourceName());
if (qstrcmp(ToCString(exception), "SyntaxError: ", 13)) {
strError.append(ToCString(exception));
strError.append("\n at ");
} else {
strError.append((ToCString(exception) + 13));
strError.append("\n at ");
}
strError.append(ToCString(filename));
int32_t lineNumber = message->GetLineNumber();
if (lineNumber > 0) {
char numStr[32];
strError.append(1, ':');
sprintf(numStr, "%d", lineNumber);
strError.append(numStr);
strError.append(1, ':');
sprintf(numStr, "%d", message->GetStartColumn() + 1);
strError.append(numStr);
}
return Runtime::setError(strError);
return Runtime::setError(GetException(try_catch, 0));
}
return CALL_E_JAVASCRIPT;
@@ -11,6 +11,7 @@
#include "ifs/encoding.h"
#include "ifs/process.h"
#include "ifs/util.h"
#include "ifs/tty.h"
#include "v8_api.h"
#include <map>
#include "console.h"
@@ -52,6 +53,57 @@ inline int64_t Ticks()
namespace fibjs {
exlib::string COLOR_RESET = "";
exlib::string COLOR_BLACK = "";
exlib::string COLOR_RED = "";
exlib::string COLOR_GREEN = "";
exlib::string COLOR_YELLOW = "";
exlib::string COLOR_BLUE = "";
exlib::string COLOR_MAGENTA = "";
exlib::string COLOR_CYAN = "";
exlib::string COLOR_WHITE = "";
exlib::string COLOR_NORMAL = "";
exlib::string COLOR_GREY = "";
exlib::string COLOR_LIGHTRED = "";
exlib::string COLOR_LIGHTGREEN = "";
exlib::string COLOR_LIGHTYELLOW = "";
exlib::string COLOR_LIGHTBLUE = "";
exlib::string COLOR_LIGHTMAGENTA = "";
exlib::string COLOR_LIGHTCYAN = "";
exlib::string COLOR_LIGHTWHITE = "";
exlib::string COLOR_TITLE = "";
void init_color()
{
bool isatty = false;
tty_base::isatty(1, isatty);
if (isatty) {
COLOR_RESET = "\x1b[0m";
COLOR_BLACK = "\x1b[0;30m"; /* Black */
COLOR_RED = "\x1b[0;31m"; /* Red */
COLOR_GREEN = "\x1b[0;32m"; /* Green */
COLOR_YELLOW = "\x1b[0;33m"; /* Yellow */
COLOR_BLUE = "\x1b[0;34m"; /* Blue */
COLOR_MAGENTA = "\x1b[0;35m"; /* Magenta */
COLOR_CYAN = "\x1b[0;36m"; /* Cyan */
COLOR_WHITE = "\x1b[0;37m"; /* White */
COLOR_NORMAL = "\x1b[0;39m"; /* Normal */
COLOR_GREY = "\x1B[90m"; /* Grey */
COLOR_LIGHTRED = "\x1b[1;31m"; /* Red */
COLOR_LIGHTGREEN = "\x1b[1;32m"; /* Green */
COLOR_LIGHTYELLOW = "\x1b[1;33m"; /* Yellow */
COLOR_LIGHTBLUE = "\x1b[1;34m"; /* Blue */
COLOR_LIGHTMAGENTA = "\x1b[1;35m"; /* Magenta */
COLOR_LIGHTCYAN = "\x1b[1;36m"; /* Cyan */
COLOR_LIGHTWHITE = "\x1b[1;37m"; /* White */
COLOR_TITLE = "\x1B[1;39m";
}
}
extern std_logger* s_std;
void asyncLog(int32_t priority, exlib::string msg);
@@ -9,19 +9,14 @@
#include "encoding_bson.h"
#include "ifs/encoding.h"
#include "Buffer.h"
#include "utils.h"
#include "MongoID.h"
#include "Int64.h"
namespace fibjs {
DECLARE_MODULE(bson);
inline const char*
ToCString(const v8::String::Utf8Value& value)
{
return *value ? *value : "<string conversion failed>";
}
void encodeArray(Isolate* isolate, bson* bb, const char* name, v8::Local<v8::Value> element);
bool encodeObject(Isolate* isolate, bson* bb, const char* name, v8::Local<v8::Value> element,
bool doJson);
@@ -143,8 +138,7 @@ bool encodeObject(Isolate* isolate, bson* bb, const char* name, v8::Local<v8::Va
if (!IsEmpty(jsonFun) && jsonFun->IsFunction()) {
v8::Local<v8::Value> p = isolate->NewString(name ? name : "");
v8::Local<v8::Value> element1 = v8::Local<v8::Function>::Cast(
jsonFun)->Call(object, 1, &p);
v8::Local<v8::Value> element1 = v8::Local<v8::Function>::Cast(jsonFun)->Call(object, 1, &p);
if (name) {
encodeValue(isolate, bb, name, element1, false);
View
@@ -261,7 +261,7 @@ class _case : public obj_base {
} else {
double n = d2.diff(d1);
str.append(logger::notice() + "\xe2\x88\x9a " COLOR_RESET);
str.append(logger::notice() + "\xe2\x88\x9a " + COLOR_RESET);
str.append(p1->m_name);
if (n > s_slow / 2) {
sprintf(buf, " (%dms) ", (int32_t)n);
@@ -316,11 +316,11 @@ class _case : public obj_base {
da2.now();
if (errcnt == 0) {
sprintf(buf,
(logger::notice() + " \xe2\x88\x9a %d tests completed" COLOR_RESET " (%dms)").c_str(),
(logger::notice() + " \xe2\x88\x9a %d tests completed" + COLOR_RESET + " (%dms)").c_str(),
cnt, (int32_t)da2.diff(da1));
asyncLog(console_base::_INFO, buf);
} else {
sprintf(buf, (logger::error() + " × %d of %d tests failed" COLOR_RESET " (%dms)").c_str(),
sprintf(buf, (logger::error() + " × %d of %d tests failed" + COLOR_RESET + " (%dms)").c_str(),
errcnt, cnt, (int32_t)da2.diff(da1));
asyncLog(console_base::_ERROR, buf);
}
Oops, something went wrong.

0 comments on commit b41dfd9

Please sign in to comment.