Skip to content

Commit

Permalink
refactor: Update StackTrace function to accept rootMapping as an inte…
Browse files Browse the repository at this point in the history
…rface
  • Loading branch information
trheyi committed Aug 1, 2024
1 parent b79f046 commit eed4aaf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
23 changes: 15 additions & 8 deletions runtime/v8/sourcemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 66,7 @@ func clearSourceMaps() {
}

// StackTrace get the stack trace
func StackTrace(jserr *v8go.JSError, rootMapping map[string]string) string {
func StackTrace(jserr *v8go.JSError, rootMapping interface{}) string {

// Production mode will not show the stack trace
if runtimeOption.Debug == false {
Expand All @@ -92,7 92,7 @@ func (entry *StackLogEntry) String() string {
}

// String the stack log entry list to string
func (list StackLogEntryList) String(rootMapping map[string]string) (string, error) {
func (list StackLogEntryList) String(rootMapping interface{}) (string, error) {
if len(list) == 0 {
return "", fmt.Errorf("StackLogEntryList.String(), empty list")
}
Expand Down Expand Up @@ -229,20 229,27 @@ func parseStackTrace(trace string) StackLogEntryList {
return res
}

func fmtFilePath(file string, rootMapping map[string]string) string {
func fmtFilePath(file string, rootMapping interface{}) string {
file = strings.ReplaceAll(file, ".." string(os.PathSeparator), "")
if !strings.HasPrefix(file, string(os.PathSeparator)) {
file = string(os.PathSeparator) file
}

file = strings.TrimPrefix(file, application.App.Root())

if rootMapping != nil {
for name, mappping := range rootMapping {
if strings.HasPrefix(file, name) {
file = mappping strings.TrimPrefix(file, name)
break
switch mapping := rootMapping.(type) {
case map[string]string:
for name, mappping := range mapping {
if strings.HasPrefix(file, name) {
file = mappping strings.TrimPrefix(file, name)
break
}
}
break

case func(string) string:
file = mapping(file)
break
}
}
return file
Expand Down
26 changes: 25 additions & 1 deletion runtime/v8/sourcemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ package v8

import (
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -61,10 62,33 @@ func TestStackTrace(t *testing.T) {
t.Fatal("error is not a JSError")
}

trace := StackTrace(e, map[string]string{"/scripts": "/iscripts"})
trace := StackTrace(e, nil)
assert.NotEmpty(t, trace)
assert.Contains(t, trace, "Exception|400: Error occurred")
assert.Contains(t, trace, "/scripts/runtime/ts/page.ts:12:2")
assert.Contains(t, trace, "/scripts/runtime/ts/lib/bar.ts:7:2")
assert.Contains(t, trace, "/scripts/runtime/ts/lib/err.ts:8:10")

// with source root
trace = StackTrace(e, map[string]string{"/scripts": "/iscripts"})
assert.NotEmpty(t, trace)
assert.Contains(t, trace, "Exception|400: Error occurred")
assert.Contains(t, trace, "/iscripts/runtime/ts/page.ts:12:2")
assert.Contains(t, trace, "/iscripts/runtime/ts/lib/bar.ts:7:2")
assert.Contains(t, trace, "/iscripts/runtime/ts/lib/err.ts:8:10")

// with source root function
replace := func(file string) string {
if strings.HasPrefix(file, "/scripts") {
return strings.Replace(file, "/scripts", "/fscripts", 1)
}
return file
}

trace = StackTrace(e, replace)
assert.NotEmpty(t, trace)
assert.Contains(t, trace, "Exception|400: Error occurred")
assert.Contains(t, trace, "/fscripts/runtime/ts/page.ts:12:2")
assert.Contains(t, trace, "/fscripts/runtime/ts/lib/bar.ts:7:2")
assert.Contains(t, trace, "/fscripts/runtime/ts/lib/err.ts:8:10")
}
6 changes: 3 additions & 3 deletions runtime/v8/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 54,7 @@ type Script struct {
File string
Source string
Root bool
SourceRoots map[string]string // the script source root mappping
SourceRoots interface{} // the script source root mappping
Timeout time.Duration
}

Expand Down Expand Up @@ -92,8 92,8 @@ type Context struct {
Sid string // set the session id
Data map[string]interface{} // set the global data
Root bool
Timeout time.Duration // terminate the execution after this time
SourceRoots map[string]string // the script source root mappping
Timeout time.Duration // terminate the execution after this time
SourceRoots interface{} // the script source root mappping
*Runner
*store.Isolate
*v8go.UnboundScript
Expand Down

0 comments on commit eed4aaf

Please sign in to comment.