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 }