Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes JSStackTrace on Safari/Firefox #1939

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 52 additions & 14 deletions korge-foundation/src/js/korlibs/js/JSStackTrace.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,68 @@
package korlibs.js

data class JSStackTrace(val message: String, val entries: List<Entry>) {
data class Entry(val method: String, val file: String, val line: Int, val column: Int)
data class Entry(val method: String, val file: String, val line: Int, val column: Int = -1)

companion object {
val stackLine = Regex("^\\s+at\\s*(.*?)?\\s+\\(?(.*?):(\\d+):(\\d+)\\)?\$")

operator fun invoke(): JSStackTrace = current()

fun parse(stack: String): JSStackTrace {
fun parse(stack: String, message: String? = null): JSStackTrace {
val entries = arrayListOf<Entry>()
var message = arrayListOf<String>()
for (line in stack.lines()) {
val stack = stackLine.matchEntire(line)
if (stack != null) {
val (all, method, file, line, column) = stack!!.groupValues
entries += Entry(method, file, line.toInt(), column.toInt())
} else {
if (entries.isEmpty()) line.trim().takeIf { it.isNotBlank() }?.let { message += it }
var messageLines = arrayListOf<String>()
var isChrome = false
for ((index, strLine) in stack.lines().withIndex()) {
val strLine = strLine.trimEnd()
if (strLine.isEmpty() && !isChrome) continue
// Chrome first line
when {
(strLine.startsWith("Error: ") || strLine == "Error") && index == 0 -> {
isChrome = true
messageLines.add(strLine.substring(7))
}
strLine.startsWith(" at ") -> {
val part = strLine.substring(7).trimEnd(')')
val column = part.substringAfterLast(':')
val part0 = part.substringBeforeLast(':')
val line = part0.substringAfterLast(':')
val part1 = part0.substringBeforeLast(':')
val (method, file) = if (part1.contains('(')) {
part1.split("(").map { it.trim() }
} else {
listOf("", part1.trim())
}
entries += Entry(method, file, line.toIntOrNull() ?: -1, column.toIntOrNull() ?: -1)
}
isChrome -> {
messageLines.add(strLine)
}
else -> {
var cline = strLine
val numParts = arrayListOf<Int>()
for (n in 0 until 2) {
cline = Regex("^(.*):(\\d+)$").replace(cline) {
numParts.add(0, it.groupValues[2].toIntOrNull() ?: -1)
it.groupValues[1]
}
}
val file = cline.substringAfterLast('@')
val method = cline.substringBeforeLast('@')
val line = numParts.firstOrNull() ?: -1
val column = if (numParts.size >= 2) numParts.lastOrNull() ?: -1 else -1
entries += Entry(method, file, line, column)
}
}

//println("STACK: ${stack!!.groupValues}")
}
return JSStackTrace(message.joinToString("\n"), entries)
if (!isChrome) {
messageLines = arrayListOf(message ?: "")
}
if (entries.isEmpty()) {
entries.add(Entry("<unknown>", "<unknown>", -1))
}
return JSStackTrace(messageLines.joinToString("\n"), entries)
}
fun from(e: Exception): JSStackTrace = parse(e.stackTraceToString())
fun from(e: Exception): JSStackTrace = parse(e.stackTraceToString(), e.message)
fun current(): JSStackTrace = from(Exception(""))
}
}
114 changes: 114 additions & 0 deletions korge-foundation/test/js/korlibs/js/JSStackTraceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package korlibs.js

import kotlin.test.*

class JSStackTraceTest {
@Test
fun testEmpty() {
val stack = JSStackTrace.parse("")
assertEquals(JSStackTrace("", listOf(
JSStackTrace.Entry("<unknown>", "<unknown>", -1)
)), stack)
}

@Test
fun test() {
//new Error().stack
//'Error\n at <anonymous>:1:1'
val stack = JSStackTrace.parse("""
Error
at <anonymous>:1:1
""".trimIndent())
assertEquals(JSStackTrace("", listOf(
JSStackTrace.Entry("", "<anonymous>", 1, 1)
)), stack)
}

@Test
fun testChromeMultiLineMessage() {
//new Error("hello\nworld\n").stack
//'Error: hello\nworld\n\n at <anonymous>:1:1'
val stack = JSStackTrace.parse("Error: hello\nworld\n\n at <anonymous>:1:1")
assertEquals(JSStackTrace("hello\nworld\n", listOf(
JSStackTrace.Entry("", "<anonymous>", 1, 1)
)), stack)
}


@Test
fun testChrome() {
val stack = JSStackTrace.parse("""
Error: test
at a (demo.html:3:14)
at b (demo.html:6:2)
at demo.html:8:1
""".trimIndent(), "test")
assertEquals(JSStackTrace("test", listOf(
JSStackTrace.Entry("a", "demo.html", 3, 14),
JSStackTrace.Entry("b", "demo.html", 6, 2),
JSStackTrace.Entry("", "demo.html", 8, 1),
)), stack)
}

@Test
fun testFirefox() {
val stack = JSStackTrace.parse("""
a@http://127.0.0.1:8080/demo.html:3:14
b@http://127.0.0.1:8080/demo.html:6:2
@http://127.0.0.1:8080/demo.html:8:1
""".trimIndent(), "test")
assertEquals(JSStackTrace("test", listOf(
JSStackTrace.Entry("a", "http://127.0.0.1:8080/demo.html", 3, 14),
JSStackTrace.Entry("b", "http://127.0.0.1:8080/demo.html", 6, 2),
JSStackTrace.Entry("", "http://127.0.0.1:8080/demo.html", 8, 1),
)), stack)
}

@Test
fun testFirefox14_29() {
val stack = JSStackTrace.parse("""
a@http://127.0.0.1:8080/demo.html:3
b@http://127.0.0.1:8080/demo.html:6
@http://127.0.0.1:8080/demo.html:8
""".trimIndent(), "test")
assertEquals(JSStackTrace("test", listOf(
JSStackTrace.Entry("a", "http://127.0.0.1:8080/demo.html", 3),
JSStackTrace.Entry("b", "http://127.0.0.1:8080/demo.html", 6),
JSStackTrace.Entry("", "http://127.0.0.1:8080/demo.html", 8),
)), stack)
}

@Test
fun testFirefox1_13() {
val stack = JSStackTrace.parse("""
Error("myError")@:0
trace()@file:///C:/example.html:9
b(3,4,"\n\n",(void 0),[object Object])@file:///C:/example.html:16
a("first call, firstarg")@file:///C:/example.html:19
@file:///C:/example.html:21
""".trimIndent(), "test")
assertEquals(JSStackTrace("test", listOf(
JSStackTrace.Entry("Error(\"myError\")", "", 0),
JSStackTrace.Entry("trace()", "file:///C:/example.html", 9),
JSStackTrace.Entry("b(3,4,\"\\n\\n\",(void 0),[object Object])", "file:///C:/example.html", 16),
JSStackTrace.Entry("a(\"first call, firstarg\")", "file:///C:/example.html", 19),
JSStackTrace.Entry("", "file:///C:/example.html", 21),
)), stack)

}

@Test
fun testSafari() {
val stack = JSStackTrace.parse("""
a@http://127.0.0.1:8080/demo.html:3:23
b@http://127.0.0.1:8080/demo.html:6:3
global code@http://127.0.0.1:8080/demo.html:8:2
""".trimIndent(), "test")
assertEquals(JSStackTrace("test", listOf(
JSStackTrace.Entry("a", "http://127.0.0.1:8080/demo.html", 3, 23),
JSStackTrace.Entry("b", "http://127.0.0.1:8080/demo.html", 6, 3),
JSStackTrace.Entry("global code", "http://127.0.0.1:8080/demo.html", 8, 2),
)), stack)

}
}