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 wasm errors #3830

Merged
merged 6 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
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
23 changes: 20 additions & 3 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, err2 := retrieveError(f, resourceList)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a more descriptive name here than err2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it another shot.

natasha41575 marked this conversation as resolved.
Show resolved Hide resolved
if err2 != nil {
additionalErrorMessage = fmt.Sprint("failed to retrieve more error information: %w", err2)
natasha41575 marked this conversation as resolved.
Show resolved Hide resolved
}
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 {
additionalErrorMessage = fmt.Sprint("failed to retrieve more error information: %w", errorResultRetrievalErr)
natasha41575 marked this conversation as resolved.
Show resolved Hide resolved
}
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 Expand Up @@ -201,4 +218,4 @@ func (f *WasmtimeFn) Write(fd int, b []byte) (n int, err error) {
// Error implements the wasmexec.errorLogger interface
func (f *WasmtimeFn) Error(format string, params ...interface{}) {
log.Printf("ERROR: "+format+"\n", params...)
}
}