Skip to content

Commit

Permalink
fix: error types and introduce error types (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaus67 authored Nov 25, 2024
1 parent 06253c6 commit 9ff0024
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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

0 comments on commit 9ff0024

Please sign in to comment.