sql error handling: no rows + duplicated key

This commit is contained in:
2023-10-20 21:34:20 +02:00
parent 513b048e49
commit d12c55d740
14 changed files with 88 additions and 2564 deletions

View File

@@ -8,6 +8,8 @@ import (
"github.com/paulmach/orb/geojson"
"solidarity-service/internal"
"solidarity-service/servers/grpc/proto"
"solidarity-service/utils"
"strings"
)
func (s *SolidarityServiceServerImpl) SetDriverRegularAvailabilities(ctx context.Context, req *proto.DriverRegularAvailabilities) (resp *proto.DriverAvailabilitiesResponse, err error) {
@@ -76,6 +78,9 @@ func (s *SolidarityServiceServerImpl) SetDriverRegularAvailabilities(ctx context
err = s.Handler.SetDriverAvailabilities(context.Background(), driver)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
err = errors.New("ID is already used")
}
return &proto.DriverAvailabilitiesResponse{
Success: false,
}, err
@@ -151,6 +156,9 @@ func (s *SolidarityServiceServerImpl) SetDriverPunctualAvailabilities(ctx contex
err = s.Handler.SetDriverAvailabilities(context.Background(), driver)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
err = errors.New("ID is already used")
}
return &proto.DriverAvailabilitiesResponse{
Success: false,
}, err
@@ -172,6 +180,9 @@ func (s *SolidarityServiceServerImpl) CreateBooking(ctx context.Context, req *pr
}
passenger, driver, err := s.Handler.CreateBooking(context.Background(), bookingRequest)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
err = errors.New("ID is already used")
}
return nil, err
}
duration, err := s.Handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)
@@ -180,7 +191,9 @@ func (s *SolidarityServiceServerImpl) CreateBooking(ctx context.Context, req *pr
}
distance := s.Handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)
priceType := proto.PriceType_FREE
resp = &proto.CreateBookingResponse{}
resp = &proto.CreateBookingResponse{
Booking: &proto.Booking{},
}
resp.Booking = &proto.Booking{
Id: bookingRequest.ID,
Driver: &proto.User{
@@ -239,6 +252,9 @@ func (s *SolidarityServiceServerImpl) UpdateBooking(ctx context.Context, req *pr
bookingStatus := internal.BookingStatus(req.Status.String())
err = s.Handler.UpdateBooking(context.Background(), req.BookingId, bookingStatus)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_NO_ROWS) {
err = errors.New("invalid ID")
}
return &proto.UpdateBookingResponse{
Success: false,
}, err
@@ -254,13 +270,18 @@ func (s *SolidarityServiceServerImpl) GetBooking(ctx context.Context, req *proto
}
booking, passenger, driver, err := s.Handler.GetBooking(context.Background(), req.BookingId)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_NO_ROWS) {
err = errors.New("invalid ID")
}
return nil, err
}
duration, err := s.Handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)
if err != nil {
duration = 0
}
resp = &proto.GetBookingResponse{}
resp = &proto.GetBookingResponse{
Booking: &proto.Booking{},
}
distance := s.Handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)
priceType := proto.PriceType_FREE
resp.Booking = &proto.Booking{
@@ -319,9 +340,14 @@ func (s *SolidarityServiceServerImpl) GetBookingsByStatus(ctx context.Context, r
}
bookings, err := s.Handler.GetBookingsByStatus(context.Background(), req.Status.String(), req.Type.String(), req.UserId)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_NO_ROWS) {
err = errors.New("invalid ID")
}
return nil, err
}
resp = &proto.GetBookingsByStatusResponse{}
resp = &proto.GetBookingsByStatusResponse{
Booking: []*proto.Booking{},
}
responses := []*proto.Booking{}
for _, v := range bookings {
passenger, err := s.Handler.GetPassenger(context.Background(), v.Passenger.ID)
@@ -404,7 +430,9 @@ func (s *SolidarityServiceServerImpl) DriverJourneys(ctx context.Context, req *p
if err != nil {
return nil, err
}
resp = &proto.DriverJourneysResponse{}
resp = &proto.DriverJourneysResponse{
DriverJourneys: []*proto.DriverJourney{},
}
response := []*proto.DriverJourney{}
for _, v := range drivers {
temp := &proto.DriverJourney{}
@@ -536,9 +564,12 @@ func (s *SolidarityServiceServerImpl) SetPassengerTrip(ctx context.Context, req
err = s.Handler.SetPassengerTrip(context.Background(), passenger)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
err = errors.New("ID is already used")
}
return &proto.PassengerTripResponse{
Success: false,
}, nil
}, err
}
return &proto.PassengerTripResponse{
Success: true,