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

Improve error recovery #705

Merged
merged 7 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-ways-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Improve error recovery
16 changes: 12 additions & 4 deletions cmd/astro-wasm/astro-wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,23 @@ func Transform() any {
h := handler.NewHandler(source, transformOptions.Filename)

styleError := []string{}
handler := js.FuncOf(func(this js.Value, args []js.Value) any {
promiseHandle := js.FuncOf(func(this js.Value, args []js.Value) any {
resolve := args[0]
reject := args[1]

go func() {
var doc *astro.Node
defer func() {
if err := recover(); err != nil {
reject.Invoke(wasm_utils.ErrorToJSError(h, err.(error)))
return
}
}()

doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h))
if err != nil {
fmt.Println(err)
reject.Invoke(wasm_utils.ErrorToJSError(h, err))
return
}

// Hoist styles and scripts to the top-level
Expand Down Expand Up @@ -403,11 +411,11 @@ func Transform() any {

return nil
})
defer handler.Release()
defer promiseHandle.Release()

// Create and return the Promise object
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
return promiseConstructor.New(promiseHandle)
})
}

Expand Down
49 changes: 49 additions & 0 deletions internal_wasm/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package wasm_utils

import (
"fmt"
"regexp"
"runtime/debug"
"strings"
"syscall/js"

"github.com/norunners/vert"
astro "github.com/withastro/compiler/internal"
"github.com/withastro/compiler/internal/handler"
)

// See https://stackoverflow.com/questions/68426700/how-to-wait-a-js-async-function-from-golang-wasm
Expand Down Expand Up @@ -50,3 +56,46 @@ func GetAttrs(n *astro.Node) js.Value {
}
return attrs
}

type JSError struct {
Message string `js:"message"`
Stack string `js:"stack"`
}

func (err *JSError) Value() js.Value {
return vert.ValueOf(err).Value
}

var FN_NAME_RE = regexp.MustCompile(`(\w+)\([^)]+\)$`)

func ErrorToJSError(h *handler.Handler, err error) js.Value {
stack := string(debug.Stack())
message := strings.TrimSpace(err.Error())
if strings.Contains(message, ":") {
message = strings.TrimSpace(strings.Split(message, ":")[1])
}
hasFnName := false
message = fmt.Sprintf("UnknownCompilerError: %s", message)
cleanStack := message
for _, v := range strings.Split(stack, "\n") {
matches := FN_NAME_RE.FindAllString(v, -1)
if len(matches) > 0 {
name := strings.Split(matches[0], "(")[0]
if name == "panic" {
cleanStack = message
continue
}
cleanStack += fmt.Sprintf("\n at %s", strings.Split(matches[0], "(")[0])
hasFnName = true
} else if hasFnName {
url := strings.Split(strings.Split(strings.TrimSpace(v), " ")[0], "/compiler/")[1]
cleanStack += fmt.Sprintf(" (@astrojs/compiler/%s)", url)
hasFnName = false
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
}
}
jsError := JSError{
Message: message,
Stack: cleanStack,
}
return jsError.Value()
}