Skip to content

Commit ec5b8b4

Browse files
committed
error handling
1 parent 0ed24c6 commit ec5b8b4

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

preview/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"io/fs"
10+
"log/slog"
1011
"path/filepath"
1112
"syscall/js"
1213

@@ -29,22 +30,34 @@ func main() {
2930
}
3031

3132
func tfpreview(this js.Value, p []js.Value) any {
33+
defer func() {
34+
if r := recover(); r != nil {
35+
// Panic happened, do something
36+
return fmt.Sprintf("Something went wrong: %v", r)
37+
}
38+
}()
39+
3240
tf, err := fileTreeFS(p[0])
3341
if err != nil {
3442
return err
3543
}
3644

45+
// TODO: Capture the logger into a bytes.Buffer, and return this
46+
// as a string in the output?
47+
logger := slog.New(slog.DiscardHandler)
3748
output, diags := preview.Preview(context.Background(), preview.Input{
3849
PlanJSONPath: "",
3950
PlanJSON: nil,
4051
ParameterValues: nil,
4152
Owner: types.WorkspaceOwner{},
53+
Logger: logger,
4254
}, tf)
4355

4456
data, _ := json.Marshal(map[string]any{
4557
"output": output,
4658
"diags": diags,
4759
})
60+
4861
return js.ValueOf(string(data))
4962
}
5063

0 commit comments

Comments
 (0)