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

Expose result info messages #1106

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1106.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
cloudflare: expose `Messages` from the `Response` object
```
4 changes: 2 additions & 2 deletions cloudflare.go
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -16,8 +17,6 @@ import (
"strings"
"time"

"errors"

"golang.org/x/time/rate"
)

Expand Down Expand Up @@ -347,6 +346,7 @@ func (api *API) makeRequestWithAuthTypeAndHeadersComplete(ctx context.Context, m
Errors: errBody.Errors,
ErrorCodes: errCodes,
ErrorMessages: errMsgs,
Messages: errBody.Messages,
}

switch resp.StatusCode {
Expand Down
23 changes: 22 additions & 1 deletion errors.go
Expand Up @@ -70,6 +70,9 @@ type Error struct {
// ErrorMessages is a list of all the error codes.
ErrorMessages []string

// Messages is a list of informational messages provided by the endpoint.
Messages []ResponseInfo

// RayID is the internal identifier for the request that was made.
RayID string
}
Expand All @@ -90,7 +93,21 @@ func (e Error) Error() string {
errMessages = append(errMessages, m)
}

return errString + strings.Join(errMessages, ", ")
msgs := []string{}
for _, m := range e.Messages {
msgs = append(msgs, m.Message)
}

errString += strings.Join(errMessages, ", ")

// `Messages` is primarily used for additional validation failure notes in
// page rules. This shouldn't be used going forward but instead, use the
// error fields appropriately.
if len(msgs) > 0 {
errString += "\n" + strings.Join(msgs, " \n")
}

return errString
}

// RequestError is for 4xx errors that we encounter not covered elsewhere
Expand Down Expand Up @@ -119,6 +136,10 @@ func (e RequestError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}

func (e RequestError) Messages() []ResponseInfo {
return e.cloudflareError.Messages
}

func (e RequestError) RayID() string {
return e.cloudflareError.RayID
}
Expand Down