Skip to content

Commit 74c5809

Browse files
committed
error handling is diagnostic
1 parent ec5b8b4 commit 74c5809

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

preview/main.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,28 @@ func main() {
2929
<-done
3030
}
3131

32+
type previewOutput struct {
33+
Output preview.Output `json:"output"`
34+
Diags []types.FriendlyDiagnostic `json:"diags"`
35+
}
36+
3237
func tfpreview(this js.Value, p []js.Value) any {
3338
defer func() {
39+
// Return a panic as a diagnostic if one occurs.
3440
if r := recover(); r != nil {
35-
// Panic happened, do something
36-
return fmt.Sprintf("Something went wrong: %v", r)
41+
data, _ := json.Marshal(previewOutput{
42+
Output: preview.Output{},
43+
Diags: []types.FriendlyDiagnostic{
44+
{
45+
Severity: types.DiagnosticSeverityError,
46+
Summary: "A panic occurred",
47+
Detail: fmt.Sprintf("%v", r),
48+
Extra: types.DiagnosticExtra{},
49+
},
50+
},
51+
})
52+
53+
return js.ValueOf(string(data))
3754
}
3855
}()
3956

@@ -53,9 +70,9 @@ func tfpreview(this js.Value, p []js.Value) any {
5370
Logger: logger,
5471
}, tf)
5572

56-
data, _ := json.Marshal(map[string]any{
57-
"output": output,
58-
"diags": diags,
73+
data, _ := json.Marshal(previewOutput{
74+
Output: output,
75+
Diags: diags,
5976
})
6077

6178
return js.ValueOf(string(data))

0 commit comments

Comments
 (0)