static void error_reporter(JSContext *ctx, const char *message, JSErrorReport *report) { unsigned char *strict, *exception, *warning, *error; struct string msg; if (!init_string(&msg)) goto reported; strict = JSREPORT_IS_STRICT(report->flags) ? " strict" : ""; exception = JSREPORT_IS_EXCEPTION(report->flags) ? " exception" : ""; warning = JSREPORT_IS_WARNING(report->flags) ? " warning" : ""; error = !report->flags ? " error" : ""; add_format_to_string(&msg, "A client script raised the following%s%s%s%s", strict, exception, warning, error); add_to_string(&msg, ":\n\n"); add_to_string(&msg, message); if (report->linebuf && report->tokenptr) { int pos = report->tokenptr - report->linebuf; add_format_to_string(&msg, "\n\n%s\n.%*s^%*s.", report->linebuf, pos - 2, " ", strlen(report->linebuf) - pos - 1, " "); } alert_smjs_error(msg.source); done_string(&msg); reported: JS_ClearPendingException(ctx); }
//--------------------------------------------------------------------------- void printError(JSContext *cx, const char *message, JSErrorReport *report) { char *cstr = 0; int where; fprintf(stderr, "JSERROR: %s:%d:\n %s\n", (report->filename ? report->filename : "NULL"), report->lineno, message); if (report->linebuf) { cstr = (char *)report->linebuf; fprintf(stderr, " \"%s\"\n", cstr); if (report->tokenptr) { where = report->tokenptr - report->linebuf; /* Todo: 80 */ if ((where >= 0) && (where < 80)) { where += 6; while (--where > 0) fputc(' ', stderr); fprintf(stderr, "^\n"); } } } fprintf(stderr, " Flags:"); if (JSREPORT_IS_WARNING(report->flags)) fprintf(stderr, " WARNING"); if (JSREPORT_IS_EXCEPTION(report->flags)) fprintf(stderr, " EXCEPTION"); if (JSREPORT_IS_STRICT(report->flags)) fprintf(stderr, " STRICT"); fprintf(stderr, " (Error number: %d)\n", report->errorNumber); /* print exception object */ //printJSException(cx); }
static void error_reporter(JSContext *ctx, const char *message, JSErrorReport *report) { struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); struct session *ses = interpreter->vs->doc_view->session; struct terminal *term; unsigned char *strict, *exception, *warning, *error; struct string msg; assert(interpreter && interpreter->vs && interpreter->vs->doc_view && ses && ses->tab); if_assert_failed goto reported; term = ses->tab->term; #ifdef CONFIG_LEDS set_led_value(ses->status.ecmascript_led, 'J'); #endif if (!get_opt_bool("ecmascript.error_reporting", ses) || !init_string(&msg)) goto reported; strict = JSREPORT_IS_STRICT(report->flags) ? " strict" : ""; exception = JSREPORT_IS_EXCEPTION(report->flags) ? " exception" : ""; warning = JSREPORT_IS_WARNING(report->flags) ? " warning" : ""; error = !report->flags ? " error" : ""; add_format_to_string(&msg, _("A script embedded in the current " "document raised the following%s%s%s%s", term), strict, exception, warning, error); add_to_string(&msg, ":\n\n"); add_to_string(&msg, message); if (report->linebuf && report->tokenptr) { int pos = report->tokenptr - report->linebuf; add_format_to_string(&msg, "\n\n%s\n.%*s^%*s.", report->linebuf, pos - 2, " ", strlen(report->linebuf) - pos - 1, " "); } info_box(term, MSGBOX_FREE_TEXT, N_("JavaScript Error"), ALIGN_CENTER, msg.source); reported: /* Im clu'les. --pasky */ JS_ClearPendingException(ctx); }
/** Error Reporter for Spidermonkey. Used to report warnings and * uncaught exceptions. */ void gpsee_errorReporter(JSContext *cx, const char *message, JSErrorReport *report) { const char *ctmp; char er_filename[64]; char er_exception[16]; char er_warning[16]; char er_number[16]; char er_lineno[16]; char er_charno[16]; char er_pfx[64]; gpsee_runtime_t *grt = JS_GetRuntimePrivate(JS_GetRuntime(cx)); int printOnTTY = 0; if (grt->errorReport == er_none) return; if (!report) { gpsee_log(cx, GLOG_NOTICE, "JS error from unknown source: %s\n", message); return; } /* Conditionally ignore reported warnings. */ if (JSREPORT_IS_WARNING(report->flags)) { if (grt->errorReport & er_noWarnings) return; if (cfg_bool_value(cfg, "gpsee_report_warnings") == cfg_false) return; if (gpsee_verbosity(0) >= GPSEE_WARNING_OUTPUT_VERBOSITY) printOnTTY = 1 && gpsee_isatty(STDERR_FILENO); } else if (gpsee_verbosity(0) >= GPSEE_ERROR_OUTPUT_VERBOSITY) printOnTTY = 1 && gpsee_isatty(STDERR_FILENO); if (report->filename) { const char *bn = strrchr(report->filename, '/'); if (bn) bn += 1; else bn = report->filename; snprintf(er_filename, sizeof(er_filename), "in %s ", bn); } else er_filename[0] = (char)0; if (report->lineno) snprintf(er_lineno, sizeof(er_lineno), "line %u ", report->lineno); else er_lineno[0] = (char)0; if (report->tokenptr && report->linebuf) snprintf(er_charno, sizeof(er_charno), "ch %ld ", (long int)(report->tokenptr - report->linebuf)); else er_charno[0] = (char)0; er_warning[0] = (char)0; if (JSREPORT_IS_EXCEPTION(report->flags)) { snprintf(er_exception, sizeof(er_exception), "exception "); if (!grt->exitType) grt->exitType = et_exception; } else { er_exception[0] = (char)0; if (JSREPORT_IS_WARNING(report->flags)) snprintf(er_warning, sizeof(er_warning), "%swarning ", (JSREPORT_IS_STRICT(report->flags) ? "strict " : "")); } if (report->errorNumber) snprintf(er_number, sizeof(er_number), "#%i ", report->errorNumber); else er_number[0] = (char)0; snprintf(er_pfx, sizeof(er_pfx), "JS %s%s%s%s" "%s" "%s%s%s" "-", er_warning, er_exception, ((er_exception[0] || er_warning[0]) ? "" : "error "), er_number, /* error 69 */ er_filename, /* in myprog.js */ ((er_lineno[0] || er_charno[0]) ? "at " :""), er_lineno, er_charno /* at line 12, ch 34 */ ); /* embedded newlines -- log each separately */ while ((ctmp = strchr(message, '\n')) != 0) { char log_message[strlen(message)]; ctmp++; strncpy(log_message, message, ctmp-message); log_message[ctmp - message] = (char)0; output_message(cx, grt, er_pfx, log_message, report, printOnTTY); message = ctmp; memset(er_pfx, ' ', strlen(er_pfx)); er_pfx[0] = '|'; } output_message(cx, grt, er_pfx, message, report, printOnTTY); }
my_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) { int i, j, k, n; char *prefix = NULL, *tmp; const char *ctmp; if (!report) { fprintf(gErrFile, "%s\n", message); return; } /* Conditionally ignore reported warnings. */ if (JSREPORT_IS_WARNING(report->flags) && !reportWarnings) return; if (report->filename) prefix = JS_smprintf("%s:", report->filename); if (report->lineno) { tmp = prefix; prefix = JS_smprintf("%s%u: ", tmp ? tmp : "", report->lineno); JS_free(cx, tmp); } if (JSREPORT_IS_WARNING(report->flags)) { tmp = prefix; prefix = JS_smprintf("%s%swarning: ", tmp ? tmp : "", JSREPORT_IS_STRICT(report->flags) ? "strict " : ""); JS_free(cx, tmp); } /* embedded newlines -- argh! */ while ((ctmp = strchr(message, '\n')) != 0) { ctmp++; if (prefix) fputs(prefix, gErrFile); fwrite(message, 1, ctmp - message, gErrFile); message = ctmp; } /* If there were no filename or lineno, the prefix might be empty */ if (prefix) fputs(prefix, gErrFile); fputs(message, gErrFile); if (!report->linebuf) { fputc('\n', gErrFile); goto out; } fprintf(gErrFile, ":\n%s%s\n%s", prefix, report->linebuf, prefix); n = report->tokenptr - report->linebuf; for (i = j = 0; i < n; i++) { if (report->linebuf[i] == '\t') { for (k = (j + 8) & ~7; j < k; j++) { fputc('.', gErrFile); } continue; } fputc('.', gErrFile); j++; } fputs("^\n", gErrFile); out: if (!JSREPORT_IS_WARNING(report->flags)) gExitCode = EXITCODE_RUNTIME_ERROR; JS_free(cx, prefix); }
bool PrintError(JSContext *cx, FILE *file, const char *message, JSErrorReport *report, bool reportWarnings) { if (!report) { fprintf(file, "%s\n", message); fflush(file); return false; } /* Conditionally ignore reported warnings. */ if (JSREPORT_IS_WARNING(report->flags) && !reportWarnings) return false; char *prefix = NULL; if (report->filename) prefix = JS_smprintf("%s:", report->filename); if (report->lineno) { char *tmp = prefix; prefix = JS_smprintf("%s%u:%u ", tmp ? tmp : "", report->lineno, report->column); JS_free(cx, tmp); } if (JSREPORT_IS_WARNING(report->flags)) { char *tmp = prefix; prefix = JS_smprintf("%s%swarning: ", tmp ? tmp : "", JSREPORT_IS_STRICT(report->flags) ? "strict " : ""); JS_free(cx, tmp); } /* embedded newlines -- argh! */ const char *ctmp; while ((ctmp = strchr(message, '\n')) != 0) { ctmp++; if (prefix) fputs(prefix, file); fwrite(message, 1, ctmp - message, file); message = ctmp; } /* If there were no filename or lineno, the prefix might be empty */ if (prefix) fputs(prefix, file); fputs(message, file); if (report->linebuf) { /* report->linebuf usually ends with a newline. */ int n = strlen(report->linebuf); fprintf(file, ":\n%s%s%s%s", prefix, report->linebuf, (n > 0 && report->linebuf[n-1] == '\n') ? "" : "\n", prefix); n = report->tokenptr - report->linebuf; for (int i = 0, j = 0; i < n; i++) { if (report->linebuf[i] == '\t') { for (int k = (j + 8) & ~7; j < k; j++) { fputc('.', file); } continue; } fputc('.', file); j++; } fputc('^', file); } fputc('\n', file); fflush(file); JS_free(cx, prefix); return true; }
static void gjs_console_error_reporter(JSContext *cx, const char *message, JSErrorReport *report) { int i, j, k, n; char *prefix, *tmp; const char *ctmp; if (!report) { fprintf(stderr, "%s\n", message); return; } prefix = NULL; if (report->filename) prefix = g_strdup_printf("%s:", report->filename); if (report->lineno) { tmp = prefix; prefix = g_strdup_printf("%s%u: ", tmp ? tmp : "", report->lineno); g_free(tmp); } if (JSREPORT_IS_WARNING(report->flags)) { tmp = prefix; prefix = g_strdup_printf("%s%swarning: ", tmp ? tmp : "", JSREPORT_IS_STRICT(report->flags) ? "strict " : ""); g_free(tmp); } /* embedded newlines -- argh! */ while ((ctmp = strchr(message, '\n')) != NULL) { ctmp++; if (prefix) fputs(prefix, stderr); fwrite(message, 1, ctmp - message, stderr); message = ctmp; } /* If there were no filename or lineno, the prefix might be empty */ if (prefix) fputs(prefix, stderr); fputs(message, stderr); if (!report->linebuf) { fputc('\n', stderr); goto out; } /* report->linebuf usually ends with a newline. */ n = strlen(report->linebuf); fprintf(stderr, ":\n%s%s%s%s", prefix, report->linebuf, (n > 0 && report->linebuf[n-1] == '\n') ? "" : "\n", prefix); n = ((char*)report->tokenptr) - ((char*) report->linebuf); for (i = j = 0; i < n; i++) { if (report->linebuf[i] == '\t') { for (k = (j + 8) & ~7; j < k; j++) { fputc('.', stderr); } continue; } fputc('.', stderr); j++; } fputs("^\n", stderr); out: g_free(prefix); }