title: Swift Combine chaining .mapError()
body: <p>I'm trying to achieve something similar to scenario presented below (create URL, request to server, decode json, error on every step wrapped in custom <code>NetworkError</code> enum):</p>
  
  <pre class="lang-swift prettyprint-override"><code>enum NetworkError: Error {
      case badUrl
      case noData
      case request(underlyingError: Error)
      case unableToDecode(underlyingError: Error)
  }
  
  //...
      func searchRepos(with query: String, success: @escaping (ReposList) -&gt; Void, failure: @escaping (NetworkError) -&gt; Void) {
          guard let url = URL(string: searchUrl + query) else {
              failure(.badUrl)
              return
          }
  
          session.dataTask(with: url) { data, response, error in
              guard let data = data else {
                  failure(.noData)
                  return
              }
  
              if let error = error {
                  failure(.request(underlyingError: error))
                  return
              }
  
              do {
                  let repos = try JSONDecoder().decode(ReposList.self, from: data)
  
                  DispatchQueue.main.async {
                      success(repos)
                  }
              } catch {
                  failure(.unableToDecode(underlyingError: error))
              }
          }.resume()
      }
  </code></pre>
  
  <p>My solution in Combine works:</p>
  
  <pre class="lang-swift prettyprint-override"><code>    func searchRepos(with query: String) -&gt; AnyPublisher&lt;ReposList, NetworkError&gt; {
          guard let url = URL(string: searchUrl + query) else {
              return Fail(error: .badUrl).eraseToAnyPublisher()
          }
  
          return session.dataTaskPublisher(for: url)
              .mapError { NetworkError.request(underlyingError: $0) }
              .map { $0.data }
              .decode(type: ReposList.self, decoder: JSONDecoder())
              .mapError { $0 as? NetworkError ?? .unableToDecode(underlyingError: $0) }
              .subscribe(on: DispatchQueue.global())
              .receive(on: DispatchQueue.main)
              .eraseToAnyPublisher()
      }
  </code></pre>
  
  <p>but I really don't like this line</p>
  
  <pre><code>.mapError { $0 as? NetworkError ?? .unableToDecode(underlyingError: $0) }
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li>Is there better way to map errors (and replace line above) using chaining in Combine? </li>
  <li>Is there any way to include first <code>guard let</code> with <code>Fail(error:)</code> in chain?</li>
  </ol>