go-ios icon indicating copy to clipboard operation
go-ios copied to clipboard

Fix unhandled Errors in the code

Open danielpaulus opened this issue 2 years ago • 3 comments

A few unhandled Errors sneaked their way into the code base over time f.ex. here: https://github.com/danielpaulus/go-ios/blob/f0d177e6276dac1fb4c130424bd64dceb0d9418a/ios/simlocation/simlocation.go#L165

Would be super helpful if someone could fix that.

danielpaulus avatar Oct 07 '22 17:10 danielpaulus

Hello.

Could you please provide more info on the unhanded errors you have spotted?

Thanks!

ioef avatar Oct 08 '22 09:10 ioef

The permalink above is a good example. The error is just silently ignored and not escalated, logged etc. Also check out this other example: https://github.com/danielpaulus/go-ios/blob/7344b452b035a41010b2e1648079782379501415/ios/testmanagerd/xcuitestrunner.go#L204 CloseXCUITestRunner returns an error, but it is not bubbled up, logged or handled in any way. Would be really great, to go through the codebase, find these and send a PR with a fix. I accept:

  • bubble the error up where possible (here f.ex. https://github.com/danielpaulus/go-ios/issues/179)
  • if the refactoring is to big, at least add a log.Error(err)
  • remove uses of panic() and replace with proper error handling

For a PR to get accepted, handling errors one at a time is ok to prevent huge refactoring work.

danielpaulus avatar Oct 08 '22 09:10 danielpaulus

Would a change like the following be appropriate in all of the affected areas throughout the code?

From this :

func DownloadReports(device ios.DeviceEntry, pattern string, targetdir string) error {
	if pattern == "" {
		return err
	}

To this:

func DownloadReports(device ios.DeviceEntry, pattern string, targetdir string) error {
	if pattern == "" {
		return fmt.Errorf("empty pattern not ok, just use *")
	}

Other example

from

func Encode(packet AfcPacket, writer io.Writer) error {
	err := binary.Write(writer, binary.LittleEndian, packet.Header)
	if err != nil {
		return err
	}
	_, err = writer.Write(packet.HeaderPayload)
	if err != nil {
		return err
	}

to

func Encode(packet AfcPacket, writer io.Writer) error {
	err := binary.Write(writer, binary.LittleEndian, packet.Header)
	if err != nil {
		return fmt.Errorf("Error writing header: %s", err)
	}
	_, err = writer.Write(packet.HeaderPayload)
	if err != nil {
		return fmt.Errorf("Error writing header payload: %s", err)
	}

Thanks!

ioef avatar Oct 08 '22 12:10 ioef