Skip to content

Commit

Permalink
fix: missing error message when return HTTPResult (#785)
Browse files Browse the repository at this point in the history
* fix: missing error message when return HTTPResult

Signed-off-by: wenfeng <[email protected]>

* fmt: goimport

Signed-off-by: wenfeng <[email protected]>

* update according to reviewer's advice

Signed-off-by: wenfeng <[email protected]>
  • Loading branch information
wenfengwang authored Jul 26, 2022
1 parent 14755d1 commit b7b2232
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
39 changes: 39 additions & 0 deletions samples/http/receiver-result/main.go
Original file line number Diff line number Diff line change
@@ -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, "")
}
3 changes: 2 additions & 1 deletion samples/http/sender-protobuf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
13 changes: 11 additions & 2 deletions samples/http/sender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
}
}
6 changes: 5 additions & 1 deletion v2/protocol/http/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,15 @@ func (p *Protocol) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

status := http.StatusOK
var errMsg string
if res != nil {
var result *Result
switch {
case protocol.ResultAs(res, &result):
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{}
Expand All @@ -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
}

Expand Down

0 comments on commit b7b2232

Please sign in to comment.