swift - How can I catch and log all errors? -


i'm using third-party functions, , don't know types of errors can produce.

i want catch , log such errors, errortype protocol not have members provide meaningful message or error code. can done log type of errors properly?

do {     try somefunction()  } catch wellknownerror { // handle } } catch let err {         let message = ... // how can error type , meaningful message err?     nslog(message)     } 

i think it's not option...

but can try this...

//---------------------------------------- // private api //----------------------------------------  public enum carerror : errortype {     case neverturnon     case outoffuel     case other(string) }  public class car {      public func turnon(newstate:int) throws -> bool     {          if newstate == 1         {             throw carerror.neverturnon         }         else if case 2...5 = newstate         {             throw carerror.outoffuel         }         else if newstate == 10         {             throw carerror.other("something gonna wrong")         }         else if newstate == 11         {             throw carerror.other(":(")         }          return false     }  } 

error handling

func main() {      let car = car()      {         try car.turnon(4)     } catch carerror.neverturnon {         print("turn off")     } catch let err nserror {         print(err.description)     } catch {         print("it's madness!!!")     }  }  main() 

with test... had result...

//error domain=trycatchtest.carerror code=1 "the operation couldn’t completed. (trycatchtest.carerror error 1.)" //program ended exit code: 0 

it's not best way solve problem, new way understand happening in background of framework.

now, asking favor.

please, let me final result ;)

it's doesn't matter if or bad result.


Comments