Skip to content
This repository was archived by the owner on Sep 26, 2019. It is now read-only.

Commit 12d19b5

Browse files
committed
Don't Panic: Forward all errors as error values. Read this for reference: https://dave.cheney.net/2012/01/18/why-go-gets-exceptions-right
1 parent 54e6cf9 commit 12d19b5

9 files changed

+424
-246
lines changed

Diff for: AccessPoint.go

+74-29
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,36 @@ type AccessPoint interface {
2424
GetPath() dbus.ObjectPath
2525

2626
// GetFlags gets flags describing the capabilities of the access point.
27-
GetFlags() uint32
27+
GetFlags() (uint32, error)
2828

2929
// GetWPAFlags gets flags describing the access point's capabilities
3030
// according to WPA (Wifi Protected Access).
31-
GetWPAFlags() uint32
31+
GetWPAFlags() (uint32, error)
3232

3333
// GetRSNFlags gets flags describing the access point's capabilities
3434
// according to the RSN (Robust Secure Network) protocol.
35-
GetRSNFlags() uint32
35+
GetRSNFlags() (uint32, error)
3636

3737
// GetSSID returns the Service Set Identifier identifying the access point.
38-
GetSSID() string
38+
GetSSID() (string, error)
3939

4040
// GetFrequency gets the radio channel frequency in use by the access point,
4141
// in MHz.
42-
GetFrequency() uint32
42+
GetFrequency() (uint32, error)
4343

4444
// GetHWAddress gets the hardware address (BSSID) of the access point.
45-
GetHWAddress() string
45+
GetHWAddress() (string, error)
4646

4747
// GetMode describes the operating mode of the access point.
48-
GetMode() Nm80211Mode
48+
GetMode() (Nm80211Mode, error)
4949

5050
// GetMaxBitrate gets the maximum bitrate this access point is capable of, in
5151
// kilobits/second (Kb/s).
52-
GetMaxBitrate() uint32
52+
GetMaxBitrate() (uint32, error)
5353

5454
// GetStrength gets the current signal quality of the access point, in
5555
// percent.
56-
GetStrength() uint8
56+
GetStrength() (uint8, error)
5757

5858
MarshalJSON() ([]byte, error)
5959
}
@@ -71,52 +71,97 @@ func (a *accessPoint) GetPath() dbus.ObjectPath {
7171
return a.obj.Path()
7272
}
7373

