Skip to content

Commit 0fe0fcf

Browse files
authored
Remove log.Fatal and replace with error propagation (danielpaulus#33)
* remove log.Fatal and replace with error propagation * remove log.fatal from devicelist, getvalues and productversion * replace log.Fatal with t.Fail * replace log.Fatal with t.Fail * remove log.Fatal from redadpair and replace with proper errors * remove log.Fatal from readpair, syslog, utils, pcap and dproxy * remove remaining log.Fatal * remove fatal from main.go * fix incorrect error logging * use t.Fatal
1 parent f16993e commit 0fe0fcf

31 files changed

+230
-188
lines changed

ios/accessibility/accessibility_control.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package accessibility
22

33
import (
4+
"fmt"
5+
46
dtx "github.com/danielpaulus/go-ios/ios/dtx_codec"
57
"github.com/danielpaulus/go-ios/ios/nskeyedarchiver"
68
log "github.com/sirupsen/logrus"
@@ -17,7 +19,7 @@ func (a ControlInterface) readhostAppStateChanged() {
1719
msg := a.channel.ReceiveMethodCall("hostAppStateChanged:")
1820
stateChange, err := nskeyedarchiver.Unarchive(msg.Auxiliary.GetArguments()[0].([]byte))
1921
if err != nil {
20-
log.Fatal(err)
22+
panic(err)
2123
}
2224
value := stateChange[0]
2325
log.Infof("hostAppStateChanged:%s", value)
@@ -29,7 +31,7 @@ func (a ControlInterface) readhostInspectorNotificationReceived() {
2931
msg := a.channel.ReceiveMethodCall("hostInspectorNotificationReceived:")
3032
notification, err := nskeyedarchiver.Unarchive(msg.Auxiliary.GetArguments()[0].([]byte))
3133
if err != nil {
32-
log.Fatal(err)
34+
panic(err)
3335
}
3436
value := notification[0].(map[string]interface{})["Value"]
3537
log.Infof("hostInspectorNotificationReceived:%s", value)
@@ -144,7 +146,7 @@ func (a ControlInterface) awaitHostInspectorCurrentElementChanged() map[string]i
144146
log.Info("received hostInspectorCurrentElementChanged")
145147
result, err := nskeyedarchiver.Unarchive(msg.Auxiliary.GetArguments()[0].([]byte))
146148
if err != nil {
147-
log.Fatalf("Failed unarchiving: %s this is a bug and should not happen", err)
149+
panic(fmt.Sprintf("Failed unarchiving: %s this is a bug and should not happen", err))
148150
}
149151
return result[0].(map[string]interface{})
150152
}

ios/accessibility/accessibility_integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import (
1313
func TestIT(t *testing.T) {
1414
device, err := ios.GetDevice("")
1515
if err != nil {
16-
log.Fatal(err)
16+
t.Fatal(err)
1717
}
1818

1919
conn, err := accessibility.New(device)
2020
if err != nil {
21-
log.Fatal(err)
21+
t.Fatal(err)
2222
}
2323

2424
conn.SwitchToDevice()
2525
if err != nil {
26-
log.Fatal(err)
26+
t.Fatal(err)
2727
}
2828
conn.EnableSelectionMode()
2929
conn.GetElement()

ios/connect.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package ios
22

33
import (
44
"fmt"
5-
6-
log "github.com/sirupsen/logrus"
75
)
86

97
type connectMessage struct {
@@ -122,19 +120,25 @@ func (muxConn *UsbMuxConnection) connectWithStartServiceResponse(deviceID int, s
122120
return nil
123121
}
124122

125-
func ConnectLockdownWithSession(device DeviceEntry) *LockDownConnection {
123+
func ConnectLockdownWithSession(device DeviceEntry) (*LockDownConnection, error) {
126124
muxConnection, err := NewUsbMuxConnectionSimple()
127125
if err != nil {
128-
log.Fatal(err)
126+
return nil, fmt.Errorf("USBMuxConnection failed with: %v", err)
129127
}
130128
defer muxConnection.ReleaseDeviceConnection()
131129

132-
pairRecord := muxConnection.ReadPair(device.Properties.SerialNumber)
130+
pairRecord, err := muxConnection.ReadPair(device.Properties.SerialNumber)
131+
if err != nil {
132+
return nil, fmt.Errorf("could not retrieve PairRecord with error: %v", err)
133+
}
133134

134135
lockdownConnection, err := muxConnection.ConnectLockdown(device.DeviceID)
135136
if err != nil {
136-
log.Fatal(err)
137+
return nil, fmt.Errorf("Lockdown connection failed with: %v", err)
138+
}
139+
resp, err := lockdownConnection.StartSession(pairRecord)
140+
if err != nil {
141+
return nil, fmt.Errorf("StartSession failed: %+v error: %v", resp, err)
137142
}
138-
lockdownConnection.StartSession(pairRecord)
139-
return lockdownConnection
143+
return lockdownConnection, nil
140144
}

ios/debugproxy/binforward.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func handleConnectToService(connectRequest ios.UsbMuxMessage,
6464
serviceInfo PhoneServiceInformation) {
6565
err := muxToDevice.SendMuxMessage(connectRequest)
6666
if err != nil {
67-
p.log.Fatal("Failed sending muxmessage to device")
67+
panic("Failed sending muxmessage to device")
6868
}
6969
connectResponse, err := muxToDevice.ReadMessage()
7070
muxOnUnixSocket.SendMuxMessage(connectResponse)

ios/debugproxy/debugproxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (d *DebugProxy) Launch(device ios.DeviceEntry, binaryMode bool) error {
100100
d.setupDirectory()
101101
listener, err := net.Listen("unix", ios.DefaultUsbmuxdSocket)
102102
if err != nil {
103-
log.Fatal("Could not listen on usbmuxd socket, do I have access permissions?", err)
103+
log.Error("Could not listen on usbmuxd socket, do I have access permissions?", err)
104104
return err
105105
}
106106

@@ -198,7 +198,7 @@ func writeJSON(filePath string, JSON interface{}) {
198198
file, err := os.OpenFile(filePath,
199199
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
200200
if err != nil {
201-
log.Fatal("Could not write to file, this should not happen", err, filePath)
201+
panic(fmt.Sprintf("Could not write to file err: %v filepath:'%s'", err, filePath))
202202
}
203203
jsonmsg, err := json.Marshal(JSON)
204204
if err != nil {

ios/debugproxy/decoders.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func writeBytes(filePath string, data []byte) {
164164
file, err := os.OpenFile(filePath,
165165
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
166166
if err != nil {
167-
log.Fatal("Could not write to file, this should not happen", err, filePath)
167+
panic(fmt.Sprintf("Could not write to file error: %v path:'%s'", err, filePath))
168168
}
169169

170170
file.Write(data)

ios/debugproxy/muxhandler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package debugproxy
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67

78
ios "github.com/danielpaulus/go-ios/ios"
@@ -44,7 +45,7 @@ func proxyUsbMuxConnection(p *ProxyConnection, muxOnUnixSocket *ios.UsbMuxConnec
4445
continue
4546
}
4647
if err != nil {
47-
p.log.Fatal("Failed forwarding message to device", request)
48+
panic(fmt.Sprintf("Failed forwarding message to device: %+v", request))
4849
}
4950
if decodedRequest["MessageType"] == "Listen" {
5051
handleListen(p, muxOnUnixSocket, muxToDevice)
@@ -100,7 +101,7 @@ func handleConnect(connectRequest ios.UsbMuxMessage, decodedConnectRequest map[s
100101
} else {
101102
info, err := p.debugProxy.retrieveServiceInfoByPort(ios.Ntohs(uint16(port)))
102103
if err != nil {
103-
p.log.Fatalf("ServiceInfo for port: %d not found, this is a bug :-)reqheader: %+v repayload: %x", port, connectRequest.Header, connectRequest.Payload)
104+
panic(fmt.Sprintf("ServiceInfo for port: %d not found, this is a bug :-)reqheader: %+v repayload: %x", port, connectRequest.Header, connectRequest.Payload))
104105
}
105106
p.log.Infof("Connection to service '%s' detected on port %d", info.ServiceName, info.ServicePort)
106107
handleConnectToService(connectRequest, decodedConnectRequest, p, muxOnUnixSocket, muxToDevice, info)
@@ -110,7 +111,7 @@ func handleConnect(connectRequest ios.UsbMuxMessage, decodedConnectRequest map[s
110111
func handleConnectToLockdown(connectRequest ios.UsbMuxMessage, decodedConnectRequest map[string]interface{}, p *ProxyConnection, muxOnUnixSocket *ios.UsbMuxConnection, muxToDevice *ios.UsbMuxConnection) {
111112
err := muxToDevice.SendMuxMessage(connectRequest)
112113
if err != nil {
113-
p.log.Fatal("Failed sending muxmessage to device")
114+
panic("Failed sending muxmessage to device")
114115
}
115116
connectResponse, err := muxToDevice.ReadMessage()
116117
muxOnUnixSocket.SendMuxMessage(connectResponse)

ios/deviceconnection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (conn *DeviceConnection) Close() {
6262
func (conn *DeviceConnection) Send(bytes []byte) error {
6363
n, err := conn.c.Write(bytes)
6464
if n < len(bytes) {
65-
log.Fatalf("DeviceConnection failed writing %d bytes, only %d sent", len(bytes), n)
65+
log.Errorf("DeviceConnection failed writing %d bytes, only %d sent", len(bytes), n)
6666
}
6767
if err != nil {
6868
log.Errorf("Failed sending: %s", err)

ios/dtx_codec/connection.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dtx
22

33
import (
4+
"errors"
45
"io"
6+
"net"
57
"sync"
68

79
ios "github.com/danielpaulus/go-ios/ios"
@@ -114,8 +116,8 @@ func reader(dtxConn *Connection) {
114116
reader := dtxConn.deviceConnection.Reader()
115117
msg, err := ReadMessage(reader)
116118
if err != nil {
117-
if err == io.EOF || err.Error() == "use of closed network connection" {
118-
log.Debug("Closing DTX Connection")
119+
if err == io.EOF || err == net.ErrClosed || errors.Unwrap(err) == net.ErrClosed {
120+
log.Debug("DTX Connection with EOF")
119121
return
120122
}
121123
log.Errorf("error reading dtx connection %+v", err)

ios/dtx_codec/decoder_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestErrors(t *testing.T) {
2323

2424
dat, err = ioutil.ReadFile("fixtures/notifyOfPublishedCapabilites")
2525
if err != nil {
26-
log.Fatal(err)
26+
t.Fatal(err)
2727
}
2828
for i := 0; i < len(dat)-4; i++ {
2929
_, _, err = dtx.DecodeNonBlocking(dat[0 : 4+i])
@@ -36,12 +36,12 @@ func TestCodec2(t *testing.T) {
3636
dat, err := ioutil.ReadFile("fixtures/requestChannelWithCodeIdentifier.bin")
3737

3838
if err != nil {
39-
log.Fatal(err)
39+
t.Fatal(err)
4040
}
4141

4242
fixtureMsg, _, err := dtx.DecodeNonBlocking(dat)
4343
if err != nil {
44-
log.Fatal(err)
44+
t.Fatal(err)
4545
}
4646
payloadBytes, err := nskeyedarchiver.ArchiveBin(fixtureMsg.Payload[0])
4747
assert.NoError(t, err)
@@ -59,7 +59,7 @@ func TestCodec(t *testing.T) {
5959
dat, err := ioutil.ReadFile("fixtures/requestChannelWithCodeIdentifier.bin")
6060

6161
if err != nil {
62-
log.Fatal(err)
62+
t.Fatal(err)
6363
}
6464
var remainingBytes []byte
6565
remainingBytes = dat
@@ -68,7 +68,7 @@ func TestCodec(t *testing.T) {
6868
log.Info(msg.StringDebug())
6969
remainingBytes = s
7070
if !assert.NoError(t, err) {
71-
log.Fatal("whet", err)
71+
t.Fatal("whet", err)
7272
}
7373
bytes, err := dtx.Encode(3, 0, 0, true, 2, msg.RawBytes[303:], msg.Auxiliary)
7474
if assert.NoError(t, err) {
@@ -86,7 +86,7 @@ func TestAXDump(t *testing.T) {
8686
dat, err := ioutil.ReadFile("fixtures/dtactivitytapmessage.bin")
8787

8888
if err != nil {
89-
log.Fatal(err)
89+
t.Fatal(err)
9090
}
9191
var remainingBytes []byte
9292
remainingBytes = dat
@@ -95,7 +95,7 @@ func TestAXDump(t *testing.T) {
9595
log.Info(msg)
9696
remainingBytes = s
9797
if !assert.NoError(t, err) {
98-
log.Fatal("whet", err)
98+
t.Fatal("whet", err)
9999
}
100100
}
101101

@@ -109,7 +109,7 @@ func TestType1Message(t *testing.T) {
109109
dat, err := ioutil.ReadFile("fixtures/unknown-d-h-h-message.bin")
110110

111111
if err != nil {
112-
log.Fatal(err)
112+
t.Fatal(err)
113113
}
114114
var remainingBytes []byte
115115
remainingBytes = dat
@@ -118,7 +118,7 @@ func TestType1Message(t *testing.T) {
118118
log.Info(msg)
119119
remainingBytes = s
120120
if !assert.NoError(t, err) {
121-
log.Fatal("whet", err)
121+
t.Fatal("whet", err)
122122
}
123123
}
124124

@@ -127,7 +127,7 @@ func TestType1Message(t *testing.T) {
127127
func TestFragmentedMessage(t *testing.T) {
128128
dat, err := ioutil.ReadFile("fixtures/fragmentedmessage.bin")
129129
if err != nil {
130-
log.Fatal(err)
130+
t.Fatal(err)
131131
}
132132

133133
//test the non blocking decoder first
@@ -193,7 +193,7 @@ func TestFragmentedMessage(t *testing.T) {
193193
func TestDecoder(t *testing.T) {
194194
dat, err := ioutil.ReadFile("fixtures/notifyOfPublishedCapabilites")
195195
if err != nil {
196-
log.Fatal(err)
196+
t.Fatal(err)
197197
}
198198
msg, remainingBytes, err := dtx.DecodeNonBlocking(dat)
199199
if assert.NoError(t, err) {

0 commit comments

Comments
 (0)