@@ -4,21 +4,23 @@ import (
44 "context"
55
66 gopher "github.com/friendsofgo/gopherapi/pkg"
7+ "github.com/friendsofgo/gopherapi/pkg/log"
78)
89
910// Service provides fetching operations.
1011type Service interface {
1112 FetchGophers (ctx context.Context ) ([]gopher.Gopher , error )
12- FetchGopherByID (ctx context.Context , ID string ) ( * gopher.Gopher , error )
13+ FetchGopherByID (ctx context.Context , ID string ) * gopher.Gopher
1314}
1415
1516type service struct {
1617 repository gopher.Repository
18+ logger * log.Logger
1719}
1820
1921// NewService creates a fetching service with the necessary dependencies
20- func NewService (repository gopher.Repository ) Service {
21- return & service {repository }
22+ func NewService (repository gopher.Repository , logger * log. Logger ) Service {
23+ return & service {repository , logger }
2224}
2325
2426// FetchGophers returns all gophers
@@ -27,6 +29,14 @@ func (s *service) FetchGophers(ctx context.Context) ([]gopher.Gopher, error) {
2729}
2830
2931// FetchGopherByID returns a gopher
30- func (s * service ) FetchGopherByID (ctx context.Context , ID string ) (* gopher.Gopher , error ) {
31- return s .repository .FetchGopherByID (ctx , ID )
32+ func (s * service ) FetchGopherByID (ctx context.Context , ID string ) * gopher.Gopher {
33+ g , err := s .repository .FetchGopherByID (ctx , ID )
34+
35+ // This error can be any error type of our repository
36+ if err != nil {
37+ s .logger .UnexpectedError (ctx , err )
38+ return nil
39+ }
40+
41+ return g
3242}
0 commit comments