refactor: replace panic calls with structured error handling

Replace ~25 panic() calls in non-fatal code paths with proper
s.logger.Error + return patterns. Panics in handler code crashed
goroutines (caught by defer/recover but still disruptive) instead
of failing gracefully.

Key changes:
- SJISToUTF8 now returns (string, error); all 30+ callers updated
- Handler DB/IO panics replaced with log + return/ack fail
- Unhandled switch-case panics replaced with logger.Error
- Sign server Accept() panic replaced with log + continue
- Dead unreachable panic in guild_model.go removed
- deltacomp patch error logs and returns partial data

Panics intentionally kept: ByteFrame sentinel, unimplemented
packet stubs, os.Exit in main.go.
This commit is contained in:
Houmgaor
2026-02-20 19:11:41 +01:00
parent 06cb3afa57
commit d32e77efba
31 changed files with 141 additions and 130 deletions

View File

@@ -4,6 +4,8 @@ import (
"erupe-ce/common/byteframe"
"erupe-ce/common/mhfcourse"
"erupe-ce/network/mhfpacket"
"go.uber.org/zap"
)
// Temporary function to just return no results for a MSG_MHF_ENUMERATE* packet
@@ -62,6 +64,36 @@ func doAckSimpleFail(s *Session, ackHandle uint32, data []byte) {
})
}
// loadCharacterData loads a column from the characters table and sends it as
// a buffered ack response. If the data is empty/nil, defaultData is sent instead.
func loadCharacterData(s *Session, ackHandle uint32, column string, defaultData []byte) {
var data []byte
err := s.server.db.QueryRow("SELECT "+column+" FROM characters WHERE id = $1", s.charID).Scan(&data)
if err != nil {
s.logger.Error("Failed to load "+column, zap.Error(err))
}
if len(data) == 0 && defaultData != nil {
data = defaultData
}
doAckBufSucceed(s, ackHandle, data)
}
// saveCharacterData saves data to a column in the characters table with size
// validation, optional save dump, and a simple ack response.
func saveCharacterData(s *Session, ackHandle uint32, column string, data []byte, maxSize int) {
if maxSize > 0 && len(data) > maxSize {
s.logger.Warn("Payload too large for "+column, zap.Int("len", len(data)), zap.Int("max", maxSize))
doAckSimpleSucceed(s, ackHandle, make([]byte, 4))
return
}
dumpSaveData(s, data, column)
_, err := s.server.db.Exec("UPDATE characters SET "+column+"=$1 WHERE id=$2", data, s.charID)
if err != nil {
s.logger.Error("Failed to save "+column, zap.Error(err))
}
doAckSimpleSucceed(s, ackHandle, make([]byte, 4))
}
func updateRights(s *Session) {
rightsInt := uint32(2)
_ = s.server.db.QueryRow("SELECT rights FROM users u INNER JOIN characters c ON u.id = c.user_id WHERE c.id = $1", s.charID).Scan(&rightsInt)