Skip to content

Commit

Permalink
stats: try to interpret error with statuser interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sypheos committed Jun 22, 2021
1 parent cf71630 commit b68db6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 6 additions & 4 deletions stats/statuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (defaultStatuser) Status(err error) string {
}

func GetStatus(err error, opts ...ExtractStatusOption) string {
type statuser interface {
Status() string
}

var o = defaultStatusOptions

for _, opt := range opts {
Expand All @@ -45,10 +49,8 @@ func GetStatus(err error, opts ...ExtractStatusOption) string {
}

for {
ws, ok := err.(*withStatus)

if ok {
return ws.status
if st, ok := err.(statuser); ok {
return st.Status()
}

cause := base.UnwrapOnce(err)
Expand Down
7 changes: 4 additions & 3 deletions stats/with_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ type withStatus struct {
status string
}

func (ws *withStatus) Error() string { return ws.cause.Error() }
func (ws *withStatus) Unwrap() error { return ws.cause }
func (ws *withStatus) Cause() error { return ws.cause }
func (ws *withStatus) Error() string { return ws.cause.Error() }
func (ws *withStatus) Unwrap() error { return ws.cause }
func (ws *withStatus) Cause() error { return ws.cause }
func (ws *withStatus) Status() string { return ws.status }

func (ws *withStatus) Tags() map[string]interface{} {
return map[string]interface{}{"status": ws.status}
Expand Down

0 comments on commit b68db6c

Please sign in to comment.