Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing error message when return HTTPResult #785

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember why we did not allow this, we need a way to let the integrator control if the error body is sent or not, some senders expect a cloudevent in response and this will not parse as a cloudevent as a result.

I am in favor of landing this as is but waiting for a release to add an option to control the output format here to let the integrator choose how this error is formatted on return.

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