mperdeck / jsnlog

JSNLog for .Net. Combines jsnlog.js client side logging with .Net server side component to receive log messages and pass them to the server side logging package. Configure loggers in web.config.
jsnlog.com
MIT License
190 stars 38 forks source link

JavaScript exception is caught but errorMsg is null... #168

Closed icnocop closed 6 years ago

icnocop commented 6 years ago

Hi.

Windows 10 64-bit Visual Studio 2015 ASP.NET MVC 5.2.4 jsnlog@2.26.2 Internet Explorer 11

_Layout.cshtml: @Html.Raw(JSNLog.JavascriptLogging.Configure())

web.config:

  <jsnlog>
    <!-- Create console appender -->
    <consoleAppender name="consoleAppender" />
    <!-- Send all log messages to console appender -->
    <logger level="TRACE" appenders="consoleAppender" />
  </jsnlog>

If a JavaScript error/exception occurs, I only see this in the browser's Console window: onerrorLogger: {"msg":"unhandledrejection","errorMsg":null}

I expected to see the JavaScript error message instead of null.

As a work-around, I can see the actual error message if I change line 1191 in jsnlog.ts (line 979 in jsnlog.js) as follows: "errorMsg": event.reason ? event.reason.message : event.message || null

Then the actual error message is displayed in the browser's Console window: onerrorLogger: {"msg":"unhandledrejection","errorMsg":"Unable to get property 'replace' of undefined or null reference\n Evaluating http://localhost/MyApp/Scripts/MyScript.js\n Loading Scripts/MyScript.js"}

I'm using SystemJS to load scripts.

For example: System.import("Scripts/MyScript.js");

SystemJs wraps exceptions and throws a new object (see https://github.com/systemjs/systemjs/issues/1309) which doesn't have a reason property.

Instead of changing jsnlog, do you recommend I catch the error and use JL explicitly instead of allowing the exception to bubble up to the window in this case?

For example:

System.import("Scripts/MyScript.js");
            .catch(function (error) {
                JL().fatalException("Exception", error);
            });

Thank you!

mperdeck commented 6 years ago

Thanks for the bug report and code fix! I'll incorporate your fix in the next release.

I don't know enough about your situation to say which of the options you mentioned is best. If you have few imports, I'd modify your imports. That way, you don't have to modify a third party library (jsnlog) and there is no risk that your modification is overwritten if you or somebody else reinstalls jsnlog. On the other hand, if you have many imports, modifying and maintaining them all will become a pain.

mperdeck commented 6 years ago

Your code fix has now been released, as part of 2.27.1.