fix(handlers): add error handling for swallowed repo/service errors

Several handler files discarded errors from repository and service
calls, creating nil-dereference risks and silent data corruption:

- guild_adventure: 3 GetByCharID calls could panic on nil guild
- gacha: GetGachaPoints silently returned zero balances on DB error
- house: HasApplication called before nil check on guild;
  GetHouseContents error discarded with 7 return values
- distitem: 3 distRepo calls had no error logging
- guild_ops: Disband/Leave service errors were invisible
- shop: gacha type/weight/fpoint lookups had no error logging
- discord: bcrypt error could result in nil password being set
This commit is contained in:
Houmgaor
2026-02-24 13:55:49 +01:00
parent 2f92b4ff62
commit 8fead0b1f3
8 changed files with 221 additions and 19 deletions

View File

@@ -31,8 +31,18 @@ func (s *Server) onInteraction(ds *discordgo.Session, i *discordgo.InteractionCr
})
}
case "password":
password, _ := bcrypt.GenerateFromPassword([]byte(i.ApplicationCommandData().Options[0].StringValue()), 10)
err := s.userRepo.SetPasswordByDiscordID(i.Member.User.ID, password)
password, err := bcrypt.GenerateFromPassword([]byte(i.ApplicationCommandData().Options[0].StringValue()), 10)
if err != nil {
_ = ds.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Failed to hash password.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
err = s.userRepo.SetPasswordByDiscordID(i.Member.User.ID, password)
if err == nil {
_ = ds.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,

View File

@@ -32,7 +32,10 @@ func handleMsgMhfEnumerateDistItem(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfEnumerateDistItem)
bf := byteframe.NewByteFrame()
itemDists, _ := s.server.distRepo.List(s.charID, pkt.DistType)
itemDists, err := s.server.distRepo.List(s.charID, pkt.DistType)
if err != nil {
s.logger.Error("Failed to list item distributions", zap.Error(err))
}
bf.WriteUint16(uint16(len(itemDists)))
for _, dist := range itemDists {
@@ -108,7 +111,10 @@ func handleMsgMhfApplyDistItem(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfApplyDistItem)
bf := byteframe.NewByteFrame()
bf.WriteUint32(pkt.DistributionID)
distItems, _ := s.server.distRepo.GetItems(pkt.DistributionID)
distItems, err := s.server.distRepo.GetItems(pkt.DistributionID)
if err != nil {
s.logger.Error("Failed to get distribution items", zap.Error(err))
}
bf.WriteUint16(uint16(len(distItems)))
for _, item := range distItems {
bf.WriteUint8(item.ItemType)
@@ -126,7 +132,10 @@ func handleMsgMhfAcquireDistItem(s *Session, p mhfpacket.MHFPacket) {
if pkt.DistributionID > 0 {
err := s.server.distRepo.RecordAccepted(pkt.DistributionID, s.charID)
if err == nil {
distItems, _ := s.server.distRepo.GetItems(pkt.DistributionID)
distItems, err := s.server.distRepo.GetItems(pkt.DistributionID)
if err != nil {
s.logger.Error("Failed to get distribution items for acquisition", zap.Error(err))
}
for _, item := range distItems {
switch item.ItemType {
case 17:

View File

@@ -53,7 +53,12 @@ func handleMsgMhfGetGachaPlayHistory(s *Session, p mhfpacket.MHFPacket) {
func handleMsgMhfGetGachaPoint(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfGetGachaPoint)
fp, gp, gt, _ := s.server.userRepo.GetGachaPoints(s.userID)
fp, gp, gt, err := s.server.userRepo.GetGachaPoints(s.userID)
if err != nil {
s.logger.Error("Failed to get gacha points", zap.Error(err))
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 12))
return
}
resp := byteframe.NewByteFrame()
resp.WriteUint32(gp)
resp.WriteUint32(gt)
@@ -159,7 +164,10 @@ func handleMsgMhfPlayStepupGacha(s *Session, p mhfpacket.MHFPacket) {
func handleMsgMhfGetStepupStatus(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfGetStepupStatus)
status, _ := s.server.gachaService.GetStepupStatus(pkt.GachaID, s.charID, TimeAdjusted())
status, err := s.server.gachaService.GetStepupStatus(pkt.GachaID, s.charID, TimeAdjusted())
if err != nil {
s.logger.Error("Failed to get stepup status", zap.Error(err))
}
bf := byteframe.NewByteFrame()
bf.WriteUint8(status.Step)

View File

@@ -21,7 +21,12 @@ type GuildAdventure struct {
func handleMsgMhfLoadGuildAdventure(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfLoadGuildAdventure)
guild, _ := s.server.guildRepo.GetByCharID(s.charID)
guild, err := s.server.guildRepo.GetByCharID(s.charID)
if err != nil || guild == nil {
s.logger.Error("Failed to get guild for character", zap.Error(err))
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 1))
return
}
adventures, err := s.server.guildRepo.ListAdventures(guild.ID)
if err != nil {
s.logger.Error("Failed to get guild adventures from db", zap.Error(err))
@@ -45,7 +50,12 @@ func handleMsgMhfLoadGuildAdventure(s *Session, p mhfpacket.MHFPacket) {
func handleMsgMhfRegistGuildAdventure(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfRegistGuildAdventure)
guild, _ := s.server.guildRepo.GetByCharID(s.charID)
guild, err := s.server.guildRepo.GetByCharID(s.charID)
if err != nil || guild == nil {
s.logger.Error("Failed to get guild for character", zap.Error(err))
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
return
}
if err := s.server.guildRepo.CreateAdventure(guild.ID, pkt.Destination, TimeAdjusted().Unix(), TimeAdjusted().Add(6*time.Hour).Unix()); err != nil {
s.logger.Error("Failed to register guild adventure", zap.Error(err))
}
@@ -70,7 +80,12 @@ func handleMsgMhfChargeGuildAdventure(s *Session, p mhfpacket.MHFPacket) {
func handleMsgMhfRegistGuildAdventureDiva(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfRegistGuildAdventureDiva)
guild, _ := s.server.guildRepo.GetByCharID(s.charID)
guild, err := s.server.guildRepo.GetByCharID(s.charID)
if err != nil || guild == nil {
s.logger.Error("Failed to get guild for character", zap.Error(err))
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
return
}
if err := s.server.guildRepo.CreateAdventureWithCharge(guild.ID, pkt.Destination, pkt.Charge, TimeAdjusted().Unix(), TimeAdjusted().Add(1*time.Hour).Unix()); err != nil {
s.logger.Error("Failed to register guild adventure", zap.Error(err))
}

View File

@@ -27,7 +27,10 @@ func handleMsgMhfOperateGuild(s *Session, p mhfpacket.MHFPacket) {
switch pkt.Action {
case mhfpacket.OperateGuildDisband:
result, _ := s.server.guildService.Disband(s.charID, guild.ID)
result, err := s.server.guildService.Disband(s.charID, guild.ID)
if err != nil {
s.logger.Error("Failed to disband guild", zap.Error(err))
}
response := 0
if result != nil && result.Success {
response = 1
@@ -46,7 +49,10 @@ func handleMsgMhfOperateGuild(s *Session, p mhfpacket.MHFPacket) {
bf.WriteUint32(0)
}
case mhfpacket.OperateGuildLeave:
result, _ := s.server.guildService.Leave(s.charID, guild.ID, characterGuildInfo.IsApplicant, guild.Name)
result, err := s.server.guildService.Leave(s.charID, guild.ID, characterGuildInfo.IsApplicant, guild.Name)
if err != nil {
s.logger.Error("Failed to leave guild", zap.Error(err))
}
response := 0
if result != nil && result.Success {
response = 1

View File

@@ -147,8 +147,8 @@ func handleMsgMhfLoadHouse(s *Session, p mhfpacket.MHFPacket) {
// Guild verification
if state > 3 {
ownGuild, err := s.server.guildRepo.GetByCharID(s.charID)
isApplicant, _ := s.server.guildRepo.HasApplication(ownGuild.ID, s.charID)
if err == nil && ownGuild != nil {
isApplicant, _ := s.server.guildRepo.HasApplication(ownGuild.ID, s.charID)
othersGuild, err := s.server.guildRepo.GetByCharID(pkt.CharID)
if err == nil && othersGuild != nil {
if othersGuild.ID == ownGuild.ID && !isApplicant {
@@ -164,7 +164,12 @@ func handleMsgMhfLoadHouse(s *Session, p mhfpacket.MHFPacket) {
}
}
houseTier, houseData, houseFurniture, bookshelf, gallery, tore, garden, _ := s.server.houseRepo.GetHouseContents(pkt.CharID)
houseTier, houseData, houseFurniture, bookshelf, gallery, tore, garden, err := s.server.houseRepo.GetHouseContents(pkt.CharID)
if err != nil {
s.logger.Error("Failed to get house contents", zap.Error(err), zap.Uint32("charID", pkt.CharID))
doAckSimpleFail(s, pkt.AckHandle, make([]byte, 4))
return
}
if houseFurniture == nil {
houseFurniture = make([]byte, 20)
}
@@ -201,7 +206,10 @@ func handleMsgMhfLoadHouse(s *Session, p mhfpacket.MHFPacket) {
func handleMsgMhfGetMyhouseInfo(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfGetMyhouseInfo)
data, _ := s.server.houseRepo.GetMission(s.charID)
data, err := s.server.houseRepo.GetMission(s.charID)
if err != nil {
s.logger.Error("Failed to get myhouse mission", zap.Error(err))
}
if len(data) > 0 {
doAckBufSucceed(s, pkt.AckHandle, data)
} else {
@@ -343,7 +351,10 @@ func handleMsgMhfOperateWarehouse(s *Session, p mhfpacket.MHFPacket) {
switch pkt.Operation {
case 0:
var count uint8
itemNames, equipNames, _ := s.server.houseRepo.GetWarehouseNames(s.charID)
itemNames, equipNames, err := s.server.houseRepo.GetWarehouseNames(s.charID)
if err != nil {
s.logger.Error("Failed to get warehouse names", zap.Error(err))
}
bf.WriteUint32(0)
bf.WriteUint16(10000) // Usages
temp := byteframe.NewByteFrame()

View File

@@ -122,13 +122,19 @@ func handleMsgMhfEnumerateShop(s *Session, p mhfpacket.MHFPacket) {
case 2: // Actual gacha
bf := byteframe.NewByteFrame()
bf.WriteUint32(pkt.ShopID)
gachaType, _ := s.server.gachaRepo.GetShopType(pkt.ShopID)
gachaType, err := s.server.gachaRepo.GetShopType(pkt.ShopID)
if err != nil {
s.logger.Error("Failed to get gacha shop type", zap.Error(err))
}
entries, err := s.server.gachaRepo.GetAllEntries(pkt.ShopID)
if err != nil {
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 4))
return
}
divisor, _ := s.server.gachaRepo.GetWeightDivisor(pkt.ShopID)
divisor, err := s.server.gachaRepo.GetWeightDivisor(pkt.ShopID)
if err != nil {
s.logger.Error("Failed to get gacha weight divisor", zap.Error(err))
}
bf.WriteUint16(uint16(len(entries)))
for _, ge := range entries {
var items []GachaItem
@@ -262,7 +268,10 @@ func handleMsgMhfGetFpointExchangeList(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfGetFpointExchangeList)
bf := byteframe.NewByteFrame()
exchanges, _ := s.server.shopRepo.GetFpointExchangeList()
exchanges, err := s.server.shopRepo.GetFpointExchangeList()
if err != nil {
s.logger.Error("Failed to get fpoint exchange list", zap.Error(err))
}
var buyables uint16
for _, e := range exchanges {
if e.Buyable {