From b68db6cb4fd6a3288248fe9c2a4bc37ea647b9ae Mon Sep 17 00:00:00 2001 From: Sylvain Prost Date: Tue, 22 Jun 2021 17:45:08 +0200 Subject: [PATCH] stats: try to interpret error with statuser interface --- stats/statuser.go | 10 ++++++---- stats/with_status.go | 7 ++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/stats/statuser.go b/stats/statuser.go index 8de7ba6..627bc32 100644 --- a/stats/statuser.go +++ b/stats/statuser.go @@ -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 { @@ -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) diff --git a/stats/with_status.go b/stats/with_status.go index 7581067..a4704bd 100644 --- a/stats/with_status.go +++ b/stats/with_status.go @@ -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}