Add custom notFoundError type and improve error handling
Introduce the notFoundError struct to provide more detailed error messages for missing handlers like alarms, JSON operations, and payload types. Update error handling to leverage the new custom type and use errors.As for better flexibility. Additionally, update module imports to version v2 in relevant files.
This commit is contained in:
parent
58b1c67b97
commit
7c348629d6
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gitea.unprism.ru/KRBL/n9m"
|
||||
"gitea.unprism.ru/KRBL/n9m/v2"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
|
30
smart.go
30
smart.go
@ -1,10 +1,19 @@
|
||||
package n9m
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
type notFoundError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e *notFoundError) Error() string {
|
||||
return fmt.Sprintf("not found %s", e.message)
|
||||
}
|
||||
|
||||
func NewSmartPackage(conn net.Conn) *SmartPackage {
|
||||
return &SmartPackage{
|
||||
pack: Package{},
|
||||
@ -42,7 +51,9 @@ func (pack *SmartPackage) handleAlarm() (err error) {
|
||||
var processFunc AlarmProcessFunc
|
||||
var ok bool
|
||||
if processFunc, ok = pack.alarmProcess[params.AlarmType]; !ok {
|
||||
return fmt.Errorf("unhanled alarm")
|
||||
return ¬FoundError{
|
||||
message: fmt.Sprintf("alarm %d", params.AlarmType),
|
||||
}
|
||||
}
|
||||
|
||||
var response SendAlarmInfoResponse
|
||||
@ -62,28 +73,35 @@ func (pack *SmartPackage) handleJson() (err error) {
|
||||
return fmt.Errorf("invalid json payload type")
|
||||
}
|
||||
|
||||
if err = pack.handleAlarm(); err == nil {
|
||||
var nfErr *notFoundError
|
||||
if err = pack.handleAlarm(); err == nil || errors.As(err, &nfErr) {
|
||||
return
|
||||
}
|
||||
|
||||
var processFunc ProcessFunc
|
||||
var ok bool
|
||||
if processFunc, ok = pack.jsonProcess[fmt.Sprintf("%s:%s", pack.pack.Payload.Module, pack.pack.Payload.Operation)]; !ok {
|
||||
return fmt.Errorf("unhanled operation")
|
||||
var key = fmt.Sprintf("%s:%s", pack.pack.Payload.Module, pack.pack.Payload.Operation)
|
||||
if processFunc, ok = pack.jsonProcess[key]; !ok {
|
||||
return ¬FoundError{
|
||||
message: fmt.Sprintf("json %s", key),
|
||||
}
|
||||
}
|
||||
|
||||
return processFunc(pack, pack.pack)
|
||||
}
|
||||
|
||||
func (pack *SmartPackage) handle() (err error) {
|
||||
if err = pack.handleJson(); err == nil {
|
||||
var nfErr *notFoundError
|
||||
if err = pack.handleJson(); err == nil || errors.As(err, &nfErr) {
|
||||
return
|
||||
}
|
||||
|
||||
var processFunc ProcessFunc
|
||||
var ok bool
|
||||
if processFunc, ok = pack.payloadProcess[pack.pack.PayloadType]; !ok {
|
||||
return fmt.Errorf("unhanled payload type")
|
||||
return ¬FoundError{
|
||||
message: fmt.Sprintf("payload type %d", pack.pack.PayloadType),
|
||||
}
|
||||
}
|
||||
|
||||
return processFunc(pack, pack.pack)
|
||||
|
@ -2,7 +2,7 @@ package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.unprism.ru/KRBL/n9m"
|
||||
"gitea.unprism.ru/KRBL/n9m/v2"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user