diff --git a/samples/http/receiver-result/main.go b/samples/http/receiver-result/main.go new file mode 100644 index 000000000..f77406e3d --- /dev/null +++ b/samples/http/receiver-result/main.go @@ -0,0 +1,39 @@ +/* + Copyright 2022 The CloudEvents Authors + SPDX-License-Identifier: Apache-2.0 +*/ + +package main + +import ( + "context" + "fmt" + "log" + "net/http" + + cloudevents "github.com/cloudevents/sdk-go/v2" +) + +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-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 fb7104840..2cf213495 100644 --- a/samples/http/sender/main.go +++ b/samples/http/sender/main.go @@ -7,7 +7,9 @@ package main import ( "context" + "fmt" "log" + "net/http" cloudevents "github.com/cloudevents/sdk-go/v2" cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" @@ -40,8 +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", i, httpResult.StatusCode) + 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 06204b2a1..dba6fd7ba 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,9 @@ func (p *Protocol) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } rw.WriteHeader(status) + if _, err := rw.Write([]byte(errMsg)); err != nil { + return err + } return nil }