Skip to content

fix: error typos and introduce error types #143

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

Merged
merged 1 commit into from
Nov 25, 2024
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
13 changes: 9 additions & 4 deletions embedded_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (

var mu sync.Mutex

var (
ErrServerNotStarted = errors.New("server has not been started")
ErrServerAlreadyStarted = errors.New("server is already started")
)

// EmbeddedPostgres maintains all configuration and runtime functions for maintaining the lifecycle of one Postgres process.
type EmbeddedPostgres struct {
config Config
Expand Down Expand Up @@ -63,7 +68,7 @@ func newDatabaseWithConfig(config Config) *EmbeddedPostgres {
//nolint:funlen
func (ep *EmbeddedPostgres) Start() error {
if ep.started {
return errors.New("server is already started")
return ErrServerAlreadyStarted
}

if err := ensurePortAvailable(ep.config.port); err != nil {
Expand Down Expand Up @@ -124,7 +129,7 @@ func (ep *EmbeddedPostgres) Start() error {
if !reuseData {
if err := ep.createDatabase(ep.config.port, ep.config.username, ep.config.password, ep.config.database); err != nil {
if stopErr := stopPostgres(ep); stopErr != nil {
return fmt.Errorf("unable to stop database casused by error %s", err)
return fmt.Errorf("unable to stop database caused by error %s", err)
}

return err
Expand All @@ -133,7 +138,7 @@ func (ep *EmbeddedPostgres) Start() error {

if err := healthCheckDatabaseOrTimeout(ep.config); err != nil {
if stopErr := stopPostgres(ep); stopErr != nil {
return fmt.Errorf("unable to stop database casused by error %s", err)
return fmt.Errorf("unable to stop database caused by error %s", err)
}

return err
Expand Down Expand Up @@ -177,7 +182,7 @@ func (ep *EmbeddedPostgres) cleanDataDirectoryAndInit() error {
// Stop will try to stop the Postgres process gracefully returning an error when there were any problems.
func (ep *EmbeddedPostgres) Stop() error {
if !ep.started {
return errors.New("server has not been started")
return ErrServerNotStarted
}

if err := stopPostgres(ep); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions embedded_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func Test_ErrorWhenStopCalledBeforeStart(t *testing.T) {

err := database.Stop()

assert.EqualError(t, err, "server has not been started")
assert.ErrorIs(t, err, ErrServerNotStarted)
}

func Test_ErrorWhenStartCalledWhenAlreadyStarted(t *testing.T) {
Expand All @@ -206,7 +206,7 @@ func Test_ErrorWhenStartCalledWhenAlreadyStarted(t *testing.T) {
assert.NoError(t, err)

err = database.Start()
assert.EqualError(t, err, "server is already started")
assert.ErrorIs(t, err, ErrServerAlreadyStarted)
}

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