sql error handling: no rows + duplicated key

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

File diff suppressed because it is too large Load Diff

View File

@ -1,23 +0,0 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.
# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

View File

@ -1,40 +0,0 @@
.openapi-generator-ignore
Dockerfile
README.md
api/openapi.yaml
go.mod
go/api.go
go/api_bookings_filter.go
go/api_bookings_filter_service.go
go/api_driver_availability_and_schedule.go
go/api_driver_availability_and_schedule_service.go
go/api_interact.go
go/api_interact_service.go
go/api_passenger_trip_request.go
go/api_passenger_trip_request_service.go
go/api_search.go
go/api_search_service.go
go/error.go
go/helpers.go
go/impl.go
go/logger.go
go/model__passenger_post_400_response.go
go/model_booking.go
go/model_booking_request.go
go/model_booking_status.go
go/model_car.go
go/model_driver_journey.go
go/model_driver_punctual_availabilities_request.go
go/model_driver_regular_availabilities_request.go
go/model_driver_trip.go
go/model_get_bookings_404_response.go
go/model_journey_schedule.go
go/model_passenger_trip_request.go
go/model_post_connections_request.go
go/model_preferences.go
go/model_price.go
go/model_punctual_availability_slot.go
go/model_regular_availability_slot.go
go/model_user.go
go/routers.go
main.go

File diff suppressed because it is too large Load Diff

View File

