[PD1] Error handling project-wide
This commit is contained in:
parent
f5b3726673
commit
b918211736
13 changed files with 364 additions and 245 deletions
|
@ -2,7 +2,6 @@ package protocol
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -30,8 +29,8 @@ const (
|
|||
// Server sends requested message
|
||||
FlagAnswerGetMsg
|
||||
|
||||
// Server tells the client that the message was successfully sent
|
||||
FlagAnswerSendMsg
|
||||
// Server tells the client that the message was successfully sent
|
||||
FlagAnswerSendMsg
|
||||
|
||||
// Report an error
|
||||
FlagReportError
|
||||
|
@ -192,118 +191,118 @@ func NewAnswerGetMsgPacket(fromUID, toUID string, subject []byte, body []byte, t
|
|||
return NewPacket(FlagAnswerGetMsg, NewAnswerGetMsg(fromUID, toUID, subject, body, timestamp, last))
|
||||
}
|
||||
|
||||
func NewAnswerSendMsgPacket() Packet{
|
||||
//This packet has no body
|
||||
return NewPacket(FlagAnswerSendMsg,nil)
|
||||
func NewAnswerSendMsgPacket() Packet {
|
||||
//This packet has no body
|
||||
return NewPacket(FlagAnswerSendMsg, nil)
|
||||
}
|
||||
|
||||
func NewReportErrorPacket(errorMessage string) Packet {
|
||||
return NewPacket(FlagReportError, NewReportError(errorMessage))
|
||||
}
|
||||
|
||||
func UnmarshalGetUserCert(data PacketBody) GetUserCert {
|
||||
func UnmarshalGetUserCert(data PacketBody) (GetUserCert, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return GetUserCert{}, err
|
||||
}
|
||||
var packet GetUserCert
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into GetUserCert: %v", err))
|
||||
return GetUserCert{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
func UnmarshalGetUnreadMsgsInfo(data PacketBody) GetUnreadMsgsInfo {
|
||||
func UnmarshalGetUnreadMsgsInfo(data PacketBody) (GetUnreadMsgsInfo, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return GetUnreadMsgsInfo{}, err
|
||||
}
|
||||
var packet GetUnreadMsgsInfo
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into GetUnreadMsgsInfo: %v", err))
|
||||
return GetUnreadMsgsInfo{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
func UnmarshalGetMsg(data PacketBody) GetMsg {
|
||||
func UnmarshalGetMsg(data PacketBody) (GetMsg, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return GetMsg{}, err
|
||||
}
|
||||
var packet GetMsg
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into GetMsg: %v", err))
|
||||
return GetMsg{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
func UnmarshalSendMsg(data PacketBody) SendMsg {
|
||||
func UnmarshalSendMsg(data PacketBody) (SendMsg, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return SendMsg{}, err
|
||||
}
|
||||
var packet SendMsg
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into SendMsg: %v", err))
|
||||
return SendMsg{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
func UnmarshalAnswerGetUserCert(data PacketBody) AnswerGetUserCert {
|
||||
func UnmarshalAnswerGetUserCert(data PacketBody) (AnswerGetUserCert, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return AnswerGetUserCert{}, err
|
||||
}
|
||||
var packet AnswerGetUserCert
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into AnswerGetUserCert: %v", err))
|
||||
return AnswerGetUserCert{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
func UnmarshalUnreadMsgInfo(data PacketBody) MsgInfo {
|
||||
func UnmarshalUnreadMsgInfo(data PacketBody) (MsgInfo, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return MsgInfo{}, err
|
||||
}
|
||||
var packet MsgInfo
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into UnreadMsgInfo: %v", err))
|
||||
return MsgInfo{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
func UnmarshalAnswerGetUnreadMsgsInfo(data PacketBody) AnswerGetUnreadMsgsInfo {
|
||||
func UnmarshalAnswerGetUnreadMsgsInfo(data PacketBody) (AnswerGetUnreadMsgsInfo, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return AnswerGetUnreadMsgsInfo{}, err
|
||||
}
|
||||
var packet AnswerGetUnreadMsgsInfo
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into AnswerGetUnreadMsgsInfo: %v", err))
|
||||
return AnswerGetUnreadMsgsInfo{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
func UnmarshalAnswerGetMsg(data PacketBody) AnswerGetMsg {
|
||||
func UnmarshalAnswerGetMsg(data PacketBody) (AnswerGetMsg, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return AnswerGetMsg{}, err
|
||||
}
|
||||
var packet AnswerGetMsg
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into AnswerGetMsg: %v", err))
|
||||
return AnswerGetMsg{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
func UnmarshalReportError(data PacketBody) ReportError {
|
||||
func UnmarshalReportError(data PacketBody) (ReportError, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
return ReportError{}, err
|
||||
}
|
||||
var packet ReportError
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into AnswerGetMsg: %v", err))
|
||||
return ReportError{}, err
|
||||
}
|
||||
return packet
|
||||
return packet, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue