Skip to content

Commit 74dec87

Browse files
committed
improve error handling
1 parent bfc0dc1 commit 74dec87

File tree

4 files changed

+8
-14
lines changed

4 files changed

+8
-14
lines changed

creational/abstract_factory/factory.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package factory
22

33
import (
4-
"errors"
54
"fmt"
65
)
76

@@ -53,14 +52,14 @@ func (i *ItalianFactory) Build(v int) (Vehicle, error) {
5352
case CarWithFiveWheelModel:
5453
return new(CarWithFiveWheelType), nil
5554
}
56-
return nil, errors.New(fmt.Sprintf("No Italian cars of type %d\n", v))
55+
return nil, fmt.Errorf("No Italian cars of type %d\n", v)
5756
}
5857

5958
func BuildFactory(f int) (VehicleFactory, error) {
6059
switch f {
6160
case ItalianType:
6261
return new(ItalianFactory), nil
6362
default:
64-
return nil, errors.New(fmt.Sprintf("No factory with id %d\n", f))
63+
return nil, fmt.Errorf("No factory with id %d\n", f)
6564
}
6665
}

creational/factory/factory.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package factory
22

3-
import "errors"
3+
import "fmt"
44

55
const (
66
Italian = 1
@@ -18,9 +18,9 @@ func GetTranslator(m int) (Translator, error) {
1818
case English:
1919
return new(EnglishGreeting), nil
2020
default:
21-
return nil, errors.New("Unknown building")
21+
return nil, fmt.Errorf("Unknown building")
2222
}
23-
return nil, errors.New("Not implemented yet")
23+
return nil, fmt.Errorf("Not implemented yet")
2424
}
2525

2626
type ItalianGreeting struct{}

creational/prototype/prototype.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package prototype
22

3-
import "errors"
3+
import "fmt"
44

55
type ShirtCloner interface {
66
GetClone(s int) (ItemInfoGetter, error)
@@ -24,7 +24,7 @@ func (s *ShirtsCache) GetClone(m int) (ItemInfoGetter, error) {
2424
newItem := *whitePrototype
2525
return &newItem, nil
2626
}
27-
return nil, errors.New("Not implemented yet")
27+
return nil, fmt.Errorf("Not implemented yet")
2828
}
2929

3030
type ItemInfoGetter interface {

runtests.sh

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
#!/bin/bash
2-
go test creational/abstract_factory/*.go
3-
go test creational/builder/*.go
4-
go test creational/factory/*.go
5-
go test creational/pool/*.go
6-
go test creational/prototype/*.go
7-
go test creational/singleton/*.go
2+
go test ./...

0 commit comments

Comments
 (0)