Skip to content

Commit

Permalink
Improve wasm errors (#3830)
Browse files Browse the repository at this point in the history
* retrieve errors from additional function

* improve error formatting a bit

* address review comments

* Allow the nodejs based wasm support to show errors

* remove left-over line

* address review comments
  • Loading branch information
wleese committed Feb 23, 2023
1 parent 048ebec commit 2c50be2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 8 additions & 1 deletion internal/fnruntime/jsglue.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const wasmBuffer = fs.readFileSync(wasmPath)
var rl = fs.readFileSync(0, 'utf-8')
WebAssembly.instantiate(wasmBuffer, go.importObject).then((result) => {
go.run(result.instance);
console.log(` + jsEntrypointFunction + `(rl));
var result = ` + jsEntrypointFunction + `(rl);
// On success, output to console, on failure retrieve and output the error message
if (result.includes("kind: ResourceList")) {
console.log(result)
} else {
console.log(` + jsEntrypointFunction + "Errors" + `(rl));
}
});`

const golangWasmJSCode = `const fs = require('fs');
Expand Down
21 changes: 19 additions & 2 deletions internal/fnruntime/wasmtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (f *WasmtimeFn) Run(r io.Reader, w io.Writer) error {
if err != nil {
return fmt.Errorf("unable to invoke %v: %v", jsEntrypointFunction, err)
}

// We expect `result` to be a *wasmexec.jsString (which is not exportable) with
// the following definition: type jsString struct { data string }. It will look
// like `&{realPayload}`
Expand All @@ -154,17 +155,33 @@ func (f *WasmtimeFn) Run(r io.Reader, w io.Writer) error {
// Try to parse the output as yaml.
resourceListOutput, err := yaml.Parse(resultStr)
if err != nil {
return fmt.Errorf("error parsing output resource list %q: %w", resultStr, err)
additionalErrorMessage, errorResultRetrievalErr := retrieveError(f, resourceList)
if errorResultRetrievalErr != nil {
return errorResultRetrievalErr
}
return fmt.Errorf("parsing output resource list with content: %q\n%w\n%s", resultStr, err, additionalErrorMessage)
}
if resourceListOutput.GetKind() != "ResourceList" {
return fmt.Errorf("invalid resource list output from wasm library; got %q", resultStr)
additionalErrorMessage, errorResultRetrievalErr := retrieveError(f, resourceList)
if errorResultRetrievalErr != nil {
return errorResultRetrievalErr
}
return fmt.Errorf("invalid resource list output from wasm library; got %q\n%s", resultStr, additionalErrorMessage)
}
if _, err = w.Write([]byte(resultStr)); err != nil {
return fmt.Errorf("unable to write the output resource list: %w", err)
}
return f.loader.cleanup()
}

func retrieveError(f *WasmtimeFn, resourceList []byte) (string, error) {
errResult, err := f.gomod.Call(jsEntrypointFunction+"Errors", string(resourceList))
if err != nil {
return "", fmt.Errorf("unable to retrieve additional error message from function: %w", err)
}
return fmt.Sprintf("%s", errResult), nil
}

var _ wasmexec.Instance = &WasmtimeFn{}

func (f *WasmtimeFn) GetSP() (uint32, error) {
Expand Down

0 comments on commit 2c50be2

Please sign in to comment.