Skip to content

Commit

Permalink
feat: return error cause in http response from gateway (#242)
Browse files Browse the repository at this point in the history
See-also: #167
  • Loading branch information
wenfengwang authored Sep 7, 2022
1 parent 09f0d1e commit 5a286a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.12.13
github.com/aws/aws-sdk-go-v2/service/lambda v1.23.8
github.com/cloudevents/sdk-go/sql/v2 v2.10.1
github.com/cloudevents/sdk-go/v2 v2.10.1
github.com/cloudevents/sdk-go/v2 v2.11.0
github.com/fatih/color v1.13.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-resty/resty/v2 v2.7.0
Expand Down Expand Up @@ -44,7 +44,7 @@ require (
google.golang.org/genproto v0.0.0-20220310185008-1973136f34c6
google.golang.org/grpc v1.44.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.23.4
k8s.io/client-go v0.23.4
k8s.io/klog/v2 v2.30.0
Expand Down Expand Up @@ -102,7 +102,7 @@ require (
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
Expand Down
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cloudevents/sdk-go/sql/v2 v2.10.1 h1:57d6xo9ZamM69Cfj/3uXSEQ9LZ6IQHJKUxtD0QvH66I=
github.com/cloudevents/sdk-go/sql/v2 v2.10.1/go.mod h1:XjZxSDaz9lziRkvFpmsZGz0P4BMIS1SnA4pyJR9A2Ok=
github.com/cloudevents/sdk-go/v2 v2.8.0/go.mod h1:GpCBmUj7DIRiDhVvsK5d6WCbgTWs8DxAWTRtAwQmIXs=
github.com/cloudevents/sdk-go/v2 v2.10.1 h1:qNFovJ18fWOd8Q9ydWJPk1oiFudXyv1GxJIP7MwPjuM=
github.com/cloudevents/sdk-go/v2 v2.10.1/go.mod h1:GpCBmUj7DIRiDhVvsK5d6WCbgTWs8DxAWTRtAwQmIXs=
github.com/cloudevents/sdk-go/v2 v2.11.0 h1:pCb7Cdkb8XpUoil+miuw6PEzuCG9cc8Erj8y1/q3odo=
github.com/cloudevents/sdk-go/v2 v2.11.0/go.mod h1:xDmKfzNjM8gBvjaF8ijFjM1VYOVUEeUfapHMUX1T5To=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -521,13 +522,16 @@ github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ai
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand Down Expand Up @@ -1042,8 +1046,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
10 changes: 5 additions & 5 deletions internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (ga *ceGateway) receive(ctx context.Context, event v2.Event) (*v2.Event, pr
ebName := getEventBusFromPath(requestDataFromContext(ctx))

if ebName == "" {
return nil, fmt.Errorf("invalid eventbus name")
return nil, v2.NewHTTPResult(http.StatusBadRequest, "invalid eventbus name")
}

extensions := event.Extensions()
Expand All @@ -115,7 +115,7 @@ func (ga *ceGateway) receive(ctx context.Context, event v2.Event) (*v2.Event, pr
log.KeyError: err,
"eventTime": eventTime.(string),
})
return nil, fmt.Errorf("invalid delivery time")
return nil, v2.NewHTTPResult(http.StatusBadRequest, "invalid delivery time")
}
ebName = primitive.TimerEventbusName
}
Expand All @@ -127,7 +127,7 @@ func (ga *ceGateway) receive(ctx context.Context, event v2.Event) (*v2.Event, pr
if !exist {
writer, err := eb.OpenBusWriter(vrn)
if err != nil {
return nil, protocol.Result(err)
return nil, v2.NewHTTPResult(http.StatusInternalServerError, err.Error())
}

var loaded bool
Expand All @@ -143,15 +143,15 @@ func (ga *ceGateway) receive(ctx context.Context, event v2.Event) (*v2.Event, pr
log.KeyError: err,
"vrn": vrn,
})
return nil, v2.NewHTTPResult(http.StatusBadRequest, err.Error())
return nil, v2.NewHTTPResult(http.StatusInternalServerError, err.Error())
}
eventData := EventData{
BusName: ebName,
EventID: eventID,
}
resEvent, err := createResponseEvent(eventData)
if err != nil {
return nil, fmt.Errorf("create response event error")
return nil, v2.NewHTTPResult(http.StatusInternalServerError, err.Error())
}
return resEvent, v2.ResultACK
}
Expand Down
10 changes: 7 additions & 3 deletions vsctl/command/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,24 @@ func sendOne(cmd *cobra.Command, ctx context.Context, ceClient ce.Client) {
cmdFailedf(cmd, "failed to send: %s\n", res.Error())
} else {
if IsFormatJSON(cmd) {
data, _ := json.Marshal(map[string]interface{}{"Result": httpResult.StatusCode})
data, _ := json.Marshal(map[string]interface{}{
"Result": httpResult.StatusCode,
"Error": fmt.Errorf(httpResult.Format, httpResult.Args...),
})
color.Green(string(data))
} else {
t := table.NewWriter()
tbcfg := []table.ColumnConfig{
{Number: 1, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 2, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
}
if detail {
t.AppendHeader(table.Row{"Result", "RESPONSE EVENT"})
t.AppendRow(table.Row{httpResult.StatusCode, resEvent})
tbcfg = append(tbcfg, table.ColumnConfig{Number: 2, Align: text.AlignCenter, AlignHeader: text.AlignCenter})
} else {
t.AppendHeader(table.Row{"Result"})
t.AppendRow(table.Row{httpResult.StatusCode})
t.AppendHeader(table.Row{"Result", "Error"})
t.AppendRow(table.Row{httpResult.StatusCode, fmt.Errorf(httpResult.Format, httpResult.Args...)})
}
t.SetColumnConfigs(tbcfg)
t.SetOutputMirror(os.Stdout)
Expand Down

0 comments on commit 5a286a0

Please sign in to comment.