Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
refactor(errors): defining errors in proper way
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jul 23, 2024
1 parent 5710be7 commit 0a66230
Show file tree
Hide file tree
Showing 24 changed files with 122 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21.1'
go-version: '1.22.2'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.21.1
go-version: '1.22.2'

- name: Create release files
run: bash ./.github/releaser/release_cli.sh
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '1.21.1'
go-version: '1.22.2'

- name: Unit tests
run: make unit_test
run: make unit-test
34 changes: 5 additions & 29 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
# Compiled binary files
*.exe
*.out
*.dll
*.so
*.dylib

# Compiled object files
*.o

# Go binary and test coverage reports
*.test
*.prof

# Dependency directories (remove these if using Go modules)
/vendor/
/Godeps/

# Ignore IDE and editor-specific files
.vscode/
.idea/
*.sublime-project
*.sublime-workspace

# OS-specific files
.DS_Store
Thumbs.db

# dev files
cmd/config.yaml
/client
log.ttrace
build/
# Development files
client
build

# python cache files
test/__pycache__
# Cache files
*cache*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build-cli:
go build -o ./build/ttrace ./cmd/main.go

### Testing
unit_test:
unit-test:
go test $(PACKAGES)

test:
Expand Down
19 changes: 19 additions & 0 deletions cmd/commands/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package commands

import "fmt"

type InvalidAuthInfoError struct {
command string
}

func (e InvalidAuthInfoError) Error() string {
return fmt.Sprintf("command %s is not a valid command to authenticate", e.command)
}

type InvalidConfigPathError struct {
path string
}

func (e InvalidConfigPathError) Error() string {
return fmt.Sprintf("path %s doesn't contain a valid config file", e.path)
}
2 changes: 1 addition & 1 deletion cmd/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

cobra "github.com/spf13/cobra"
"github.com/zurvan-lab/TimeTrace/config"
"github.com/zurvan-lab/timetrace/config"
)

func InitCommand(parentCmd *cobra.Command) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/peterh/liner"
cobra "github.com/spf13/cobra"
"github.com/zurvan-lab/TimeTrace/utils/errors"
)

