friends/nex/friends-wiiu/cancel_friend_request.go
Daniel López Guimaraes 8d8a05a7e2
Add missing error handling
Now database getters do error handling aswell, and in case of any error,
the NEX or GRPC method throws a proper error about it.

There are still some doubts on how to handle errors when a list of data
is processed, so for now skip that element on the list and continue.

Also add some constant errors to do better error handling.
2023-08-13 23:19:34 +01:00

54 lines
1.6 KiB
Go

package nex_friends_wiiu
import (
"github.com/PretendoNetwork/friends/database"
database_wiiu "github.com/PretendoNetwork/friends/database/wiiu"
"github.com/PretendoNetwork/friends/globals"
notifications_wiiu "github.com/PretendoNetwork/friends/notifications/wiiu"
nex "github.com/PretendoNetwork/nex-go"
friends_wiiu "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu"
)
func CancelFriendRequest(err error, client *nex.Client, callID uint32, id uint64) uint32 {
if err != nil {
globals.Logger.Error(err.Error())
return nex.Errors.FPD.InvalidArgument
}
pid, err := database_wiiu.DeleteFriendRequestAndReturnFriendPID(id)
if err != nil {
if err == database.ErrFriendRequestNotFound {
return nex.Errors.FPD.InvalidMessageID
} else {
globals.Logger.Critical(err.Error())
return nex.Errors.FPD.Unknown
}
}
connectedUser := globals.ConnectedUsers[pid]
if connectedUser != nil {
// This may send the friend removed notification, but they are the same.
go notifications_wiiu.SendFriendshipRemoved(connectedUser.Client, client.PID())
}
rmcResponse := nex.NewRMCResponse(friends_wiiu.ProtocolID, callID)
rmcResponse.SetSuccess(friends_wiiu.MethodCancelFriendRequest, nil)
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV0(client, nil)
responsePacket.SetVersion(0)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
globals.SecureServer.Send(responsePacket)
return 0
}