Skip to content

Commit

Permalink
fix error source implementing stringer interface with fallback to def…
Browse files Browse the repository at this point in the history
…ault if not set
  • Loading branch information
marefr committed Oct 9, 2024
1 parent 10dba60 commit 25d4cc7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/error_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ const (
DefaultErrorSource ErrorSource = ErrorSourcePlugin
)

// IsValid return true if es is ErrorSourceDownstream or ErrorSourcePlugin.
func (es ErrorSource) IsValid() bool {
return es == ErrorSourceDownstream || es == ErrorSourcePlugin
}

// String returns the string representation of es. If es is not valid, DefaultErrorSource is returned.
func (es ErrorSource) String() string {
if !es.IsValid() {
return string(DefaultErrorSource)
}

return string(es)
}

// ErrorSourceFromStatus returns an [ErrorSource] based on provided HTTP status code.
func ErrorSourceFromHTTPStatus(statusCode int) ErrorSource {
switch statusCode {
Expand Down
3 changes: 3 additions & 0 deletions backend/error_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import (
func TestErrorSource(t *testing.T) {
var es ErrorSource
require.False(t, es.IsValid())
require.Equal(t, "plugin", es.String())
require.True(t, ErrorSourceDownstream.IsValid())
require.Equal(t, "downstream", ErrorSourceDownstream.String())
require.True(t, ErrorSourcePlugin.IsValid())
require.Equal(t, "plugin", ErrorSourcePlugin.String())
}

func TestIsDownstreamError(t *testing.T) {
Expand Down

0 comments on commit 25d4cc7

Please sign in to comment.