Remove all panicking from methods

This commit is contained in:
jkirkwood 2019-06-19 17:47:59 +00:00
parent 2a9ef418e6
commit 744dfb200e
5 changed files with 63 additions and 51 deletions

View file

@ -43,8 +43,8 @@ type Connection interface {
// GetSettings gets the settings maps describing this network configuration.
// This will never include any secrets required for connection to the
// network, as those are often protected. Secrets must be requested
// separately using the GetSecrets() callWithReturnAndPanic.
GetSettings() ConnectionSettings
// separately using the GetSecrets() method.
GetSettings() (ConnectionSettings, error)
// Clear the secrets belonging to this network connection profile.
ClearSecrets() error
@ -89,9 +89,13 @@ func (c *connection) Delete() error {
return c.call(ConnectionDelete)
}
func (c *connection) GetSettings() ConnectionSettings {
func (c *connection) GetSettings() (ConnectionSettings, error) {
var settings map[string]map[string]dbus.Variant
c.callWithReturnAndPanic(&settings, ConnectionGetSettings)
err := c.callWithReturn(&settings, ConnectionGetSettings)
if err != nil {
return nil, err
}
rv := make(ConnectionSettings)
@ -103,7 +107,7 @@ func (c *connection) GetSettings() ConnectionSettings {
}
}
return rv
return rv, nil
}
func (c *connection) ClearSecrets() error {
@ -127,5 +131,6 @@ func (c *connection) GetPropertyFilename() (string, error) {
}
func (c *connection) MarshalJSON() ([]byte, error) {
return json.Marshal(c.GetSettings())
s, _ := c.GetSettings()
return json.Marshal(s)
}

View file

@ -21,7 +21,7 @@ type DeviceWireless interface {
// Note that this list does not include access points which hide their SSID.
// To retrieve a list of all access points (including hidden ones) use the
// GetAllAccessPoints() method.
GetAccessPoints() []AccessPoint
GetAccessPoints() ([]AccessPoint, error)
RequestScan()
}
@ -35,21 +35,24 @@ type deviceWireless struct {
device
}
func (d *deviceWireless) GetAccessPoints() []AccessPoint {
func (d *deviceWireless) GetAccessPoints() ([]AccessPoint, error) {
var apPaths []dbus.ObjectPath
err := d.callWithReturn(&apPaths, DeviceWirelessGetAccessPoints)
if err != nil {
return nil, err
}
d.callWithReturnAndPanic(&apPaths, DeviceWirelessGetAccessPoints)
aps := make([]AccessPoint, len(apPaths))
var err error
for i, path := range apPaths {
aps[i], err = NewAccessPoint(path)
if err != nil {
panic(err)
return aps, err
}
}
return aps
return aps, nil
}
func (d *deviceWireless) RequestScan() {
@ -59,6 +62,6 @@ func (d *deviceWireless) RequestScan() {
func (d *deviceWireless) MarshalJSON() ([]byte, error) {
m := d.device.marshalMap()
m["AccessPoints"] = d.GetAccessPoints()
m["AccessPoints"], _ = d.GetAccessPoints()
return json.Marshal(m)
}

View file

@ -229,14 +229,17 @@ func (nm *networkManager) Reload(flags uint32) error {
func (nm *networkManager) GetDevices() (devices []Device, err error) {
var devicePaths []dbus.ObjectPath
err = nm.callWithReturn(&devicePaths, NetworkManagerGetDevices)
if err != nil {
return
}
nm.callWithReturnAndPanic(&devicePaths, NetworkManagerGetDevices)
devices = make([]Device, len(devicePaths))
for i, path := range devicePaths {
devices[i], err = DeviceFactory(path)
if err != nil {
panic(err)
return
}
}
@ -246,13 +249,17 @@ func (nm *networkManager) GetDevices() (devices []Device, err error) {
func (nm *networkManager) GetAllDevices() (devices []Device, err error) {
var devicePaths []dbus.ObjectPath
nm.callWithReturnAndPanic(&devicePaths, NetworkManagerGetAllDevices)
err = nm.callWithReturn(&devicePaths, NetworkManagerGetAllDevices)
if err != nil {
return
}
devices = make([]Device, len(devicePaths))
for i, path := range devicePaths {
devices[i], err = DeviceFactory(path)
if err != nil {
panic(err)
return
}
}
@ -277,11 +284,14 @@ func (nm *networkManager) GetDeviceByIpIface(interfaceId string) (device Device,
func (nm *networkManager) ActivateConnection(c Connection, d Device) (ac ActiveConnection, err error) {
var opath dbus.ObjectPath
nm.callWithReturnAndPanic(&opath, NetworkManagerActivateConnection, c.GetPath(), d.GetPath(), dbus.ObjectPath("/"))
err = nm.callWithReturn(&opath, NetworkManagerActivateConnection, c.GetPath(), d.GetPath(), dbus.ObjectPath("/"))
if err != nil {
return
}
ac, err = NewActiveConnection(opath)
if err != nil {
panic(err)
return
}
return

View file

@ -25,16 +25,16 @@ const (
type Settings interface {
// ListConnections gets list the saved network connections known to NetworkManager
ListConnections() []Connection
ListConnections() ([]Connection, error)
// AddConnection callWithReturnAndPanic new connection and save it to disk.
AddConnection(settings ConnectionSettings) Connection
// AddConnection adds new connection and save it to disk.
AddConnection(settings ConnectionSettings) (Connection, error)
// Add new connection but do not save it to disk immediately. This operation does not start the network connection unless (1) device is idle and able to connect to the network described by the new connection, and (2) the connection is allowed to be started automatically. Use the 'Save' method on the connection to save these changes to disk. Note that unsaved changes will be lost if the connection is reloaded from disk (either automatically on file change or due to an explicit ReloadConnections call).
AddConnectionUnsaved(settings ConnectionSettings) Connection
AddConnectionUnsaved(settings ConnectionSettings) (Connection, error)
// Save the hostname to persistent configuration.
SaveHostname(hostname string)
SaveHostname(hostname string) error
// If true, adding and modifying connections is supported.
GetPropertyCanModify() (bool, error)
@ -52,48 +52,49 @@ type settings struct {
dbusBase
}
func (s *settings) ListConnections() []Connection {
func (s *settings) ListConnections() ([]Connection, error) {
var connectionPaths []dbus.ObjectPath
s.callWithReturnAndPanic(&connectionPaths, SettingsListConnections)
err := s.callWithReturn(&connectionPaths, SettingsListConnections)
if err != nil {
return nil, err
}
connections := make([]Connection, len(connectionPaths))
var err error
for i, path := range connectionPaths {
connections[i], err = NewConnection(path)
if err != nil {
panic(err)
return connections, err
}
}
return connections
return connections, nil
}
func (s *settings) AddConnection(settings ConnectionSettings) Connection {
func (s *settings) AddConnection(settings ConnectionSettings) (Connection, error) {
var path dbus.ObjectPath
s.callWithReturnAndPanic(&path, SettingsAddConnection, settings)
con, err := NewConnection(path)
err := s.callWithReturn(&path, SettingsAddConnection, settings)
if err != nil {
panic(err)
return nil, err
}
return con
return NewConnection(path)
}
func (s *settings) AddConnectionUnsaved(settings ConnectionSettings) Connection {
func (s *settings) AddConnectionUnsaved(settings ConnectionSettings) (Connection, error) {
var path dbus.ObjectPath
s.callWithReturnAndPanic(&path, SettingsAddConnectionUnsaved, settings)
con, err := NewConnection(path)
err := s.callWithReturn(&path, SettingsAddConnectionUnsaved, settings)
if err != nil {
panic(err)
return nil, err
}
return con
return NewConnection(path)
}
func (s *settings) SaveHostname(hostname string) {
err := s.call(SettingsSaveHostname, hostname)
if err != nil {
panic(err)
}
func (s *settings) SaveHostname(hostname string) error {
return s.call(SettingsSaveHostname, hostname)
}
func (s *settings) GetPropertyHostname() (string, error) {

View file

@ -34,13 +34,6 @@ func (d *dbusBase) call(method string, args ...interface{}) error {
return d.obj.Call(method, 0, args...).Err
}
func (d *dbusBase) callWithReturnAndPanic(ret interface{}, method string, args ...interface{}) {
err := d.callWithReturn(ret, method, args...)
if err != nil {
panic(err)
}
}
func (d *dbusBase) callWithReturn(ret interface{}, method string, args ...interface{}) error {
return d.obj.Call(method, 0, args...).Store(ret)
}