-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patherror_response.go
59 lines (47 loc) · 1.29 KB
/
error_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"errors"
"github.com/bool64/ctxd"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)
type customErr struct {
Message string `json:"msg"`
Details map[string]any `json:"details,omitempty"`
}
func errorResponse() usecase.Interactor {
type errType struct {
Type string `query:"type" enum:"ok,invalid_argument,conflict" required:"true"`
}
type okResp struct {
Status string `json:"status"`
}
u := usecase.NewIOI(new(errType), new(okResp), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*errType)
out = output.(*okResp)
)
switch in.Type {
case "ok":
out.Status = "ok"
case "invalid_argument":
return status.Wrap(errors.New("bad value for foo"), status.InvalidArgument)
case "conflict":
return status.Wrap(ctxd.NewError(ctx, "conflict", "foo", "bar"),
status.AlreadyExists)
}
return nil
})
u.SetTitle("Declare Expected Errors")
u.SetDescription("This use case demonstrates documentation of expected errors.")
u.SetExpectedErrors(status.InvalidArgument, anotherErr{}, status.FailedPrecondition, status.AlreadyExists)
return u
}
// anotherErr is another custom error.
type anotherErr struct {
Foo int `json:"foo"`
}
func (anotherErr) Error() string {
return "foo happened"
}