Skip to content

Commit

Permalink
Add trace-id to flaps error type (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwuah committed Apr 18, 2024
1 parent 0bf2e23 commit c1c291c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions flaps/flaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func (f *Client) _sendRequest(ctx context.Context, method, endpoint string, in,
ResponseStatusCode: resp.StatusCode,
ResponseBody: responseBody,
FlyRequestId: resp.Header.Get(headerFlyRequestId),
TraceID: span.SpanContext().TraceID().String(),
}
}
if out != nil {
Expand Down
17 changes: 17 additions & 0 deletions flaps/flaps_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type FlapsError struct {
ResponseStatusCode int
ResponseBody []byte
FlyRequestId string
TraceID string
}

func (fe *FlapsError) Error() string {
Expand Down Expand Up @@ -79,6 +80,18 @@ func GetErrorRequestID(err error) string {
return ""
}

type ErrorTraceID interface {
ErrTraceID() string
}

func GetErrorTraceID(err error) string {
var ferr ErrorTraceID
if errors.As(err, &ferr) {
return ferr.ErrTraceID()
}
return ""
}

func (fe *FlapsError) StatusCode() *StatusCode {
var errResp errorResponse
unmarshalErr := json.Unmarshal(fe.ResponseBody, &errResp)
Expand All @@ -94,6 +107,10 @@ func (fe *FlapsError) ErrRequestID() string {
return fe.FlyRequestId
}

func (fe *FlapsError) ErrTraceID() string {
return fe.TraceID
}

func (fe *FlapsError) Is(target error) bool {
if other, ok := target.(*FlapsError); ok {
return fe.ResponseStatusCode == other.ResponseStatusCode
Expand Down

0 comments on commit c1c291c

Please sign in to comment.