Skip to content

The only tool you need to know what is happening and how to fix it.

source map resolver bug fix

ddux59a6f8764465
diff --git a/backend/app/services/sourcemap_resolver.go b/backend/app/services/sourcemap_resolver.go
index e726e59b..06e98fd0 100644
--- a/backend/app/services/sourcemap_resolver.go
+++ b/backend/app/services/sourcemap_resolver.go
@@ -20,7 +20,16 @@ import (
)
var stackFrameRe = regexp.MustCompile(`^(\s{4})(.+):(\d+):(\d+)$`)
-var jsFuncDeclRe = regexp.MustCompile(`(?:function\s+(\w+)|(?:const|let|var)\s+(\w+)\s*=|^\s*(?:async\s+)?(\w+)\s*\()`)
+var jsFuncDeclRe = regexp.MustCompile(
+ `(?:(?:export\s+(?:default\s+)?)?function\s+(\w+)` +
+ `|(?:const|let|var)\s+(\w+)\s*=` +
+ `|^\s*(?:async\s+)?(\w+)\s*\([^)]*\)\s*\{)`,
+)
+
+var jsControlFlowKeywords = map[string]bool{
+ "if": true, "for": true, "while": true, "switch": true,
+ "catch": true, "return": true, "throw": true, "else": true,
+}
func ResolveStackTrace(ctx context.Context, projectId uuid.UUID, appVersion string, stackTrace string) string {
sourceMaps, err := pgdb.ExecuteTransaction(func(tx *sql.Tx) ([]*models.SourceMap, error) {
@@ -89,9 +98,9 @@ func ResolveStackTrace(ctx context.Context, projectId uuid.UUID, appVersion stri
continue
}
- if origName == "" {
- if content := consumer.SourceContent(origFile); content != "" {
- origName = extractFunctionName(content, origLine)
+ if content := consumer.SourceContent(origFile); content != "" {
+ if extracted := extractFunctionName(content, origLine); extracted != "" {
+ origName = extracted
}
}
@@ -158,7 +167,7 @@ func extractFunctionName(sourceContent string, line int) string {
matches := jsFuncDeclRe.FindStringSubmatch(lines[i])
if matches != nil {
for _, m := range matches[1:] {
- if m != "" {
+ if m != "" && !jsControlFlowKeywords[m] {
return m
}
}
diff --git a/backend/app/services/sourcemap_resolver_test.go b/backend/app/services/sourcemap_resolver_test.go
new file mode 100644
index 00000000..f91d41f2
--- /dev/null
+++ b/backend/app/services/sourcemap_resolver_test.go
@@ -0,0 +1,129 @@
+package services
+
+import "testing"
+
+func TestExtractFunctionName(t *testing.T) {
+ tests := []struct {
+ name string
+ content string
+ line int
+ expected string
+ }{
+ {
+ name: "arrow function above throw",
+ content: `import React from 'react';
+const handleEventError = () => {
+ try {
+ log("Triggering error...")
+ throw new Error("Test error")
+ } catch (e) {
+ throw e
+ }
+}`,
+ line: 5,
+ expected: "handleEventError",
+ },
+ {
+ name: "regular function declaration",
+ content: `function processData(input) {
+ transform(input)
+ return input
+}`,
+ line: 3,
+ expected: "processData",
+ },
+ {
+ name: "export function",
+ content: `export function handleError(err) {
+ console.error(err)
+ reportError(err)
+}`,
+ line: 3,
+ expected: "handleError",
+ },
+ {
+ name: "export default function",
+ content: `export default function App() {
+ return <div>Hello</div>
+}`,
+ line: 2,
+ expected: "App",
+ },
+ {
+ name: "class method",
+ content: `class MyComponent {
+ handleClick(event) {
+ event.preventDefault()
+ this.setState({ clicked: true })
+ }
+}`,
+ line: 4,
+ expected: "handleClick",
+ },
+ {
+ name: "async method",
+ content: `class ApiClient {
+ async fetchData(url) {
+ await fetch(url)
+ return null
+ }
+}`,
+ line: 4,
+ expected: "fetchData",
+ },
+ {
+ name: "skips function calls",
+ content: `const fn = () => {
+ log("hello")
+ doSomething()
+ throw new Error("fail")
+}`,
+ line: 4,
+ expected: "fn",
+ },
+ {
+ name: "skips control flow keywords",
+ content: `function foo() {
+ if (x) {
+ doSomething()
+ }
+}`,
+ line: 3,
+ expected: "foo",
+ },
+ {
+ name: "returns empty when no function found",
+ content: `x = 1
+y = 2
+z = x + y`,
+ line: 3,
+ expected: "",
+ },
+ {
+ name: "var declaration with function expression",
+ content: `var onSubmit = function(e) {
+ e.preventDefault()
+ submit(data)
+}`,
+ line: 3,
+ expected: "onSubmit",
+ },
+ {
+ name: "let declaration with arrow",
+ content: `let compute = (a, b) => {
+ return a + b
+}`,
+ line: 2,
+ expected: "compute",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := extractFunctionName(tt.content, tt.line)
+ if got != tt.expected {
+ t.Errorf("extractFunctionName() = %q, want %q", got, tt.expected)
+ }
+ })
+ }
+}