@ -44,7 +44,7 @@ func (s *BookingsFilterAPIService) FilterBookingsByStatus(ctx context.Context, o
if err != nil {
fmt.Println(err)
if strings.Contains(err.Error(), " no rows in result set") {
return Response(http.StatusBadRequest, "ID not found in the database"), nil
return Response(http.StatusBadRequest, "no related data recheck your parameters"), nil
} else {
return Response(http.StatusInternalServerError, nil), nil
}

View File

@ -19,6 +19,8 @@ import (
"solidarity-service/handler"
"solidarity-service/internal"
"solidarity-service/storage"
"solidarity-service/utils"
"strings"
)
// DriverAvailabilityAndScheduleAPIService is a service that implements the logic for the DriverAvailabilityAndScheduleAPIServicer
@ -77,6 +79,9 @@ func (s *DriverAvailabilityAndScheduleAPIService) DriverPunctualAvailabilitiesPo
driver.Car.Model = driverPunctualAvailabilitiesRequest.Car.Model
err := s.storage.CreateDriver(driver)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
return Response(http.StatusBadRequest, "ID already in use"), nil
}
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
} else {
return Response(201, "Punctual driver availabilities set successfully"), nil
@ -120,6 +125,9 @@ func (s *DriverAvailabilityAndScheduleAPIService) DriverRegularAvailabilitiesPos
driver.Car.Model = driverRegularAvailabilitiesRequest.Car.Model
err := s.storage.CreateDriver(driver)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
return Response(http.StatusBadRequest, "ID already in use"), nil
}
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
} else {
return Response(201, "Regular driver availabilities set successfully"), nil

View File

@ -18,6 +18,7 @@ import (
"solidarity-service/handler"
"solidarity-service/internal"
"solidarity-service/storage"
"solidarity-service/utils"
"strings"
)
@ -44,7 +45,7 @@ func (s *InteractAPIService) GetBookings(ctx context.Context, operator string, b
booking, err := s.storage.GetBooking(bookingId)
if err != nil {
if strings.Contains(err.Error(), " no rows in result set") {
return Response(http.StatusBadRequest, "ID not found in the database"), nil
return Response(http.StatusBadRequest, "no related data recheck your parameters"), nil
} else {
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
}
@ -114,7 +115,7 @@ func (s *InteractAPIService) PatchBookings(ctx context.Context, operator string,
fmt.Println(err)
if err != nil {
if strings.Contains(err.Error(), " no rows in result set") {
return Response(http.StatusBadRequest, "ID not found in the database"), nil
return Response(http.StatusBadRequest, "no related data recheck your params"), nil
}
return Response(http.StatusInternalServerError, nil), nil
}
@ -131,6 +132,9 @@ func (s *InteractAPIService) PostBookings(ctx context.Context, bookingRequest Bo
booking.Operator = bookingRequest.Operator
err := s.storage.CreateBooking(booking)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
return Response(http.StatusBadRequest, "ID already in use"), nil
}
return Response(http.StatusInternalServerError, nil), errors.New("PostBookings internal server error")
}
passenger, err := s.storage.GetPassenger(booking.Passenger_id)

View File

@ -19,6 +19,8 @@ import (
"solidarity-service/handler"
"solidarity-service/internal"
"solidarity-service/storage"
"solidarity-service/utils"
"strings"
)
// PassengerTripRequestAPIService is a service that implements the logic for the PassengerTripRequestAPIServicer
@ -73,6 +75,9 @@ func (s *PassengerTripRequestAPIService) PassengerPost(ctx context.Context, pass
passenger.Preferences.Music = passengerTripRequest.Preferences.Music
err := s.storage.CreatePassenger(passenger)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
return Response(http.StatusBadRequest, "ID already in use"), nil
}
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
} else {
return Response(201, "Trip request created"), nil

View File

@ -36,5 +36,5 @@ func Run(cfg *viper.Viper, handler *handler.SolidarityServiceHandler, storage st
router := openapi.NewRouter(BookingsFilterAPIController, DriverAvailabilityAndScheduleAPIController, InteractAPIController, PassengerTripRequestAPIController, SearchAPIController)
log.Fatal(http.ListenAndServe(":9999", router))
log.Fatal(http.ListenAndServe(cfg.GetString("services.solidarity-api.address"), router))
}

View File

@ -25,7 +25,6 @@ func main() {
routing_service_type = cfg.GetString("routing.type")
valhalla_base_url = cfg.GetString("routing.valhalla.base_url")
)
log.Info().Msg("Running " + service_name)
storageService, err := storage.NewStorage(cfg)
if err != nil {
@ -50,7 +49,7 @@ func main() {
}
if solidarity_api_enable {
log.Info().Msg("Running OCSS REST API")
log.Info().Msg("Running Interoperability REST API")
go api.Run(cfg, handler, storageService)
}

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,

View File

@ -80,20 +80,20 @@ func (s PostgresqlStorage) CreatePassenger(passenger internal.Passenger) (err er
if err != nil {
errMsg := "Postgresql Storage CreatePassenger Error encoding Preferences to JSON"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
departureJSON, err := json.Marshal(passenger.Passenger_departure_address)
if err != nil {
errMsg := "Postgresql Storage CreatePassenger Error encoding departure Feature to JSON"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
destinationJSON, err := json.Marshal(passenger.Passenger_destination_address)
if err != nil {
errMsg := "Postgresql Storage CreatePassenger Error converting destination Feature to JSON"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
_, err = s.DbConnection.Exec(fmt.Sprintf("INSERT INTO %s (passenger_id, passenger_departure_route, passenger_destination_route, alias, last_name, first_name, grade, picture, verified_identity, operator, preferences, passenger_pickup_date) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)", s.Tables["passengers"]),
passenger.Passenger.ID,
@ -111,7 +111,7 @@ func (s PostgresqlStorage) CreatePassenger(passenger internal.Passenger) (err er
if err != nil {
errMsg := "Postgresql Storage CreatePassenger Error inserting data into the database"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
return nil
}
@ -139,26 +139,26 @@ func (s PostgresqlStorage) GetPassenger(passengerID string) (passenger internal.
fmt.Println(err)
errMsg := "Postgresql Storage GetPassenger Error querying data from the database"
log.Error().Err(err).Msg(errMsg)
return passenger, errors.New(errMsg)
return passenger, errors.New(errMsg + err.Error())
}
err = json.Unmarshal(preferencesJSON, &passenger.Preferences)
if err != nil {
errMsg := "Postgresql Storage GetPassenger Error decoding Preferences from JSON"
log.Error().Err(err).Msg(errMsg)
return passenger, errors.New(errMsg)
return passenger, errors.New(errMsg + err.Error())
}
passenger.Passenger_destination_address, err = geojson.UnmarshalFeature(destination_address)
if err != nil {
errMsg := "Postgresql Storage GetPassenger Error decoding Passenger destination route into GeoJSON"
log.Error().Err(err).Msg(errMsg)
return passenger, errors.New(errMsg)
return passenger, errors.New(errMsg + err.Error())
}
passenger.Passenger_departure_address, err = geojson.UnmarshalFeature(departure_address)
if err != nil {
errMsg := "Postgresql Storage GetPassenger Error decoding Passenger departure route into GeoJSON"
log.Error().Err(err).Msg(errMsg)
return passenger, errors.New(errMsg)
return passenger, errors.New(errMsg + err.Error())
}
return passenger, nil
}
@ -174,19 +174,19 @@ func (s PostgresqlStorage) CreateDriver(driver internal.Driver) (err error) {
if err != nil {
errMsg := "Postgresql Storage CreateDriver Error encoding departure Feature to JSON"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
preferencesJSON, err := json.Marshal(driver.Preferences)
if err != nil {
errMsg := "Postgresql Storage CreateDriver Error encoding Preferences to JSON"
log.Error().Err(err).Msg(errMsg)
log.Error().Err(err).Msg(errMsg + err.Error())
return errors.New(errMsg)
}
carJSON, err := json.Marshal(driver.Car)
if err != nil {
errMsg := "Postgresql Storage CreateDriver Error encoding Car to JSON"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
if driver.Driver.Alias == "" || driver.Driver.Operator == "" {
errMsg := "Postgresql Storage CreateDriver empty alias or operator FQDN."
@ -209,14 +209,14 @@ func (s PostgresqlStorage) CreateDriver(driver internal.Driver) (err error) {
if err != nil {
errMsg := "Postgresql Storage CreateDriver error converting Punctual availabilities"
log.Error().Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
case internal.Regular:
availabilities, err = json.Marshal(driver.RegularAvailabilities)
if err != nil {
errMsg := "Postgresql Storage CreateDriver error converting Regular availabilities"
log.Error().Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
}
_, err = s.DbConnection.Exec(fmt.Sprintf("INSERT INTO %s (driver_id,driver_departure_route,driver_radius,last_name,first_name,grade,alias,picture,verified_identity,preferences,availabilities_type,availabilities,operator , car) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14)", s.Tables["drivers"]),
@ -239,7 +239,7 @@ func (s PostgresqlStorage) CreateDriver(driver internal.Driver) (err error) {
fmt.Println(err)
errMsg := "Postgresql Storage CreateDriver Error inserting data into the database"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
return nil
}
@ -269,26 +269,26 @@ func (s PostgresqlStorage) GetDriver(driverID string) (driver internal.Driver, e
if err != nil {
errMsg := "Postgresql Storage GetDriver Error querying data from the database"
log.Error().Err(err).Msg(errMsg)
return driver, errors.New(errMsg)
return driver, errors.New(errMsg + err.Error())
}
err = json.Unmarshal(preferencesJSON, &driver.Preferences)
if err != nil {
errMsg := "Postgresql Storage GetDriver Error decoding Preferences from JSON"
log.Error().Err(err).Msg(errMsg)
return driver, errors.New(errMsg)
return driver, errors.New(errMsg + err.Error())
}
err = json.Unmarshal(carJSON, &driver.Car)
if err != nil {
errMsg := "Postgresql Storage GetDriver Error decoding Car from JSON"
log.Error().Err(err).Msg(errMsg)
return driver, errors.New(errMsg)
return driver, errors.New(errMsg + err.Error())
}
driver.Driver_departure_address, err = geojson.UnmarshalFeature(departureAddress)
if err != nil {
errMsg := "Postgresql Storage GetDriver Error decoding Driver departure route into GeoJSON"
log.Error().Err(err).Msg(errMsg)
return driver, errors.New(errMsg)
return driver, errors.New(errMsg + err.Error())
}
switch driver.AvailabilitiesType {
@ -297,19 +297,19 @@ func (s PostgresqlStorage) GetDriver(driverID string) (driver internal.Driver, e
if err != nil {
errMsg := "Postgresql Storage GetDriver Error decoding Regular Availabilities from JSON"
log.Error().Err(err).Msg(errMsg)
return driver, errors.New(errMsg)
return driver, errors.New(errMsg + err.Error())
}
case internal.Punctual:
err = json.Unmarshal(availabilitiesJSON, &driver.PunctualAvailabilities)
if err != nil {
errMsg := "Postgresql Storage GetDriver Error decoding Punctual Availabilities from JSON"
log.Error().Err(err).Msg(errMsg)
return driver, errors.New(errMsg)
return driver, errors.New(errMsg + err.Error())
}
default:
errMsg := "Postgresql Storage GetDriver Invalid Availabilities Type"
log.Error().Msg(errMsg)
return driver, errors.New(errMsg)
return driver, errors.New(errMsg + err.Error())
}
return driver, nil
@ -345,7 +345,7 @@ func (s PostgresqlStorage) CreateBooking(booking internal.BookingRequest) (err e
if err != nil {
errMsg := "Postgresql Storage CreateBooking Error inserting data into the database"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
return nil
}
@ -358,7 +358,7 @@ func (s PostgresqlStorage) GetBooking(id string) (booking internal.Booking, err
&booking.Driver.ID,
&booking.Status,
)
fmt.Println(err)
if err != nil {
errMsg := "Postgresql Storage GetBooking Error getting booking"
log.Error().Err(err).Msg(errMsg)
@ -398,7 +398,7 @@ func (s PostgresqlStorage) UpdateBookingStatus(id string, status internal.Bookin
if err != nil {
errMsg := "Postgresql Storage UpdateBookingStatus Error updating booking status"
log.Error().Err(err).Msg(errMsg)
return errors.New(errMsg)
return errors.New(errMsg + err.Error())
}
return nil

6
utils/sql_errors.go Normal file
View File

@ -0,0 +1,6 @@
package utils
const (
SQL_NO_ROWS = "no rows in result set"
SQL_DUPLICATE = "duplicate key"
)