-
Notifications
You must be signed in to change notification settings - Fork 67
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 runtime error messages by adding a function to retrieve errors from resourceList #978
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,20 @@ | |
package main | ||
|
||
import ( | ||
"fmt" | ||
"syscall/js" | ||
|
||
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-replacements/replacements" | ||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" | ||
) | ||
|
||
func run() error { | ||
resourceList := []byte("") | ||
|
||
// Register js function processResourceList to the globals. | ||
js.Global().Set("processResourceList", resourceListProcessorWrapper()) | ||
js.Global().Set("processResourceList", resourceListProcessorWrapper(&resourceList)) | ||
// Provide a second function that serves purely to also return the resourceList, | ||
// in case of the above function failing. | ||
js.Global().Set("processResourceListErrors", resourceListProcessorErrors(&resourceList)) | ||
// We need to ensure that the Go program is running when JavaScript calls it. | ||
// Otherwise, it will complain the Go program has already exited. | ||
<-make(chan bool) | ||
|
@@ -37,19 +41,42 @@ func applyReplacements(input []byte) ([]byte, error) { | |
return fn.Run(fn.ResourceListProcessorFunc(replacements.ApplyReplacements), []byte(input)) | ||
} | ||
|
||
func resourceListProcessorWrapper() js.Func { | ||
// TODO: figure out a better way to surface a golang error to JS environment. | ||
// Currently error is surfaced as a string. | ||
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} { | ||
func resourceListProcessorWrapper(resourceList *[]byte) js.Func { | ||
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any { | ||
if len(args) != 1 { | ||
return "Invalid number of arguments passed" | ||
} | ||
input := args[0].String() | ||
applied, err := applyReplacements([]byte(input)) | ||
if err != nil { | ||
return fmt.Errorf("unable to process resource list:", err.Error()) | ||
*resourceList = applied | ||
return "unable to process resource list: " + err.Error() | ||
} | ||
*resourceList = applied | ||
return string(applied) | ||
}) | ||
|
||
return jsonFunc | ||
} | ||
|
||
// This funcion will return ALL Results with Severity error, | ||
// meaning unrelated errors may also be included. | ||
func resourceListProcessorErrors(resourceList *[]byte) js.Func { | ||
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any { | ||
rl, err := fn.ParseResourceList(*resourceList) | ||
if err != nil { | ||
return "" | ||
} | ||
if len(rl.Results) == 0 { | ||
return "" | ||
} | ||
errorMessages := "" | ||
for _, r := range(rl.Results) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running in a pipeline, the function only appends I'd suggest at least adding a comment to notify future contributors about this since it could cause some unexpected failures in certain risk conditions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added! |
||
if (r.Severity == "error") { | ||
errorMessages += r.Message | ||
} | ||
} | ||
return errorMessages | ||
}) | ||
return jsonFunc | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this entire function could simply either return the resourceList or empty string, always deferring to the other function to get error messages, rather than mixing those approaches. Would require an update to kptdev/kpt#3830 if we take that route.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. I think deferring to the other function to get errors makes a lot of sense. Would like to hear @droot @mortent opinion on this.