diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-03 23:43:48 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-03 23:43:48 +0200 |
| commit | 7e6b2e4c2ba957899b12dac7e0ea9f7c29a9a7b3 (patch) | |
| tree | 40ddb216b080bd0da08502bd855c25d45f9dc4cd /internal/worktime | |
| parent | 293406c3bd2acc85490da11afabaf6733babd5e4 (diff) | |
worktime: use sentinel login state errors
Replace string matching in timer sync with errors.Is and add regression tests.
Diffstat (limited to 'internal/worktime')
| -rw-r--r-- | internal/worktime/entries.go | 11 | ||||
| -rw-r--r-- | internal/worktime/entries_test.go | 5 |
2 files changed, 14 insertions, 2 deletions
diff --git a/internal/worktime/entries.go b/internal/worktime/entries.go index f9226ce..b9a7e40 100644 --- a/internal/worktime/entries.go +++ b/internal/worktime/entries.go @@ -13,6 +13,13 @@ const ( actionAdd = "add" ) +var ( + // ErrAlreadyLoggedIn indicates that a category already has an open login entry. + ErrAlreadyLoggedIn = errors.New("already logged in") + // ErrNotLoggedIn indicates that a category has no active login entry. + ErrNotLoggedIn = errors.New("not logged in") +) + // Login creates a login entry after validating the category is not already logged in. func Login(dbDir, hostname, category string, at time.Time, descr string) (Entry, error) { host, err := normalizeHostname(hostname) @@ -26,7 +33,7 @@ func Login(dbDir, hostname, category string, at time.Time, descr string) (Entry, return Entry{}, err } if loggedIn { - return Entry{}, fmt.Errorf("already logged in for %q", cat) + return Entry{}, fmt.Errorf("%w for %q", ErrAlreadyLoggedIn, cat) } entry := newEntry(actionLogin, host, cat, at, 0, descr) @@ -46,7 +53,7 @@ func Logout(dbDir, hostname, category string, at time.Time, descr string) (Entry return Entry{}, err } if !loggedIn { - return Entry{}, fmt.Errorf("not logged in for %q", cat) + return Entry{}, fmt.Errorf("%w for %q", ErrNotLoggedIn, cat) } entry := newEntry(actionLogout, host, cat, at, 0, descr) diff --git a/internal/worktime/entries_test.go b/internal/worktime/entries_test.go index 1327a1a..f49dbd8 100644 --- a/internal/worktime/entries_test.go +++ b/internal/worktime/entries_test.go @@ -1,6 +1,7 @@ package worktime import ( + "errors" "testing" "time" ) @@ -19,6 +20,8 @@ func TestLoginLogoutValidation(t *testing.T) { if _, err := Login(dbDir, host, "work", time.Unix(110, 0), "start again"); err == nil { t.Fatal("Login() error = nil, want already logged in error") + } else if !errors.Is(err, ErrAlreadyLoggedIn) { + t.Fatalf("Login() error = %v, want ErrAlreadyLoggedIn", err) } logoutEntry, err := Logout(dbDir, host, "work", time.Unix(120, 0), "stop") @@ -31,6 +34,8 @@ func TestLoginLogoutValidation(t *testing.T) { if _, err := Logout(dbDir, host, "work", time.Unix(130, 0), "stop again"); err == nil { t.Fatal("Logout() error = nil, want not logged in error") + } else if !errors.Is(err, ErrNotLoggedIn) { + t.Fatalf("Logout() error = %v, want ErrNotLoggedIn", err) } } |
