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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.unprism.ru/KRBL/n9m"
|
"gitea.unprism.ru/KRBL/n9m/v2"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
30
smart.go
30
smart.go
@ -1,10 +1,19 @@
|
|||||||
package n9m
|
package n9m
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"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 {
|
func NewSmartPackage(conn net.Conn) *SmartPackage {
|
||||||
return &SmartPackage{
|
return &SmartPackage{
|
||||||
pack: Package{},
|
pack: Package{},
|
||||||
@ -42,7 +51,9 @@ func (pack *SmartPackage) handleAlarm() (err error) {
|
|||||||
var processFunc AlarmProcessFunc
|
var processFunc AlarmProcessFunc
|
||||||
var ok bool
|
var ok bool
|
||||||
if processFunc, ok = pack.alarmProcess[params.AlarmType]; !ok {
|
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
|
var response SendAlarmInfoResponse
|
||||||
@ -62,28 +73,35 @@ func (pack *SmartPackage) handleJson() (err error) {
|
|||||||
return fmt.Errorf("invalid json payload type")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var processFunc ProcessFunc
|
var processFunc ProcessFunc
|
||||||
var ok bool
|
var ok bool
|
||||||
if processFunc, ok = pack.jsonProcess[fmt.Sprintf("%s:%s", pack.pack.Payload.Module, pack.pack.Payload.Operation)]; !ok {
|
var key = fmt.Sprintf("%s:%s", pack.pack.Payload.Module, pack.pack.Payload.Operation)
|
||||||
return fmt.Errorf("unhanled operation")
|
if processFunc, ok = pack.jsonProcess[key]; !ok {
|
||||||
|
return ¬FoundError{
|
||||||
|
message: fmt.Sprintf("json %s", key),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return processFunc(pack, pack.pack)
|
return processFunc(pack, pack.pack)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pack *SmartPackage) handle() (err error) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var processFunc ProcessFunc
|
var processFunc ProcessFunc
|
||||||
var ok bool
|
var ok bool
|
||||||
if processFunc, ok = pack.payloadProcess[pack.pack.PayloadType]; !ok {
|
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)
|
return processFunc(pack, pack.pack)
|
||||||
|
@ -2,7 +2,7 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.unprism.ru/KRBL/n9m"
|
"gitea.unprism.ru/KRBL/n9m/v2"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user