Skip to content

Commit

Permalink
add errors to support console
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Oct 6, 2024
1 parent bdef0b4 commit 920e9fd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
7 changes: 7 additions & 0 deletions errors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ var (
StorageFacadeNotSet = New("storage facade is not initialized")
InvalidHttpContext = New("invalid http context")

ConsoleFileAlreadyExists = New("the %s already exists. Use the --force or -f flag to overwrite").SetModule(ModuleConsole)
ConsoleEmptyFieldValue = New("the %s name cannot be empty").SetModule(ModuleConsole)

DockerUnknownContainerType = New("unknown container type")
DockerInsufficientDatabaseContainers = New("the number of database container is not enough, expect: %d, got: %d")
DockerDatabaseContainerCountZero = New("the number of database container must be greater than 0")

AuthEmptySecret = New("authentication secret is missing or required")
AuthInvalidClaims = New("authentication token contains invalid claims")
AuthInvalidKey = New("authentication key is invalid")
Expand Down
1 change: 1 addition & 0 deletions errors/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errors
var (
ModuleAuth = "auth"
ModuleCache = "cache"
ModuleConsole = "console"
ModuleCrypt = "crypt"
ModuleEvent = "event"
ModuleFacade = "facade"
Expand Down
11 changes: 6 additions & 5 deletions support/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)
Expand All @@ -22,7 +23,7 @@ func NewMake(ctx console.Context, ttype, name, root string) (*Make, error) {
name, err = ctx.Ask(fmt.Sprintf("Enter the %s name", ttype), console.AskOption{
Validate: func(s string) error {
if s == "" {
return fmt.Errorf("the %s name cannot be empty", ttype)
return errors.ConsoleEmptyFieldValue.Args(ttype)

Check warning on line 26 in support/console/console.go

View check run for this annotation

Codecov / codecov/patch

support/console/console.go#L26

Added line #L26 was not covered by tests
}

return nil
Expand All @@ -33,16 +34,16 @@ func NewMake(ctx console.Context, ttype, name, root string) (*Make, error) {
}
}

make := &Make{
m := &Make{
name: name,
root: root,
}

if !ctx.OptionBool("force") && file.Exists(make.GetFilePath()) {
return nil, fmt.Errorf("the %s already exists. Use the --force or -f flag to overwrite", ttype)
if !ctx.OptionBool("force") && file.Exists(m.GetFilePath()) {
return nil, errors.ConsoleFileAlreadyExists.Args(ttype)
}

return make, nil
return m, nil
}

func (m *Make) GetFilePath() string {
Expand Down
8 changes: 4 additions & 4 deletions support/console/console_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package console

import (
"errors"
"os"
"path/filepath"
"testing"
Expand All @@ -10,6 +9,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"

"github.com/goravel/framework/errors"
consolemocks "github.com/goravel/framework/mocks/console"
"github.com/goravel/framework/support/file"
)
Expand Down Expand Up @@ -81,10 +81,10 @@ func TestNewMake(t *testing.T) {
name: "Sad path - name is empty",
setup: func() {
name = ""
mockCtx.EXPECT().Ask("Enter the rule name", mock.Anything).Return("", errors.New("the rule name cannot be empty")).Once()
mockCtx.EXPECT().Ask("Enter the rule name", mock.Anything).Return("", errors.ConsoleEmptyFieldValue.Args("rule")).Once()
},
expectMake: nil,
expectError: errors.New("the rule name cannot be empty"),
expectError: errors.ConsoleEmptyFieldValue.Args("rule"),
},
{
name: "Sad path - name already exists",
Expand All @@ -94,7 +94,7 @@ func TestNewMake(t *testing.T) {
mockCtx.EXPECT().OptionBool("force").Return(false).Once()
},
expectMake: nil,
expectError: errors.New("the rule already exists. Use the --force or -f flag to overwrite"),
expectError: errors.ConsoleFileAlreadyExists.Args("rule"),
},
{
name: "Happy path - name already exists, but force is true",
Expand Down
7 changes: 4 additions & 3 deletions support/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/errors"
)

// Define different test model, to improve the local testing speed.
Expand Down Expand Up @@ -67,7 +68,7 @@ func Sqlites(num int) []testing.DatabaseDriver {

func Database(containerType ContainerType, database, username, password string, num int) []testing.DatabaseDriver {
if num <= 0 {
panic("the number of database container must be greater than 0")
panic(errors.DockerDatabaseContainerCountZero)
}

var drivers []testing.DatabaseDriver
Expand Down Expand Up @@ -95,7 +96,7 @@ func Database(containerType ContainerType, database, username, password string,
}

if len(drivers) != num {
panic(fmt.Sprintf("the number of database container is not enough, expect: %d, got: %d", num, len(drivers)))
panic(errors.DockerInsufficientDatabaseContainers.Args(num, len(drivers)))

Check warning on line 99 in support/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

support/docker/docker.go#L99

Added line #L99 was not covered by tests
}

for _, driver := range drivers {
Expand All @@ -118,7 +119,7 @@ func DatabaseDriver(containerType ContainerType, database, username, password st
case ContainerTypeSqlite:
return NewSqliteImpl(database)
default:
panic("unknown container type")
panic(errors.DockerUnknownContainerType)

Check warning on line 122 in support/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

support/docker/docker.go#L122

Added line #L122 was not covered by tests
}
}

Expand Down

0 comments on commit 920e9fd

Please sign in to comment.