diff --git a/client.go b/client.go index 0ebbaedf..10bf82fb 100644 --- a/client.go +++ b/client.go @@ -85,9 +85,9 @@ func GetEndpoints(ctx context.Context, endpoint string, opts ...Option) ([]*ua.E // security policy and security mode. policy and mode can be omitted so that // only one of them has to match. // todo(fs): should this function return an error? -func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua.MessageSecurityMode) *ua.EndpointDescription { +func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua.MessageSecurityMode) (*ua.EndpointDescription, error) { if len(endpoints) == 0 { - return nil + return nil, errors.Errorf("no endpoints available") } sort.Sort(sort.Reverse(bySecurityLevel(endpoints))) @@ -95,26 +95,26 @@ func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua. // don't care -> return highest security level if policy == "" && mode == ua.MessageSecurityModeInvalid { - return endpoints[0] + return endpoints[0], nil } for _, p := range endpoints { // match only security mode if policy == "" && p.SecurityMode == mode { - return p + return p, nil } // match only security policy if p.SecurityPolicyURI == policy && mode == ua.MessageSecurityModeInvalid { - return p + return p, nil } // match both if p.SecurityPolicyURI == policy && p.SecurityMode == mode { - return p + return p, nil } } - return nil + return nil, errors.Errorf("no matching endpoint found for policy %s and mode %s", policy, mode) } type bySecurityLevel []*ua.EndpointDescription diff --git a/examples/datetime/datetime.go b/examples/datetime/datetime.go index dca80633..02bcb7aa 100644 --- a/examples/datetime/datetime.go +++ b/examples/datetime/datetime.go @@ -31,9 +31,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } fmt.Println("*", ep.SecurityPolicyURI, ep.SecurityMode) diff --git a/examples/monitor/monitor.go b/examples/monitor/monitor.go index ef0f39ca..f9c06df4 100644 --- a/examples/monitor/monitor.go +++ b/examples/monitor/monitor.go @@ -47,9 +47,9 @@ func main() { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } log.Print("*", ep.SecurityPolicyURI, ep.SecurityMode) diff --git a/examples/subscribe/subscribe.go b/examples/subscribe/subscribe.go index 8127cd9a..596f4b89 100644 --- a/examples/subscribe/subscribe.go +++ b/examples/subscribe/subscribe.go @@ -43,9 +43,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } ep.EndpointURL = *endpoint diff --git a/examples/trigger/trigger.go b/examples/trigger/trigger.go index beafb3b3..566bb5cf 100644 --- a/examples/trigger/trigger.go +++ b/examples/trigger/trigger.go @@ -43,9 +43,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } fmt.Println("*", ep.SecurityPolicyURI, ep.SecurityMode) diff --git a/examples/udt/udt.go b/examples/udt/udt.go index 6fa3ad5a..e5ed1987 100644 --- a/examples/udt/udt.go +++ b/examples/udt/udt.go @@ -39,9 +39,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } fmt.Println("*", ep.SecurityPolicyURI, ep.SecurityMode)