Promisekit: Promise3 + catching an error on Promise<Void>

Created on 23 Oct 2015  路  6Comments  路  Source: mxcl/PromiseKit

Not sure what i'm doing wrong but i returned a promise in one of my functions a Promise because i don't need to give something back when it succeeds. But now when i try to catch the error it always gives me this: "Cannot call value of non-function type 'ErrorType?'". I'm currently working on the last Xcode (7.1) and this is what i am trying to do:

myFunction().error() {
        error -> Void in

        print("My error: \(error.description)")
    }

Does anyone know what i am doing wrong?

kind regards,
Daan

Most helpful comment

I'm also seeing this confusing compiler error, and I can't make sense of what is confusing the Swift compiler so badly, but your suggestions (remove the ()) didn't work. However, after much playing around, I got it working...

With a func doThing() -> Promise<Void> and another func handleError(e: NSError), I tried:

      firstly {
          doThing()
      }.then {
          NSLog("DONE!")
      }.error { (e) -> () in
//      ^^^^^  Cannot call value of non-function type 'ErrorType?'
          handleError(e)
      }

But adding a second line to the method changed the error. Any second line would do, NSLog("error") or return were what I tried. The following gave a different error:

      firstly {
          doThing()
      }.then {
          NSLog("DONE!")
      }.error { (e) -> () in
          handleError(e)
//                    ^ Cannot convert value of type 'ErrorType' to expected
          return
      }

Which was easily enough fixed, and then I could remove the superfluous second line too:

      firstly {
          doThing()
      }.then {
          NSLog("DONE!")
      }.error { (e) -> () in
          handleError(e as NSError)
      }
// Compiles fine

I hope this helps someone else who gets here via google. :smiley:

All 6 comments

Don't use () it is confusing the swift compiler apparently.

So just .error {

In fact it is necessary to provide a parameter to the closure or the Swift compiler is confused with our error property:

.error { error in

This way it works with parenthesis too:

.error() { error in

Swift compiler bug.

I'm also seeing this confusing compiler error, and I can't make sense of what is confusing the Swift compiler so badly, but your suggestions (remove the ()) didn't work. However, after much playing around, I got it working...

With a func doThing() -> Promise<Void> and another func handleError(e: NSError), I tried:

      firstly {
          doThing()
      }.then {
          NSLog("DONE!")
      }.error { (e) -> () in
//      ^^^^^  Cannot call value of non-function type 'ErrorType?'
          handleError(e)
      }

But adding a second line to the method changed the error. Any second line would do, NSLog("error") or return were what I tried. The following gave a different error:

      firstly {
          doThing()
      }.then {
          NSLog("DONE!")
      }.error { (e) -> () in
          handleError(e)
//                    ^ Cannot convert value of type 'ErrorType' to expected
          return
      }

Which was easily enough fixed, and then I could remove the superfluous second line too:

      firstly {
          doThing()
      }.then {
          NSLog("DONE!")
      }.error { (e) -> () in
          handleError(e as NSError)
      }
// Compiles fine

I hope this helps someone else who gets here via google. :smiley:

It did help at least one person. Thank you nevans!

Make that two. Been pulling my hair out on this. Thanks @nevans !!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

drekka picture drekka  路  5Comments

emreond picture emreond  路  5Comments

mxcl picture mxcl  路  4Comments

matteonovelli picture matteonovelli  路  7Comments

anilabsinc-ajay picture anilabsinc-ajay  路  6Comments