Scala.js 堆栈跟踪

Scala.js stacktraces

当我的 Scala.js 代码抛出异常时,我对难以理解的堆栈跟踪感到沮丧。我以为我有一个使用 Javascript 库的解决方案(请参阅 ),但它经常出错。

you 如何从堆栈跟踪中提取含义(程序在哪里中断;它是如何到达那里的——就 Scala 代码而言),如下所示。还是我做错了什么甚至得到了未翻译的堆栈跟踪?

看看我之前在 youi 框架中写的这段代码:https://github.com/outr/youi/tree/e66dc36a12780fa8941152d07de9c3a52d28fc10/app/js/src/main/scala/io/youi/app/sourceMap

它用于将 JS 堆栈跟踪反转为 Scala 堆栈跟踪。在 youi 中,我将错误发送到服务器,这样我就可以监控浏览器发生的错误以及完整的回溯。

简要概述

  1. 来源-map.js

You need source-map.js to parse the js.map file that Scala.js generated when it compiled your code. See: https://github.com/mozilla/source-map

  1. 通过Ajax
  2. 加载js.map文件

The SourceMapConsumer needs a js.Object (JSON) of the js.map file. See https://github.com/outr/youi/blob/e66dc36a12780fa8941152d07de9c3a52d28fc10/app/js/src/main/scala/io/youi/app/sourceMap/ErrorTrace.scala#L58 for an example of loading via youi's Ajax features.

  1. 处理 Throwable

The trace represents line and columns in the JS file and you can pass that information to SourceMapConsumer to get the original Scala line numbers back (see SourceMapConsumer.originalPositionFor). See ErrorTrace.toCause (https://github.com/outr/youi/blob/e66dc36a12780fa8941152d07de9c3a52d28fc10/app/js/src/main/scala/io/youi/app/sourceMap/ErrorTrace.scala#L98) for an example iterating over the Throwable's trace elements.

  1. 处理错误

Now that you have the capacity to process JavaScript errors and convert them back to Scala traces, you need to actually receive the errors. If you want to globally handle uncaught errors set a function to window.onerror to capture errors. As of this writing, the function signature in Scala.js isn't ideal for handling all information, so in youi I use js.Dynamic to set it to what I need (see: https://github.com/outr/youi/blob/e66dc36a12780fa8941152d07de9c3a52d28fc10/app/js/src/main/scala/io/youi/app/ClientApplication.scala#L35). Also, notice that in ErrorTrace it supports multiple incoming types of errors (ErrorEvent, Throwable, and a more generic scenario). This is because in JavaScript the errors come in different ways based on what's happening. This is a fairly complex topic, and why I created this functionality in youi to simplify things.

不像我希望的那样简短的概述,但这不是一个容易解决的问题。 source-map GitHub 项目 (https://github.com/mozilla/source-map) 有不错的文档,是我最初用来编写解决方案的工具(添加了一些试验和错误)。如果我提供的信息不完整,我建议您在那里阅读更多内容,因为它应该提供大部分信息,并且可能有更好的解释。