/*
Expand Down Expand Up @@ -94,7 +93,9 @@ func ConnectCommand(parentCmd *cobra.Command) {
}
}
} else {
ExitOnError(cmd, fmt.Errorf("%w: %s", errors.ErrInvalidCommand, response))
ExitOnError(cmd, fmt.Errorf("%w: %s", InvalidAuthInfoError{
command: conQuery,
}, response))
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package commands

import (
cobra "github.com/spf13/cobra"
"github.com/zurvan-lab/TimeTrace/config"
"github.com/zurvan-lab/TimeTrace/core/database"
"github.com/zurvan-lab/TimeTrace/core/server"
tte "github.com/zurvan-lab/TimeTrace/utils/errors"
ttlog "github.com/zurvan-lab/TimeTrace/utils/log"
"github.com/zurvan-lab/timetrace/config"
"github.com/zurvan-lab/timetrace/core/database"
"github.com/zurvan-lab/timetrace/core/server"
ttlog "github.com/zurvan-lab/timetrace/log"
)

func RunCommand(parentCmd *cobra.Command) {
Expand All @@ -16,14 +15,16 @@ func RunCommand(parentCmd *cobra.Command) {
}
parentCmd.AddCommand(run)

confingPath := run.Flags().StringP("config", "c", "", "Path to your config.yaml file.")
configPath := run.Flags().StringP("config", "c", "", "Path to your config.yaml file.")

run.Run = func(cmd *cobra.Command, args []string) {
if confingPath == nil || *confingPath == "" {
ExitOnError(cmd, tte.ErrInavlidConfigPath)
if configPath == nil || *configPath == "" {
ExitOnError(cmd, InvalidConfigPathError{
path: *configPath,
})
}

cfg, err := config.LoadFromFile(*confingPath)
cfg, err := config.LoadFromFile(*configPath)
if err != nil {
ExitOnError(cmd, err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"github.com/spf13/cobra"
timetrace "github.com/zurvan-lab/TimeTrace"
"github.com/zurvan-lab/TimeTrace/cmd/commands"
tt "github.com/zurvan-lab/timetrace"
"github.com/zurvan-lab/timetrace/cmd/commands"
)

func main() {
rootCmd := &cobra.Command{
Use: "ttrace",
Version: timetrace.StringVersion(),
Version: tt.StringVersion(),
}

commands.RunCommand(rootCmd)
Expand Down
10 changes: 7 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"slices"

tte "github.com/zurvan-lab/TimeTrace/utils/errors"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -44,7 +43,9 @@ type User struct {

func (conf *Config) BasicCheck() error {
if len(conf.Users) == 0 {
return tte.ErrInvalidUsers
return BasicCheckError{
reason: "at least one user must be defined in config",
}
}

for _, u := range conf.Users {
Expand All @@ -57,7 +58,10 @@ func (conf *Config) BasicCheck() error {
}

if allCmds && len(u.Cmds) > 1 {
return tte.ErrSpecificAndAllCommandSameAtTime
return BasicCheckError{
reason: "you can't use all (*) commands and specific commands" +
" permission for one user at the same time",
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
tte "github.com/zurvan-lab/TimeTrace/utils/errors"
)

func TestDefaultConfig(t *testing.T) {
Expand Down Expand Up @@ -56,10 +55,10 @@ func TestBasicCheck(t *testing.T) {

c.Users = []User{}
err = c.BasicCheck()
assert.Error(t, tte.ErrInvalidUsers, err)
assert.Error(t, BasicCheckError{}, err)

c.Users = []User{DefaultConfig().Users[0]}
c.Users[0].Cmds = []string{"*", "GET"}
err = c.BasicCheck()
assert.Error(t, tte.ErrSpecificAndAllCommandSameAtTime, err)
assert.Error(t, BasicCheckError{}, err)
}
11 changes: 11 additions & 0 deletions config/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

import "fmt"

type BasicCheckError struct {
reason string
}

func (e BasicCheckError) Error() string {
return fmt.Sprintf("config basic check failed: %s", e.reason)
}
2 changes: 1 addition & 1 deletion core/TQL/execute/execute.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package execute

import "github.com/zurvan-lab/TimeTrace/core/database"
import "github.com/zurvan-lab/timetrace/core/database"

type Executor func(database.IDataBase, []string) string

Expand Down
6 changes: 3 additions & 3 deletions core/TQL/execute/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/zurvan-lab/TimeTrace/config"
"github.com/zurvan-lab/TimeTrace/core/TQL/parser"
"github.com/zurvan-lab/TimeTrace/core/database"
"github.com/zurvan-lab/timetrace/config"
"github.com/zurvan-lab/timetrace/core/TQL/parser"
"github.com/zurvan-lab/timetrace/core/database"
)

func TestExecute(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion core/TQL/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package core
import (
"strings"

"github.com/zurvan-lab/TimeTrace/core/database"
"github.com/zurvan-lab/timetrace/core/database"
)

// parsing TQL queries. see: docs/TQL.
Expand Down
2 changes: 1 addition & 1 deletion core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/zurvan-lab/TimeTrace/config"
"github.com/zurvan-lab/timetrace/config"
)

type Database struct {
Expand Down
2 changes: 1 addition & 1 deletion core/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/zurvan-lab/TimeTrace/config"
"github.com/zurvan-lab/timetrace/config"
)

func TestDataBase(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions core/server/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package server

import "fmt"

type AuthenticateError struct {
reason string
}

func (e AuthenticateError) Error() string {
return fmt.Sprintf("auth error: %s", e.reason)
}
Loading

0 comments on commit 0a66230

Please sign in to comment.