Skip to content

Commit 9ff0024

Browse files
authored
fix: error types and introduce error types (#143)
1 parent 06253c6 commit 9ff0024

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

embedded_postgres.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import (
1414

1515
var mu sync.Mutex
1616

17+
var (
18+
ErrServerNotStarted = errors.New("server has not been started")
19+
ErrServerAlreadyStarted = errors.New("server is already started")
20+
)
21+
1722
// EmbeddedPostgres maintains all configuration and runtime functions for maintaining the lifecycle of one Postgres process.
1823
type EmbeddedPostgres struct {
1924
config Config
@@ -63,7 +68,7 @@ func newDatabaseWithConfig(config Config) *EmbeddedPostgres {
6368
//nolint:funlen
6469
func (ep *EmbeddedPostgres) Start() error {
6570
if ep.started {
66-
return errors.New("server is already started")
71+
return ErrServerAlreadyStarted
6772
}
6873

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

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

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

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

183188
if err := stopPostgres(ep); err != nil {

embedded_postgres_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func Test_ErrorWhenStopCalledBeforeStart(t *testing.T) {
190190

191191
err := database.Stop()
192192

193-
assert.EqualError(t, err, "server has not been started")
193+
assert.ErrorIs(t, err, ErrServerNotStarted)
194194
}
195195

196196
func Test_ErrorWhenStartCalledWhenAlreadyStarted(t *testing.T) {
@@ -206,7 +206,7 @@ func Test_ErrorWhenStartCalledWhenAlreadyStarted(t *testing.T) {
206206
assert.NoError(t, err)
207207

208208
err = database.Start()
209-
assert.EqualError(t, err, "server is already started")
209+
assert.ErrorIs(t, err, ErrServerAlreadyStarted)
210210
}
211211

212212
func Test_ErrorWhenCannotStartPostgresProcess(t *testing.T) {

0 commit comments

Comments
 (0)