From 7cc564ff827c1bd1fcf0a0dc6788363290229000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20R=C3=BChl?= Date: Tue, 2 May 2023 18:51:40 +0200 Subject: [PATCH] refactor(plc4go): avoid panics if possible --- plc4go/internal/ads/Discoverer.go | 5 +- plc4go/internal/ads/model/Tag.go | 2 +- plc4go/internal/cbus/CBusMessageMapper.go | 45 ++++-- plc4go/internal/cbus/TagHandler.go | 16 +-- plc4go/internal/cbus/ValueHandler.go | 132 +++++++++--------- plc4go/internal/eip/Reader.go | 4 +- plc4go/internal/simulated/Connection.go | 4 +- .../pkg/api/cache/plcConnectionLease_test.go | 4 +- plc4go/pkg/api/driverManager.go | 6 - plc4go/spi/default/DefaultConnection.go | 10 +- plc4go/spi/default/DefaultConnection_test.go | 10 +- plc4go/spi/testutils/DriverTestRunner.go | 2 +- plc4go/spi/testutils/ManualTestRunner.go | 6 +- plc4go/spi/utils/ReadBufferByteBased.go | 2 +- plc4go/spi/utils/WriteBufferBoxBased.go | 3 +- plc4go/spi/utils/WriteBufferJsonBased.go | 42 +++--- plc4go/spi/values/WriteBufferPlcValueBased.go | 36 +++-- 17 files changed, 165 insertions(+), 164 deletions(-) diff --git a/plc4go/internal/ads/Discoverer.go b/plc4go/internal/ads/Discoverer.go index f1d4fa25aa2..c9ce8efeb86 100644 --- a/plc4go/internal/ads/Discoverer.go +++ b/plc4go/internal/ads/Discoverer.go @@ -23,6 +23,7 @@ import ( "context" "encoding/binary" "fmt" + "github.com/pkg/errors" "net" "net/url" "strconv" @@ -131,11 +132,11 @@ func (d *Discoverer) Discover(ctx context.Context, callback func(event apiModel. for _, discoveryItem := range discoveryItems { responseAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", discoveryItem.localAddress, model.AdsDiscoveryConstants_ADSDISCOVERYUDPDEFAULTPORT)) if err != nil { - panic(err) + return errors.Wrap(err, "error resolving udp") } socket, err := net.ListenUDP("udp4", responseAddr) if err != nil { - panic(err) + return errors.Wrap(err, "error listening udp") } discoveryItem.socket = socket diff --git a/plc4go/internal/ads/model/Tag.go b/plc4go/internal/ads/model/Tag.go index 6f77f701c0c..868466799e3 100644 --- a/plc4go/internal/ads/model/Tag.go +++ b/plc4go/internal/ads/model/Tag.go @@ -158,7 +158,7 @@ func (m DirectPlcTag) SerializeWithWriteBuffer(writeBuffer utils.WriteBuffer) er } func (m DirectPlcTag) MarshalXMLAttr(name xml.Name) (xml.Attr, error) { - panic(name) + return xml.Attr{}, errors.Errorf("%s", name) // TODO: why did this panic before } type SymbolicPlcTag struct { diff --git a/plc4go/internal/cbus/CBusMessageMapper.go b/plc4go/internal/cbus/CBusMessageMapper.go index a8e5ca53a1e..a51e604dbfe 100644 --- a/plc4go/internal/cbus/CBusMessageMapper.go +++ b/plc4go/internal/cbus/CBusMessageMapper.go @@ -96,7 +96,8 @@ func TagToCBusMessage(tag apiModel.PlcTag, value apiValues.PlcValue, alphaGenera var salData readWriteModel.SALData switch tagType.application.ApplicationId() { case readWriteModel.ApplicationId_FREE_USAGE: - panic("Not yet implemented") // TODO: implement + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_TEMPERATURE_BROADCAST: var temperatureBroadcastData readWriteModel.TemperatureBroadcastData switch salCommand { @@ -114,7 +115,8 @@ func TagToCBusMessage(tag apiModel.PlcTag, value apiValues.PlcValue, alphaGenera } salData = readWriteModel.NewSALDataTemperatureBroadcast(temperatureBroadcastData, nil) case readWriteModel.ApplicationId_ROOM_CONTROL_SYSTEM: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_LIGHTING, readWriteModel.ApplicationId_VENTILATION, @@ -166,35 +168,48 @@ func TagToCBusMessage(tag apiModel.PlcTag, value apiValues.PlcValue, alphaGenera lightingData = readWriteModel.NewLightingDataTerminateRamp(group, commandTypeContainer) supportsWrite = true case readWriteModel.LightingCommandType_LABEL.PLC4XEnumName(): - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return default: return nil, false, false, false, errors.Errorf("Unsupported command %s for %s", salCommand, tagType.application.ApplicationId()) } salData = readWriteModel.NewSALDataLighting(lightingData, nil) case readWriteModel.ApplicationId_AIR_CONDITIONING: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_TRIGGER_CONTROL: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_ENABLE_CONTROL: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_SECURITY: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_METERING: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_ACCESS_CONTROL: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_CLOCK_AND_TIMEKEEPING: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_TELEPHONY_STATUS_AND_CONTROL: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_MEASUREMENT: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_TESTING: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_MEDIA_TRANSPORT_CONTROL: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return case readWriteModel.ApplicationId_ERROR_REPORTING: - panic("Implement me") + err = errors.New("Not yet implemented") // TODO: implement + return default: return nil, false, false, false, errors.Errorf("No support for %s", tagType.application) } diff --git a/plc4go/internal/cbus/TagHandler.go b/plc4go/internal/cbus/TagHandler.go index 5da0695fbf7..4fb943ef1df 100644 --- a/plc4go/internal/cbus/TagHandler.go +++ b/plc4go/internal/cbus/TagHandler.go @@ -145,8 +145,8 @@ func (m TagHandler) handleStatusRequestPattern(match map[string]string) (apiMode } else if levelArgument := match["startingGroupAddressLabel"]; levelArgument != "" { statusRequestType = StatusRequestTypeLevel decodedHex, _ := hex.DecodeString(levelArgument) - if len(decodedHex) != 1 { - panic("invalid state. Should have exactly 1") + if hexLength := len(decodedHex); hexLength != 1 { + return nil, errors.Errorf("invalid state. Should have exactly 1. Actual length %d", hexLength) } startingGroupAddressLabel = &decodedHex[0] } else { @@ -177,7 +177,7 @@ func (m TagHandler) handleCalPattern(match map[string]string) (apiModel.PlcTag, calTypeArgument := match["calType"] switch { case strings.HasPrefix(calTypeArgument, "reset"): - panic("Not implemented") // TODO: implement me + return nil, errors.New("Not implemented") // TODO: implement me case strings.HasPrefix(calTypeArgument, "recall="): var recalParamNo readWriteModel.Parameter recallParamNoArgument := match["recallParamNo"] @@ -263,15 +263,15 @@ func (m TagHandler) handleCalPattern(match map[string]string) (apiModel.PlcTag, count = uint8(atoi) return NewCALGetStatusTag(unitAddress, bridgeAddresses, recalParamNo, count, 1), nil case strings.HasPrefix(calTypeArgument, "write="): - panic("Not implemented") // TODO: implement me + return nil, errors.New("Not implemented") // TODO: implement me case strings.HasPrefix(calTypeArgument, "identifyReply="): - panic("Not implemented") // TODO: implement me + return nil, errors.New("Not implemented") // TODO: implement me case strings.HasPrefix(calTypeArgument, "reply="): - panic("Not implemented") // TODO: implement me + return nil, errors.New("Not implemented") // TODO: implement me case strings.HasPrefix(calTypeArgument, "status="): - panic("Not implemented") // TODO: implement me + return nil, errors.New("Not implemented") // TODO: implement me case strings.HasPrefix(calTypeArgument, "statusExtended="): - panic("Not implemented") // TODO: implement me + return nil, errors.New("Not implemented") // TODO: implement me default: return nil, errors.Errorf("Invalid cal type %s", calTypeArgument) } diff --git a/plc4go/internal/cbus/ValueHandler.go b/plc4go/internal/cbus/ValueHandler.go index a58dd6201f0..1bdb4638b2d 100644 --- a/plc4go/internal/cbus/ValueHandler.go +++ b/plc4go/internal/cbus/ValueHandler.go @@ -45,7 +45,7 @@ func (m ValueHandler) NewPlcValue(tag apiModel.PlcTag, value any) (apiValues.Plc CAL_IDENTIFY_REPLY, CAL_STATUS, CAL_STATUS_EXTENDED: - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case SAL: var curValues []any if len(tag.GetArrayInfo()) > 0 && tag.GetArrayInfo()[0].GetSize() > 1 { @@ -143,7 +143,7 @@ func (m ValueHandler) NewPlcValue(tag apiModel.PlcTag, value any) (apiValues.Plc } return group, nil case readWriteModel.LightingCommandType_LABEL.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } @@ -156,13 +156,13 @@ func (m ValueHandler) NewPlcValue(tag apiModel.PlcTag, value any) (apiValues.Plc } return zoneGroup, nil case readWriteModel.AirConditioningCommandType_ZONE_HVAC_PLANT_STATUS.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_ZONE_HUMIDITY_PLANT_STATUS.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_ZONE_TEMPERATURE.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_ZONE_HUMIDITY.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_REFRESH.PLC4XEnumName(): zoneGroup, err := m.DefaultValueHandler.NewPlcValueFromType(apiValues.BYTE, curValues[0]) if err != nil { @@ -170,23 +170,23 @@ func (m ValueHandler) NewPlcValue(tag apiModel.PlcTag, value any) (apiValues.Plc } return zoneGroup, nil case readWriteModel.AirConditioningCommandType_SET_ZONE_HVAC_MODE.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_PLANT_HVAC_LEVEL.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_ZONE_HUMIDITY_MODE.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_PLANT_HUMIDITY_LEVEL.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_HVAC_UPPER_GUARD_LIMIT.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_HVAC_LOWER_GUARD_LIMIT.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_HVAC_SETBACK_LIMIT.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_HUMIDITY_UPPER_GUARD_LIMIT.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_HUMIDITY_LOWER_GUARD_LIMIT.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_SET_ZONE_GROUP_ON.PLC4XEnumName(): zoneGroup, err := m.DefaultValueHandler.NewPlcValueFromType(apiValues.BYTE, curValues[0]) if err != nil { @@ -194,95 +194,95 @@ func (m ValueHandler) NewPlcValue(tag apiModel.PlcTag, value any) (apiValues.Plc } return zoneGroup, nil case readWriteModel.AirConditioningCommandType_SET_HUMIDITY_SETBACK_LIMIT.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_HVAC_SCHEDULE_ENTRY.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AirConditioningCommandType_HUMIDITY_SCHEDULE_ENTRY.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_TRIGGER_CONTROL: switch salCommand { case readWriteModel.TriggerControlCommandType_TRIGGER_EVENT.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.TriggerControlCommandType_TRIGGER_MIN.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.TriggerControlCommandType_TRIGGER_MAX.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.TriggerControlCommandType_INDICATOR_KILL.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.TriggerControlCommandType_LABEL.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_ENABLE_CONTROL: switch salCommand { case readWriteModel.EnableControlCommandType_SET_NETWORK_VARIABLE.PLC4XEnumName(): - panic("Implement me") //TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_SECURITY: switch salCommand { case readWriteModel.SecurityCommandType_OFF.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.SecurityCommandType_ON.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.SecurityCommandType_EVENT.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_METERING: switch salCommand { case readWriteModel.MeteringCommandType_EVENT.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_ACCESS_CONTROL: switch salCommand { case readWriteModel.AccessControlCommandType_CLOSE_ACCESS_POINT.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AccessControlCommandType_LOCK_ACCESS_POINT.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AccessControlCommandType_ACCESS_POINT_LEFT_OPEN.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AccessControlCommandType_ACCESS_POINT_FORCED_OPEN.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AccessControlCommandType_ACCESS_POINT_CLOSED.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AccessControlCommandType_REQUEST_TO_EXIT.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AccessControlCommandType_VALID_ACCESS.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.AccessControlCommandType_INVALID_ACCESS.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_CLOCK_AND_TIMEKEEPING: switch salCommand { case readWriteModel.ClockAndTimekeepingCommandType_UPDATE_NETWORK_VARIABLE.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me case readWriteModel.ClockAndTimekeepingCommandType_REQUEST_REFRESH.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_TELEPHONY_STATUS_AND_CONTROL: switch salCommand { case readWriteModel.TelephonyCommandType_EVENT.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_MEASUREMENT: switch salCommand { case readWriteModel.MeasurementCommandType_MEASUREMENT_EVENT.PLC4XEnumName(): - panic("Implement me") // TODO: implement + return nil, errors.New("Implement me") //TODO: implement me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } @@ -295,62 +295,62 @@ func (m ValueHandler) NewPlcValue(tag apiModel.PlcTag, value any) (apiValues.Plc case readWriteModel.ApplicationId_MEDIA_TRANSPORT_CONTROL: switch salCommand { case readWriteModel.MediaTransportControlCommandType_STOP.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_PLAY.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_PAUSE_RESUME.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_SELECT_CATEGORY.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_SELECT_SELECTION.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_SELECT_TRACK.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_SHUFFLE_ON_OFF.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_REPEAT_ON_OFF.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_NEXT_PREVIOUS_CATEGORY.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_NEXT_PREVIOUS_SELECTION.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_NEXT_PREVIOUS_TRACK.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_FAST_FORWARD.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_REWIND.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_SOURCE_POWER_CONTROL.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_TOTAL_TRACKS.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_STATUS_REQUEST.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_ENUMERATE_CATEGORIES_SELECTIONS_TRACKS.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_ENUMERATION_SIZE.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_TRACK_NAME.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_SELECTION_NAME.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_CATEGORY_NAME.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.MediaTransportControlCommandType_FAST_FORWARD.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } case readWriteModel.ApplicationId_ERROR_REPORTING: switch salCommand { case readWriteModel.ErrorReportingCommandType_DEPRECATED.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.ErrorReportingCommandType_ERROR_REPORT.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.ErrorReportingCommandType_ACKNOWLEDGE.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me case readWriteModel.ErrorReportingCommandType_CLEAR_MOST_SEVERE.PLC4XEnumName(): - panic("Implement me") // TODO: implement me + return nil, errors.New("Implement me") //TODO: implement me me default: return nil, errors.Errorf("Unsupported command %s for %s", salCommand, tmpSalTag.application.ApplicationId()) } diff --git a/plc4go/internal/eip/Reader.go b/plc4go/internal/eip/Reader.go index 65260715427..3348aecde04 100644 --- a/plc4go/internal/eip/Reader.go +++ b/plc4go/internal/eip/Reader.go @@ -281,7 +281,7 @@ func parsePlcValue(tag EIPPlcTag, data utils.ReadBufferByteBased, _type readWrit list = append(list, spiValues.NewPlcSINT(readInt8)) case readWriteModel.CIPDataTypeCode_REAL: if _type.Size()*8 != 64 { - panic("Unexpected size") + return nil, errors.New("Unexpected size") } readFloat64, err := data.ReadFloat64("", 64) if err != nil { @@ -321,7 +321,7 @@ func parsePlcValue(tag EIPPlcTag, data utils.ReadBufferByteBased, _type readWrit return spiValues.NewPlcDINT(readInt32), nil case readWriteModel.CIPDataTypeCode_REAL: if _type.Size()*8 != 64 { - panic("Unexpected size") + return nil, errors.New("Unexpected size") } readFloat32, err := data.ReadFloat32("", 64) if err != nil { diff --git a/plc4go/internal/simulated/Connection.go b/plc4go/internal/simulated/Connection.go index e6ef48d4921..a56287d1400 100644 --- a/plc4go/internal/simulated/Connection.go +++ b/plc4go/internal/simulated/Connection.go @@ -241,9 +241,9 @@ func (c *Connection) SubscriptionRequestBuilder() model.PlcSubscriptionRequestBu } func (c *Connection) UnsubscriptionRequestBuilder() model.PlcUnsubscriptionRequestBuilder { - panic("not implemented") + panic("not provided by simulated connection") } func (c *Connection) BrowseRequestBuilder() model.PlcBrowseRequestBuilder { - panic("not implemented") + panic("not provided by simulated connection") } diff --git a/plc4go/pkg/api/cache/plcConnectionLease_test.go b/plc4go/pkg/api/cache/plcConnectionLease_test.go index a90141d2247..5ed63ab1806 100644 --- a/plc4go/pkg/api/cache/plcConnectionLease_test.go +++ b/plc4go/pkg/api/cache/plcConnectionLease_test.go @@ -565,7 +565,7 @@ func TestLeasedPlcConnection_UnsubscriptionRequestBuilder(t *testing.T) { func() { defer func() { if r := recover(); r != nil { - assert.Equal(t, r, "not implemented") + assert.Equal(t, r, "not provided by simulated connection") } else { t.Errorf("The code did not panic") } @@ -615,7 +615,7 @@ func TestLeasedPlcConnection_BrowseRequestBuilder(t *testing.T) { func() { defer func() { if r := recover(); r != nil { - assert.Equal(t, r, "not implemented") + assert.Equal(t, r, "not provided by simulated connection") } else { t.Errorf("The code did not panic") } diff --git a/plc4go/pkg/api/driverManager.go b/plc4go/pkg/api/driverManager.go index 2e182dda8d2..411f4979e9c 100644 --- a/plc4go/pkg/api/driverManager.go +++ b/plc4go/pkg/api/driverManager.go @@ -137,9 +137,6 @@ func convertToInternalOptions(withDiscoveryOptions ...WithDiscoveryOption) []opt /////////////////////////////////////// func (m *plcDriverManger) RegisterDriver(driver PlcDriver) { - if driver == nil { - panic("driver must not be nil") - } log.Debug().Str("protocolName", driver.GetProtocolName()).Msg("Registering driver") // If this driver is already registered, just skip resetting it for driverName := range m.drivers { @@ -170,9 +167,6 @@ func (m *plcDriverManger) GetDriver(driverName string) (PlcDriver, error) { } func (m *plcDriverManger) RegisterTransport(transport transports.Transport) { - if transport == nil { - panic("transport must not be nil") - } log.Debug().Str("transportName", transport.GetTransportName()).Msg("Registering transport") // If this transport is already registered, just skip resetting it for transportName := range m.transports { diff --git a/plc4go/spi/default/DefaultConnection.go b/plc4go/spi/default/DefaultConnection.go index 798548bf8aa..7ce2a07cdfa 100644 --- a/plc4go/spi/default/DefaultConnection.go +++ b/plc4go/spi/default/DefaultConnection.go @@ -307,23 +307,23 @@ func (d *defaultConnection) GetMetadata() model.PlcConnectionMetadata { } func (d *defaultConnection) ReadRequestBuilder() model.PlcReadRequestBuilder { - panic("not implemented") + panic("not provided by actual connection") } func (d *defaultConnection) WriteRequestBuilder() model.PlcWriteRequestBuilder { - panic("not implemented") + panic("not provided by actual connection") } func (d *defaultConnection) SubscriptionRequestBuilder() model.PlcSubscriptionRequestBuilder { - panic("not implemented") + panic("not provided by actual connection") } func (d *defaultConnection) UnsubscriptionRequestBuilder() model.PlcUnsubscriptionRequestBuilder { - panic("not implemented") + panic("not provided by actual connection") } func (d *defaultConnection) BrowseRequestBuilder() model.PlcBrowseRequestBuilder { - panic("not implemented") + panic("not provided by actual connection") } func (d *defaultConnection) GetTransportInstance() transports.TransportInstance { diff --git a/plc4go/spi/default/DefaultConnection_test.go b/plc4go/spi/default/DefaultConnection_test.go index d07b02c5dbb..f6d46ab64b4 100644 --- a/plc4go/spi/default/DefaultConnection_test.go +++ b/plc4go/spi/default/DefaultConnection_test.go @@ -477,7 +477,7 @@ func Test_defaultConnection_BrowseRequestBuilder(t *testing.T) { t.Run(tt.name, func(t *testing.T) { defer func() { if err := recover(); err != nil { - assert.Equal(t, "not implemented", err) + assert.Equal(t, "not provided by actual connection", err) } else { t.Error("should fail") } @@ -903,7 +903,7 @@ func Test_defaultConnection_ReadRequestBuilder(t *testing.T) { t.Run(tt.name, func(t *testing.T) { defer func() { if err := recover(); err != nil { - assert.Equal(t, "not implemented", err) + assert.Equal(t, "not provided by actual connection", err) } else { t.Error("should fail") } @@ -975,7 +975,7 @@ func Test_defaultConnection_SubscriptionRequestBuilder(t *testing.T) { t.Run(tt.name, func(t *testing.T) { defer func() { if err := recover(); err != nil { - assert.Equal(t, "not implemented", err) + assert.Equal(t, "not provided by actual connection", err) } else { t.Error("should fail") } @@ -1013,7 +1013,7 @@ func Test_defaultConnection_UnsubscriptionRequestBuilder(t *testing.T) { t.Run(tt.name, func(t *testing.T) { defer func() { if err := recover(); err != nil { - assert.Equal(t, "not implemented", err) + assert.Equal(t, "not provided by actual connection", err) } else { t.Error("should fail") } @@ -1051,7 +1051,7 @@ func Test_defaultConnection_WriteRequestBuilder(t *testing.T) { t.Run(tt.name, func(t *testing.T) { defer func() { if err := recover(); err != nil { - assert.Equal(t, "not implemented", err) + assert.Equal(t, "not provided by actual connection", err) } else { t.Error("should fail") } diff --git a/plc4go/spi/testutils/DriverTestRunner.go b/plc4go/spi/testutils/DriverTestRunner.go index c3a971acc5d..58c2d9feea1 100644 --- a/plc4go/spi/testutils/DriverTestRunner.go +++ b/plc4go/spi/testutils/DriverTestRunner.go @@ -323,7 +323,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti } actualRawOutput := testTransportInstance.DrainWriteBuffer(expectedRawOutputLength) if testTransportInstance.GetNumDrainableBytes() != 0 { - //panic(fmt.Sprintf("leftover drainable bytes (%d)", testTransportInstance.GetNumDrainableBytes())) + t.Logf("leftover drainable bytes (%d)", testTransportInstance.GetNumDrainableBytes()) } var bufferFactory func([]byte, ...utils.ReadBufferByteBasedOptions) utils.ReadBufferByteBased diff --git a/plc4go/spi/testutils/ManualTestRunner.go b/plc4go/spi/testutils/ManualTestRunner.go index 2ec36ccfde0..d0d5bb0dc4a 100644 --- a/plc4go/spi/testutils/ManualTestRunner.go +++ b/plc4go/spi/testutils/ManualTestRunner.go @@ -112,14 +112,14 @@ func (m *ManualTestSuite) runSingleTest(t *testing.T, connection plc4go.PlcConne readRequestBuilder.AddTagAddress(tagName, testCase.Address) readRequest, err := readRequestBuilder.Build() if err != nil { - panic(err) + t.Fatal(err) + return } // Execute the read request readResponseResult := <-readRequest.Execute() if readResponseResult.GetErr() != nil { - t.Errorf("Error getting response %v", readResponseResult.GetErr()) - t.FailNow() + t.Fatalf("Error getting response %v", readResponseResult.GetErr()) return } readResponse := readResponseResult.GetResponse() diff --git a/plc4go/spi/utils/ReadBufferByteBased.go b/plc4go/spi/utils/ReadBufferByteBased.go index 3ffa13c66a9..d9957e538f9 100644 --- a/plc4go/spi/utils/ReadBufferByteBased.go +++ b/plc4go/spi/utils/ReadBufferByteBased.go @@ -96,7 +96,7 @@ func (rb *byteReadBuffer) Reset(pos uint16) { bytesToSkip := make([]byte, pos) _, err := rb.reader.Read(bytesToSkip) if err != nil { - panic(err) + panic(errors.Wrap(err, "Should not happen")) // TODO: maybe this is a possible occurence since we accept a argument, better returns a error } rb.pos = uint64(pos * 8) } diff --git a/plc4go/spi/utils/WriteBufferBoxBased.go b/plc4go/spi/utils/WriteBufferBoxBased.go index b076cb51ff0..f706a90f76f 100644 --- a/plc4go/spi/utils/WriteBufferBoxBased.go +++ b/plc4go/spi/utils/WriteBufferBoxBased.go @@ -23,6 +23,7 @@ import ( "container/list" "context" "fmt" + "github.com/pkg/errors" "math/big" ) @@ -264,7 +265,7 @@ findTheBox: finalBoxes = append(asciiBoxes, finalBoxes...) break findTheBox default: - panic("We should never reach this point") + return errors.New("We should never reach this point") } } if b.mergeSingleBoxes && len(finalBoxes) == 1 { diff --git a/plc4go/spi/utils/WriteBufferJsonBased.go b/plc4go/spi/utils/WriteBufferJsonBased.go index 98000381f29..c6eca9b29d0 100644 --- a/plc4go/spi/utils/WriteBufferJsonBased.go +++ b/plc4go/spi/utils/WriteBufferJsonBased.go @@ -198,17 +198,15 @@ func (j *jsonWriteBuffer) PopContext(logicalName string, _ ...WithWriterArgs) er pop := j.Pop() var poppedName string var unwrapped any - switch pop.(type) { + switch _context := pop.(type) { case *elementContext: - context := pop.(*elementContext) - poppedName = context.logicalName - unwrapped = context.properties + poppedName = _context.logicalName + unwrapped = _context.properties case *listContext: - context := pop.(*listContext) - poppedName = context.logicalName - unwrapped = context.list + poppedName = _context.logicalName + unwrapped = _context.list default: - panic("broken context") + return errors.New("broken context") } if poppedName != logicalName { return errors.Errorf("unexpected closing context %s, expected %s", poppedName, logicalName) @@ -220,17 +218,15 @@ func (j *jsonWriteBuffer) PopContext(logicalName string, _ ...WithWriterArgs) er return nil } j.rootNode = j.Peek() - switch j.rootNode.(type) { + switch _context := j.rootNode.(type) { case *elementContext: - context := j.rootNode.(*elementContext) - context.properties[logicalName] = unwrapped + _context.properties[logicalName] = unwrapped case *listContext: - context := j.rootNode.(*listContext) wrappedWrap := make(map[string]any) wrappedWrap[logicalName] = unwrapped - context.list = append(context.list, wrappedWrap) + _context.list = append(_context.list, wrappedWrap) default: - panic("broken context") + return errors.New("broken context") } return nil } @@ -249,30 +245,28 @@ func (j *jsonWriteBuffer) GetJsonString() (string, error) { func (j *jsonWriteBuffer) encodeNode(logicalName string, value any, attr map[string]any, _ ...WithWriterArgs) error { logicalName = j.SanitizeLogicalName(logicalName) peek := j.Peek() - switch peek.(type) { + switch _context := peek.(type) { case *elementContext: - context := peek.(*elementContext) - context.properties[logicalName] = value + _context.properties[logicalName] = value for key, attrValue := range attr { - context.properties[key] = attrValue + _context.properties[key] = attrValue } return nil case *listContext: - context := peek.(*listContext) m := make(map[string]any) m[logicalName] = value for attrKey, attrValue := range attr { m[attrKey] = attrValue } - context.list = append(context.list, m) + _context.list = append(_context.list, m) return nil default: - context := &elementContext{logicalName, make(map[string]any)} - context.properties[logicalName] = value + newContext := &elementContext{logicalName, make(map[string]any)} + newContext.properties[logicalName] = value for key, attrValue := range attr { - context.properties[key] = attrValue + newContext.properties[key] = attrValue } - j.Push(context) + j.Push(newContext) return nil } } diff --git a/plc4go/spi/values/WriteBufferPlcValueBased.go b/plc4go/spi/values/WriteBufferPlcValueBased.go index d969023ec16..8189e00b7fa 100644 --- a/plc4go/spi/values/WriteBufferPlcValueBased.go +++ b/plc4go/spi/values/WriteBufferPlcValueBased.go @@ -182,17 +182,15 @@ func (p *writeBufferPlcValueBased) PopContext(logicalName string, _ ...utils.Wit pop := p.Pop() var poppedName string var unwrapped apiValues.PlcValue - switch pop.(type) { + switch _context := pop.(type) { case *plcValueContext: - context := pop.(*plcValueContext) - poppedName = context.logicalName - unwrapped = NewPlcStruct(context.properties) + poppedName = _context.logicalName + unwrapped = NewPlcStruct(_context.properties) case *plcListContext: - context := pop.(*plcListContext) - poppedName = context.logicalName - unwrapped = NewPlcList(context.list) + poppedName = _context.logicalName + unwrapped = NewPlcList(_context.list) default: - panic("broken context") + return errors.New("broken context") } if poppedName != logicalName { return errors.Errorf("unexpected closing context %s, expected %s", poppedName, logicalName) @@ -201,13 +199,13 @@ func (p *writeBufferPlcValueBased) PopContext(logicalName string, _ ...utils.Wit p.rootNode = NewPlcStruct(map[string]apiValues.PlcValue{logicalName: unwrapped}) return nil } - switch context := p.Peek().(type) { + switch _context := p.Peek().(type) { case *plcValueContext: - context.properties[logicalName] = unwrapped + _context.properties[logicalName] = unwrapped case *plcListContext: - context.list = append(context.list, NewPlcStruct(map[string]apiValues.PlcValue{logicalName: unwrapped})) + _context.list = append(_context.list, NewPlcStruct(map[string]apiValues.PlcValue{logicalName: unwrapped})) default: - panic("broken context") + return errors.New("broken context") } return nil } @@ -219,19 +217,17 @@ func (p *writeBufferPlcValueBased) GetPlcValue() apiValues.PlcValue { func (p *writeBufferPlcValueBased) appendValue(logicalName string, value apiValues.PlcValue) error { logicalName = p.SanitizeLogicalName(logicalName) peek := p.Peek() - switch peek.(type) { + switch _context := peek.(type) { case *plcValueContext: - context := peek.(*plcValueContext) - context.properties[logicalName] = value + _context.properties[logicalName] = value return nil case *plcListContext: - context := peek.(*plcListContext) - context.list = append(context.list, value) + _context.list = append(_context.list, value) return nil default: - context := &plcValueContext{logicalName, make(map[string]apiValues.PlcValue)} - context.properties[logicalName] = value - p.Push(context) + newContext := &plcValueContext{logicalName, make(map[string]apiValues.PlcValue)} + newContext.properties[logicalName] = value + p.Push(newContext) return nil } }