From 3c177ac44fdd76572991782fa8dda07db4b5d7b7 Mon Sep 17 00:00:00 2001 From: wenfeng Date: Wed, 20 Jul 2022 21:05:33 +0800 Subject: [PATCH 1/3] fix: missing error message when return HTTPResult Signed-off-by: wenfeng --- samples/http/receiver-result/main.go | 38 ++++++++++++++++++++++++++++ samples/http/sender/main.go | 7 ++--- v2/protocol/http/protocol.go | 8 +++++- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 samples/http/receiver-result/main.go diff --git a/samples/http/receiver-result/main.go b/samples/http/receiver-result/main.go new file mode 100644 index 000000000..c5632d618 --- /dev/null +++ b/samples/http/receiver-result/main.go @@ -0,0 +1,38 @@ +/* + Copyright 2021 The CloudEvents Authors + SPDX-License-Identifier: Apache-2.0 +*/ + +package main + +import ( + "context" + "fmt" + cloudevents "github.com/cloudevents/sdk-go/v2" + "log" + "net/http" +) + +func main() { + ctx := context.Background() + p, err := cloudevents.NewHTTP() + if err != nil { + log.Fatalf("failed to create protocol: %s", err.Error()) + } + + c, err := cloudevents.NewClient(p) + if err != nil { + log.Fatalf("failed to create client, %v", err) + } + + log.Printf("will listen on :8080\n") + log.Fatalf("failed to start receiver: %s", c.StartReceiver(ctx, receive)) +} + +func receive(ctx context.Context, event cloudevents.Event) cloudevents.Result { + fmt.Printf("%s", event) + if event.Type() != "com.cloudevents.sample.sent" { + return cloudevents.NewHTTPResult(http.StatusBadRequest, "invalid type of %s", event.Type()) + } + return cloudevents.NewHTTPResult(http.StatusOK, "") +} diff --git a/samples/http/sender/main.go b/samples/http/sender/main.go index fb7104840..a8c0ece82 100644 --- a/samples/http/sender/main.go +++ b/samples/http/sender/main.go @@ -7,10 +7,10 @@ package main import ( "context" - "log" - + "fmt" cloudevents "github.com/cloudevents/sdk-go/v2" cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" + "log" ) func main() { @@ -41,7 +41,8 @@ func main() { } else { var httpResult *cehttp.Result cloudevents.ResultAs(res, &httpResult) - log.Printf("Sent %d with status code %d", i, httpResult.StatusCode) + log.Printf("Sent %d with status code %d, error: %s", i, httpResult.StatusCode, + fmt.Errorf(httpResult.Format, httpResult.Args)) } } } diff --git a/v2/protocol/http/protocol.go b/v2/protocol/http/protocol.go index 06204b2a1..290ec8b9b 100644 --- a/v2/protocol/http/protocol.go +++ b/v2/protocol/http/protocol.go @@ -359,6 +359,7 @@ func (p *Protocol) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } status := http.StatusOK + var errMsg string if res != nil { var result *Result switch { @@ -366,7 +367,7 @@ func (p *Protocol) ServeHTTP(rw http.ResponseWriter, req *http.Request) { if result.StatusCode > 100 && result.StatusCode < 600 { status = result.StatusCode } - + errMsg = fmt.Errorf(result.Format, result.Args...).Error() case !protocol.IsACK(res): // Map client errors to http status code validationError := event.ValidationError{} @@ -390,6 +391,11 @@ func (p *Protocol) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } rw.WriteHeader(status) + if errMsg != "" { + if _, err := rw.Write([]byte(errMsg)); err != nil { + return err + } + } return nil } From 41e179a54d167bc0efdb5a7e5435316123cd32ae Mon Sep 17 00:00:00 2001 From: wenfeng Date: Wed, 20 Jul 2022 21:12:21 +0800 Subject: [PATCH 2/3] fmt: goimport Signed-off-by: wenfeng --- samples/http/receiver-result/main.go | 3 ++- samples/http/sender/main.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/samples/http/receiver-result/main.go b/samples/http/receiver-result/main.go index c5632d618..b4a59406a 100644 --- a/samples/http/receiver-result/main.go +++ b/samples/http/receiver-result/main.go @@ -8,9 +8,10 @@ package main import ( "context" "fmt" - cloudevents "github.com/cloudevents/sdk-go/v2" "log" "net/http" + + cloudevents "github.com/cloudevents/sdk-go/v2" ) func main() { diff --git a/samples/http/sender/main.go b/samples/http/sender/main.go index a8c0ece82..b86b9aa47 100644 --- a/samples/http/sender/main.go +++ b/samples/http/sender/main.go @@ -8,9 +8,10 @@ package main import ( "context" "fmt" + "log" + cloudevents "github.com/cloudevents/sdk-go/v2" cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" - "log" ) func main() { From 3a64bdf12cde87fa154b9319e92d94ef02abe7ae Mon Sep 17 00:00:00 2001 From: wenfeng Date: Thu, 21 Jul 2022 10:58:52 +0800 Subject: [PATCH 3/3] update according to reviewer's advice Signed-off-by: wenfeng --- samples/http/receiver-result/main.go | 2 +- samples/http/sender-protobuf/main.go | 3 ++- samples/http/sender/main.go | 13 ++++++++++--- v2/protocol/http/protocol.go | 6 ++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/samples/http/receiver-result/main.go b/samples/http/receiver-result/main.go index b4a59406a..f77406e3d 100644 --- a/samples/http/receiver-result/main.go +++ b/samples/http/receiver-result/main.go @@ -1,5 +1,5 @@ /* - Copyright 2021 The CloudEvents Authors + Copyright 2022 The CloudEvents Authors SPDX-License-Identifier: Apache-2.0 */ diff --git a/samples/http/sender-protobuf/main.go b/samples/http/sender-protobuf/main.go index 5e6414c78..09c1d8612 100644 --- a/samples/http/sender-protobuf/main.go +++ b/samples/http/sender-protobuf/main.go @@ -42,8 +42,9 @@ func main() { var httpResult *cehttp.Result if cloudevents.ResultAs(res, &httpResult) { log.Printf("Sent %d with status code %d", i, httpResult.StatusCode) + } else { + log.Printf("Send did not return an HTTP response: %s", res) } - log.Printf("Send did not return an HTTP response: %s", res) } } } diff --git a/samples/http/sender/main.go b/samples/http/sender/main.go index b86b9aa47..2cf213495 100644 --- a/samples/http/sender/main.go +++ b/samples/http/sender/main.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "log" + "net/http" cloudevents "github.com/cloudevents/sdk-go/v2" cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" @@ -41,9 +42,15 @@ func main() { log.Printf("Failed to send: %v", res) } else { var httpResult *cehttp.Result - cloudevents.ResultAs(res, &httpResult) - log.Printf("Sent %d with status code %d, error: %s", i, httpResult.StatusCode, - fmt.Errorf(httpResult.Format, httpResult.Args)) + if cloudevents.ResultAs(res, &httpResult) { + var err error + if httpResult.StatusCode != http.StatusOK { + err = fmt.Errorf(httpResult.Format, httpResult.Args...) + } + log.Printf("Sent %d with status code %d, error: %v", i, httpResult.StatusCode, err) + } else { + log.Printf("Send did not return an HTTP response: %s", res) + } } } } diff --git a/v2/protocol/http/protocol.go b/v2/protocol/http/protocol.go index 290ec8b9b..dba6fd7ba 100644 --- a/v2/protocol/http/protocol.go +++ b/v2/protocol/http/protocol.go @@ -391,10 +391,8 @@ func (p *Protocol) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } rw.WriteHeader(status) - if errMsg != "" { - if _, err := rw.Write([]byte(errMsg)); err != nil { - return err - } + if _, err := rw.Write([]byte(errMsg)); err != nil { + return err } return nil }