Skip to content

Commit

Permalink
tracking errors are reported to the user
Browse files Browse the repository at this point in the history
  • Loading branch information
dir01 committed Sep 29, 2023
1 parent ac8c1a2 commit 1d8a39a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
7 changes: 7 additions & 0 deletions core/parcels_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"

Expand All @@ -11,6 +12,8 @@ import (
"go.uber.org/zap"
)

var ErrNotFound = errors.New("not found")

type ParcelsAPI struct {
apiURL string
}
Expand All @@ -28,6 +31,10 @@ func (api *ParcelsAPI) GetTrackingInfo(ctx context.Context, trackingNumber strin
return nil, err
}

if resp.StatusCode == http.StatusNotFound {
return nil, ErrNotFound
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down
23 changes: 16 additions & 7 deletions core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ type ServiceImpl struct {
updatesChan chan TrackingUpdate
}

func (s *ServiceImpl) Updates() chan TrackingUpdate {
return s.updatesChan
}

type Storage interface {
SaveTracking(ctx context.Context, tracking *Tracking) (*Tracking, error)
GetTrackingsLastPolledBefore(ctx context.Context, t time.Time) ([]*Tracking, error)
Expand All @@ -67,6 +63,11 @@ type TrackingUpdate struct {
DisplayName string
NewTrackingInfos []*parcels_api.TrackingInfo
NewTrackingEvents []*parcels_api.TrackingEvent
TrackingErrors []error
}

func (s *ServiceImpl) Updates() chan TrackingUpdate {
return s.updatesChan
}

func (s *ServiceImpl) Start(ctx context.Context) {
Expand Down Expand Up @@ -107,7 +108,7 @@ func (s *ServiceImpl) Track(ctx context.Context, userID int64, trackingNumber st
if tracking, err := s.storage.SaveTracking(ctx, tracking); err == nil {
zapFields := append(zapFields, zap.Int64("tracking_id", tracking.ID), zaperr.ToField(err))
s.logger.Info("tracking added", zapFields...)
go s.fetchTrackingInfo(ctx, tracking)
go s.fetchTrackingInfo(ctx, tracking, true)
return nil
} else {
return zaperr.Wrap(err, "failed to add tracking", zapFields...)
Expand All @@ -117,7 +118,7 @@ func (s *ServiceImpl) Track(ctx context.Context, userID int64, trackingNumber st
// fetchTrackingInfo fetches the tracking info from parcels service
// and updates the tracking in the storage if required
// it also publishes any updates to the user
func (s *ServiceImpl) fetchTrackingInfo(ctx context.Context, tracking *Tracking) {
func (s *ServiceImpl) fetchTrackingInfo(ctx context.Context, tracking *Tracking, reportErrors bool) {
zapFields := []zap.Field{
zap.Any("tracking", tracking),
}
Expand All @@ -126,6 +127,14 @@ func (s *ServiceImpl) fetchTrackingInfo(ctx context.Context, tracking *Tracking)
fetchedTrackingInfos, err := s.parcelsAPI.GetTrackingInfo(ctx, tracking.TrackingNumber)
if err != nil {
s.logger.Error("failed to fetch tracking info", append(zapFields, zaperr.ToField(err))...)
if reportErrors {
s.updatesChan <- TrackingUpdate{
TrackingNumber: tracking.TrackingNumber,
UserID: tracking.UserID,
DisplayName: tracking.DisplayName,
TrackingErrors: []error{err},
}
}
return
}

Expand Down Expand Up @@ -164,7 +173,7 @@ func (s *ServiceImpl) poll(ctx context.Context) {
}
s.logger.Info("polling", zap.Int("trackings_count", len(trackings)))
for _, tracking := range trackings {
s.fetchTrackingInfo(ctx, tracking)
s.fetchTrackingInfo(ctx, tracking, false)
}
}

Expand Down

0 comments on commit 1d8a39a

Please sign in to comment.