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

Return Error from ParseLevel #83

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions level.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package log

import "strings"
import (
"errors"
"fmt"
"strings"
)

// Level is a logging level.
type Level int32
Expand Down Expand Up @@ -38,20 +42,23 @@ func (l Level) String() string {
}
}

// ErrInvalidLevel is an error returned when parsing an invalid level string.
var ErrInvalidLevel = errors.New("invalid level")

// ParseLevel converts level in string to Level type. Default level is InfoLevel.
func ParseLevel(level string) Level {
func ParseLevel(level string) (Level, error) {
switch strings.ToLower(level) {
case DebugLevel.String():
return DebugLevel
return DebugLevel, nil
case InfoLevel.String():
return InfoLevel
return InfoLevel, nil
case WarnLevel.String():
return WarnLevel
return WarnLevel, nil
case ErrorLevel.String():
return ErrorLevel
return ErrorLevel, nil
case FatalLevel.String():
return FatalLevel
return FatalLevel, nil
default:
return InfoLevel
return 0, fmt.Errorf("%w: %q", ErrInvalidLevel, level)
dezren39 marked this conversation as resolved.
Show resolved Hide resolved
}
}
46 changes: 29 additions & 17 deletions level_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package log

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,52 +11,63 @@ func TestDefaultLevel(t *testing.T) {
var level Level
assert.Equal(t, InfoLevel, level)
}

func TestParseLevel(t *testing.T) {
testCases := []struct {
name string
level string
expLevel Level
input string
expected Level
error error
}{
{
name: "Parse debug",
level: "debug",
expLevel: DebugLevel,
input: "debug",
expected: DebugLevel,
error: nil,
},
{
name: "Parse info",
level: "Info",
expLevel: InfoLevel,
input: "Info",
expected: InfoLevel,
error: nil,
},
{
name: "Parse warn",
level: "WARN",
expLevel: WarnLevel,
input: "WARN",
expected: WarnLevel,
error: nil,
},
{
name: "Parse error",
level: "error",
expLevel: ErrorLevel,
input: "error",
expected: ErrorLevel,
error: nil,
},
{
name: "Parse fatal",
level: "FATAL",
expLevel: FatalLevel,
input: "FATAL",
expected: FatalLevel,
error: nil,
},
{
name: "Default",
level: "",
expLevel: InfoLevel,
input: "",
expected: InfoLevel,
error: fmt.Errorf("%w: %q", ErrInvalidLevel, ""),
},
{
name: "Wrong level, set INFO",
level: "WRONG_LEVEL",
expLevel: InfoLevel,
input: "WRONG_LEVEL",
expected: InfoLevel,
error: fmt.Errorf("%w: %q", ErrInvalidLevel, "WRONG_LEVEL"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expLevel, ParseLevel(tc.level))
lvl, err := ParseLevel(tc.input)
assert.Equal(t, tc.expected, lvl)
assert.Equal(t, tc.error, err)
})
}
}
Loading