Skip to content

Commit 2c50be2

Browse files
authored
Improve wasm errors (#3830)
* 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
1 parent 048ebec commit 2c50be2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

internal/fnruntime/jsglue.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ const wasmBuffer = fs.readFileSync(wasmPath)
2929
var rl = fs.readFileSync(0, 'utf-8')
3030
WebAssembly.instantiate(wasmBuffer, go.importObject).then((result) => {
3131
go.run(result.instance);
32-
console.log(` + jsEntrypointFunction + `(rl));
32+
var result = ` + jsEntrypointFunction + `(rl);
33+
34+
// On success, output to console, on failure retrieve and output the error message
35+
if (result.includes("kind: ResourceList")) {
36+
console.log(result)
37+
} else {
38+
console.log(` + jsEntrypointFunction + "Errors" + `(rl));
39+
}
3340
});`
3441

3542
const golangWasmJSCode = `const fs = require('fs');

internal/fnruntime/wasmtime.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (f *WasmtimeFn) Run(r io.Reader, w io.Writer) error {
146146
if err != nil {
147147
return fmt.Errorf("unable to invoke %v: %v", jsEntrypointFunction, err)
148148
}
149+
149150
// We expect `result` to be a *wasmexec.jsString (which is not exportable) with
150151
// the following definition: type jsString struct { data string }. It will look
151152
// like `&{realPayload}`
@@ -154,17 +155,33 @@ func (f *WasmtimeFn) Run(r io.Reader, w io.Writer) error {
154155
// Try to parse the output as yaml.
155156
resourceListOutput, err := yaml.Parse(resultStr)
156157
if err != nil {
157-
return fmt.Errorf("error parsing output resource list %q: %w", resultStr, err)
158+
additionalErrorMessage, errorResultRetrievalErr := retrieveError(f, resourceList)
159+
if errorResultRetrievalErr != nil {
160+
return errorResultRetrievalErr
161+
}
162+
return fmt.Errorf("parsing output resource list with content: %q\n%w\n%s", resultStr, err, additionalErrorMessage)
158163
}
159164
if resourceListOutput.GetKind() != "ResourceList" {
160-
return fmt.Errorf("invalid resource list output from wasm library; got %q", resultStr)
165+
additionalErrorMessage, errorResultRetrievalErr := retrieveError(f, resourceList)
166+
if errorResultRetrievalErr != nil {
167+
return errorResultRetrievalErr
168+
}
169+
return fmt.Errorf("invalid resource list output from wasm library; got %q\n%s", resultStr, additionalErrorMessage)
161170
}
162171
if _, err = w.Write([]byte(resultStr)); err != nil {
163172
return fmt.Errorf("unable to write the output resource list: %w", err)
164173
}
165174
return f.loader.cleanup()
166175
}
167176

177+
func retrieveError(f *WasmtimeFn, resourceList []byte) (string, error) {
178+
errResult, err := f.gomod.Call(jsEntrypointFunction+"Errors", string(resourceList))
179+
if err != nil {
180+
return "", fmt.Errorf("unable to retrieve additional error message from function: %w", err)
181+
}
182+
return fmt.Sprintf("%s", errResult), nil
183+
}
184+
168185
var _ wasmexec.Instance = &WasmtimeFn{}
169186

170187
func (f *WasmtimeFn) GetSP() (uint32, error) {

0 commit comments

Comments
 (0)