2 Commits

Author SHA1 Message Date
7c348629d6 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.
2025-02-23 11:51:16 +03:00
58b1c67b97 Update module path to include version suffix
The module path was updated to align with Go versioning conventions by appending "/v2". This change ensures compatibility with Go's module versioning system and properly reflects the major version update.
2025-02-22 22:16:52 +03:00
4 changed files with 27 additions and 9 deletions

View File

@ -3,7 +3,7 @@ package main
import (
"errors"
"fmt"
"gitea.unprism.ru/KRBL/n9m"
"gitea.unprism.ru/KRBL/n9m/v2"
"io"
"net"
"os"

2
go.mod
View File

@ -1,4 +1,4 @@
module gitea.unprism.ru/KRBL/n9m
module gitea.unprism.ru/KRBL/n9m/v2
go 1.21.3

View File

@ -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 &notFoundError{
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 &notFoundError{
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 &notFoundError{
message: fmt.Sprintf("payload type %d", pack.pack.PayloadType),
}
}
return processFunc(pack, pack.pack)

View File

@ -2,7 +2,7 @@ package test
import (
"fmt"
"gitea.unprism.ru/KRBL/n9m"
"gitea.unprism.ru/KRBL/n9m/v2"
"testing"
)