74-
func (a *accessPoint) GetFlags() uint32 {
74+
func (a *accessPoint) GetFlags() (uint32, error) {
7575
return a.getUint32Property(AccessPointPropertyFlags)
7676
}
7777

78-
func (a *accessPoint) GetWPAFlags() uint32 {
78+
func (a *accessPoint) GetWPAFlags() (uint32, error) {
7979
return a.getUint32Property(AccessPointPropertyWPAFlags)
8080
}
8181

82-
func (a *accessPoint) GetRSNFlags() uint32 {
82+
func (a *accessPoint) GetRSNFlags() (uint32, error) {
8383
return a.getUint32Property(AccessPointPropertyRSNFlags)
8484
}
8585

86-
func (a *accessPoint) GetSSID() string {
87-
return string(a.getSliceByteProperty(AccessPointPropertySSID))
86+
func (a *accessPoint) GetSSID() (string, error) {
87+
r, err := a.getSliceByteProperty(AccessPointPropertySSID)
88+
if err != nil {
89+
return "", err
90+
}
91+
return string(r), nil
8892
}
8993

90-
func (a *accessPoint) GetFrequency() uint32 {
94+
func (a *accessPoint) GetFrequency() (uint32, error) {
9195
return a.getUint32Property(AccessPointPropertyFrequency)
9296
}
9397

94-
func (a *accessPoint) GetHWAddress() string {
98+
func (a *accessPoint) GetHWAddress() (string, error) {
9599
return a.getStringProperty(AccessPointPropertyHWAddress)
96100
}
97101

98-
func (a *accessPoint) GetMode() Nm80211Mode {
99-
return Nm80211Mode(a.getUint32Property(AccessPointPropertyMode))
102+
func (a *accessPoint) GetMode() (Nm80211Mode, error) {
103+
r, err := a.getUint32Property(AccessPointPropertyMode)
104+
if err != nil {
105+
return Nm80211ModeUnknown, err
106+
}
107+
return Nm80211Mode(r), nil
100108
}
101109

102-
func (a *accessPoint) GetMaxBitrate() uint32 {
110+
func (a *accessPoint) GetMaxBitrate() (uint32, error) {
103111
return a.getUint32Property(AccessPointPropertyMaxBitrate)
104112
}
105113

106-
func (a *accessPoint) GetStrength() uint8 {
114+
func (a *accessPoint) GetStrength() (uint8, error) {
107115
return a.getUint8Property(AccessPointPropertyStrength)
108116
}
109117

110118
func (a *accessPoint) MarshalJSON() ([]byte, error) {
119+
Flags, err := a.GetFlags()
120+
if err != nil {
121+
return nil, err
122+
}
123+
WPAFlags, err := a.GetWPAFlags()
124+
if err != nil {
125+
return nil, err
126+
}
127+
RSNFlags, err := a.GetRSNFlags()
128+
if err != nil {
129+
return nil, err
130+
}
131+
SSID, err := a.GetSSID()
132+
if err != nil {
133+
return nil, err
134+
}
135+
Frequency, err := a.GetFrequency()
136+
if err != nil {
137+
return nil, err
138+
}
139+
HWAddress, err := a.GetHWAddress()
140+
if err != nil {
141+
return nil, err
142+
}
143+
Mode, err := a.GetMode()
144+
if err != nil {
145+
return nil, err
146+
}
147+
MaxBitrate, err := a.GetMaxBitrate()
148+
if err != nil {
149+
return nil, err
150+
}
151+
Strength, err := a.GetStrength()
152+
if err != nil {
153+
return nil, err
154+
}
155+
111156
return json.Marshal(map[string]interface{}{
112-
"Flags": a.GetFlags(),
113-
"WPAFlags": a.GetWPAFlags(),
114-
"RSNFlags": a.GetRSNFlags(),
115-
"SSID": a.GetSSID(),
116-
"Frequency": a.GetFrequency(),
117-
"HWAddress": a.GetHWAddress(),
118-
"Mode": a.GetMode().String(),
119-
"MaxBitrate": a.GetMaxBitrate(),
120-
"Strength": a.GetStrength(),
157+
"Flags": Flags,
158+
"WPAFlags": WPAFlags,
159+
"RSNFlags": RSNFlags,
160+
"SSID": SSID,
161+
"Frequency": Frequency,
162+
"HWAddress": HWAddress,
163+
"Mode": Mode.String(),
164+
"MaxBitrate": MaxBitrate,
165+
"Strength": Strength,
121166
})
122167
}

Diff for: ActiveConnection.go

+72-49
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,43 @@ const (
2424

2525
type ActiveConnection interface {
2626
// GetConnection gets connection object of the connection.
27-
GetConnection() Connection
27+
GetConnection() (Connection, error)
2828

2929
// GetSpecificObject gets a specific object associated with the active connection.
30-
GetSpecificObject() AccessPoint
30+
GetSpecificObject() (AccessPoint, error)
3131

3232
// GetID gets the ID of the connection.
33-
GetID() string
33+
GetID() (string, error)
3434

3535
// GetUUID gets the UUID of the connection.
36-
GetUUID() string
36+
GetUUID() (string, error)
3737

3838
// GetType gets the type of the connection.
39-
GetType() string
39+
GetType() (string, error)
4040

4141
// GetDevices gets array of device objects which are part of this active connection.
42-
GetDevices() []Device
42+
GetDevices() ([]Device, error)
4343

4444
// GetState gets the state of the connection.
45-
GetState() uint32
45+
GetState() (uint32, error)
4646

4747
// GetStateFlags gets the state flags of the connection.
48-
GetStateFlags() uint32
48+
GetStateFlags() (uint32, error)
4949

5050
// GetDefault gets the default IPv4 flag of the connection.
51-
GetDefault() bool
51+
GetDefault() (bool, error)
5252

5353
// GetIP4Config gets the IP4Config of the connection.
54-
GetIP4Config() IP4Config
54+
GetIP4Config() (IP4Config, error)
5555

5656
// GetDHCP4Config gets the DHCP4Config of the connection.
57-
GetDHCP4Config() DHCP4Config
57+
GetDHCP4Config() (DHCP4Config, error)
5858

5959
// GetVPN gets the VPN flag of the connection.
60-
GetVPN() bool
60+
GetVPN() (bool, error)
6161

6262
// GetMaster gets the master device of the connection.
63-
GetMaster() Device
63+
GetMaster() (Device, error)
6464
}
6565

6666
func NewActiveConnection(objectPath dbus.ObjectPath) (ActiveConnection, error) {
@@ -72,90 +72,113 @@ type activeConnection struct {
7272
dbusBase
7373
}
7474

75-
func (a *activeConnection) GetConnection() Connection {
76-
path := a.getObjectProperty(ActiveConnectionProperyConnection)
75+
func (a *activeConnection) GetConnection() (Connection, error) {
76+
path, err := a.getObjectProperty(ActiveConnectionProperyConnection)
77+
if err != nil {
78+
return nil, err
79+
}
7780
con, err := NewConnection(path)
7881
if err != nil {
79-
panic(err)
82+
return nil, err
8083
}
81-
return con
84+
return con, nil
8285
}
8386

84-
func (a *activeConnection) GetSpecificObject() AccessPoint {
85-
path := a.getObjectProperty(ActiveConnectionProperySpecificObject)
87+
func (a *activeConnection) GetSpecificObject() (AccessPoint, error) {
88+
path, err := a.getObjectProperty(ActiveConnectionProperySpecificObject)
89+
if err != nil {
90+
return nil, err
91+
}
8692
ap, err := NewAccessPoint(path)
8793
if err != nil {
88-
panic(err)
94+
return nil, err
8995
}
90-
return ap
96+
return ap, nil
9197
}
9298

93-
func (a *activeConnection) GetID() string {
99+
func (a *activeConnection) GetID() (string, error) {
94100
return a.getStringProperty(ActiveConnectionProperyID)
95101
}
96102

97-
func (a *activeConnection) GetUUID() string {
103+
func (a *activeConnection) GetUUID() (string, error) {
98104
return a.getStringProperty(ActiveConnectionProperyUUID)
99105
}
100106

101-
func (a *activeConnection) GetType() string {
107+
func (a *activeConnection) GetType() (string, error) {
102108
return a.getStringProperty(ActiveConnectionProperyType)
103109
}
104110

105-
func (a *activeConnection) GetDevices() []Device {
106-
paths := a.getSliceObjectProperty(ActiveConnectionProperyDevices)
111+
func (a *activeConnection) GetDevices() ([]Device, error) {
112+
paths, err := a.getSliceObjectProperty(ActiveConnectionProperyDevices)
113+
if err != nil {
114+
return nil, err
115+
}
107116
devices := make([]Device, len(paths))
108-
var err error
109117
for i, path := range paths {
110118
devices[i], err = DeviceFactory(path)
111119
if err != nil {
112-
panic(err)
120+
return nil, err
113121
}
114122
}
115-
return devices
123+
return devices, nil
116124
}
117125

118-
func (a *activeConnection) GetState() uint32 {
126+
func (a *activeConnection) GetState() (uint32, error) {
119127
return a.getUint32Property(ActiveConnectionProperyState)
120128
}
121129

122-
func (a *activeConnection) GetStateFlags() uint32 {
130+
func (a *activeConnection) GetStateFlags() (uint32, error) {
123131
return a.getUint32Property(ActiveConnectionProperyStateFlags)
124132
}
125133

126-
func (a *activeConnection) GetDefault() bool {
127-
b := a.getProperty(ActiveConnectionProperyDefault)
128-
return b.(bool)
134+
func (a *activeConnection) GetDefault() (bool, error) {
135+
b, err := a.getProperty(ActiveConnectionProperyDefault)
136+
if err != nil {
137+
return false, err
138+
}
139+
return b.(bool), nil
129140
}
130141

131-
func (a *activeConnection) GetIP4Config() IP4Config {
132-
path := a.getObjectProperty(ActiveConnectionProperyIP4Config)
142+
func (a *activeConnection) GetIP4Config() (IP4Config, error) {
143+
path, err := a.getObjectProperty(ActiveConnectionProperyIP4Config)
144+
if err != nil {
145+
return nil, err
146+
}
133147
r, err := NewIP4Config(path)
134148
if err != nil {
135-
panic(err)
149+
return nil, err
136150
}
137-
return r
151+
return r, nil
138152
}
139153

140-
func (a *activeConnection) GetDHCP4Config() DHCP4Config {
141-
path := a.getObjectProperty(ActiveConnectionProperyDHCP4Config)
154+
func (a *activeConnection) GetDHCP4Config() (DHCP4Config, error) {
155+
path, err := a.getObjectProperty(ActiveConnectionProperyDHCP4Config)
156+
if err != nil {
157+
return nil, err
158+
}
142159
r, err := NewDHCP4Config(path)
143160
if err != nil {
144-
panic(err)
161+
return nil, err
145162
}
146-
return r
163+
return r, nil
147164
}
148165

149-
func (a *activeConnection) GetVPN() bool {
150-
ret := a.getProperty(ActiveConnectionProperyVPN)
151-
return ret.(bool)
166+
func (a *activeConnection) GetVPN() (bool, error) {
167+
ret, err := a.getProperty(ActiveConnectionProperyVPN)
168+
if err != nil {
169+
return false, err
170+
}
171+
return ret.(bool), nil
152172
}
153173

154-
func (a *activeConnection) GetMaster() Device {
155-
path := a.getObjectProperty(ActiveConnectionProperyMaster)
174+
func (a *activeConnection) GetMaster() (Device, error) {
175+
path, err := a.getObjectProperty(ActiveConnectionProperyMaster)
176+
if err != nil {
177+
return nil, err
178+
}
156179
r, err := DeviceFactory(path)
157180
if err != nil {
158-
panic(err)
181+
return nil, err
159182
}
160-
return r
183+
return r, nil
161184
}

0 commit comments

Comments
 (0)