Skip to content

Commit 86c14b3

Browse files
committed
[breaking] allow handlers to return errors
1 parent d9609cb commit 86c14b3

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ What if you can influence the CLI behaviour from the server? This enables you to
1212

1313
### Status
1414

15-
Alpha, stabilising API and looking for design/usage feedback!
15+
Alpha, looking for design/usage feedback!
1616

1717
### Ideally support:
1818
- more of the OpenAPI types and their checks. eg arrays, enums, objects, multi types etc
@@ -56,17 +56,19 @@ Define a cobra root command:
5656

5757
```go
5858
rootCmd := &cobra.Command{
59-
Use: "calc",
60-
Short: "My Calc",
61-
Long: "My Calc powered by OpenAPI",
59+
Use: "calc",
60+
Short: "My Calc",
61+
Long: "My Calc powered by OpenAPI",
6262
}
6363
```
6464

6565
Define one or more handler functions of the following signature:
6666
```go
67-
func handler(opts *cobra.Command, args []string, data climate.HandlerData) {
68-
// do something more useful
69-
slog.Info("called!", "data", fmt.Sprintf("%+v", data))
67+
func handler(opts *cobra.Command, args []string, data climate.HandlerData) error {
68+
slog.Info("called!", "data", fmt.Sprintf("%+v", data))
69+
err := doSomethingUseful(data)
70+
71+
return err
7072
}
7173
```
7274
#### Handler Data
@@ -89,10 +91,10 @@ Define the handlers for the necessary operations. These map to the `operationId`
8991

9092
```go
9193
handlers := map[string]Handler{
92-
"AddGet": handler,
93-
"AddPost": handler,
94-
"HealthCheck": handler,
95-
"GetInfo": handler,
94+
"AddGet": handler,
95+
"AddPost": handler,
96+
"HealthCheck": handler,
97+
"GetInfo": handler,
9698
}
9799
```
98100

lib.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type HandlerData struct {
4242
}
4343

4444
// The handler signature
45-
type Handler func(opts *cobra.Command, args []string, data HandlerData)
45+
type Handler func(opts *cobra.Command, args []string, data HandlerData) error
4646

4747
type extensions struct {
4848
hidden bool
@@ -260,12 +260,12 @@ func BootstrapV3(rootCmd *cobra.Command, model libopenapi.DocumentModel[v3.Docum
260260
if op.Summary != "" {
261261
cmd.Short = op.Summary
262262
}
263-
cmd.Run = func(opts *cobra.Command, args []string) {
263+
cmd.RunE = func(opts *cobra.Command, args []string) error {
264264
if err := interpolatePath(&cmd, &hData); err != nil {
265-
slog.Error("Error interpolating path", "err", err)
265+
return err
266266
}
267267

268-
handler(opts, args, hData)
268+
return handler(opts, args, hData)
269269
}
270270

271271
cmd.Use = op.OperationId // default

lib_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ func TestBootstrapV3(t *testing.T) {
7777
model, err := LoadFileV3("api.yaml")
7878
assert.NoError(t, err)
7979

80-
handler := func(opts *cobra.Command, args []string, data HandlerData) {
80+
handler := func(opts *cobra.Command, args []string, data HandlerData) error {
8181
assert.Equal(t, data.PathParams, []ParamMeta{{Name: "p1", Type: Integer}})
8282
assert.Equal(t, data.QueryParams, []ParamMeta{{Name: "p2", Type: String}})
8383
assert.Equal(t, data.HeaderParams, []ParamMeta{{Name: "p3", Type: Number}})
8484
assert.Equal(t, data.CookieParams, []ParamMeta{{Name: "p4", Type: Boolean}})
8585
assert.Equal(t, data.RequestBodyParam, &ParamMeta{Name: "req-body", Type: String})
86+
87+
return nil
8688
}
8789
rootCmd := &cobra.Command{
8890
Use: "calc",

0 commit comments

Comments
 (0)