Rxswift: Custom Error Objects

Created on 17 Mar 2017  路  4Comments  路  Source: ReactiveX/RxSwift

Short description of the issue:
Observalbes emitting custom error objects

Description:

We have this class that represents an API error

public class APIResponseError :  Error {

    var code : Int?
    var errors : [String]?
    var message : String?
}

When the network services receives an error we want to emit this event, This is how i'm doing it

var apiError : APIResponseError!
//Configuring this error object 
observer.on(Event.error(apiError))

Till now it's great.
But when we subscribe to this observable.

  .subscribe(onNext: { (categories) in
                    self.categories = categories
                    self.setupPageView()

                }, onError: { (error ) in
                  //Here I'm forced to parse it as  APIResponseError
                     let errorObject = error as! APIResponseError
                     //handle the error 
                })

There should be an easier , cleaner way to emit custom error objects

Most helpful comment

Hi @wassimseif ,

if you need to parse the error, then you don't have a Rx error, but a special value case. You can use your enum or a Result as an element type.

If Swift compiler adds support for specifying generic error throwing constraints, then we can maybe consider some other options.

All 4 comments

Hi @wassimseif ,

if you need to parse the error, then you don't have a Rx error, but a special value case. You can use your enum or a Result as an element type.

If Swift compiler adds support for specifying generic error throwing constraints, then we can maybe consider some other options.

Alright! Got it, thanks

@kzaher : Any Update on this issue ??

Please read the discussion #1320.
There's no update yet, it won't happen in RxSwift 5 (maybe next version).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marlowcharite picture marlowcharite  路  3Comments

trungp picture trungp  路  3Comments

gekitz picture gekitz  路  3Comments

trant picture trant  路  3Comments

Z-JaDe picture Z-JaDe  路  3Comments