title: wrong url is not triggering error using combine
body: <p>I am using the code here. But I couldn't figure out how to use the code here.</p>
  <p><strong>Stack Overflow <a href="https://stackoverflow.com/questions/65126447/error-is-not-triggering-on-url-session-using-combine">link</a></strong></p>
  <p>How can I run this function with combine?
  How can I run a function with return type <code>AnyPublisher&lt;T, NetworkingError&gt;</code> with combine and catch errors.
  my apikey is faulty and i want to catch this error.</p>
  <p><strong>Function:</strong></p>
  <pre><code>private func performOperation&lt;T: Decodable&gt;(requestUrl: URLRequest, responseType: T.Type) -&gt; AnyPublisher&lt;T, NetworkingError&gt; {
          URLSession.shared.dataTaskPublisher(for: requestUrl)
              .jsonDecodingPublisher(type: T.self)
      }
  </code></pre>
  <p><strong>My Code:</strong></p>
  <pre><code>class MovieService {
      
      private let baseURL = &quot;https://api.themoviedb.org/3/movie&quot;
      private let apiKey = &quot;594b8eb4999a8b44ad5136ee3ed1ebdb31&quot;
      private let language = &quot;language=en-US&quot;
      var anyCancellable = Set&lt;AnyCancellable&gt;()
      init() { }
      
      func loadAllMovie(categoryType: CategoryType, page: Int) -&gt; Future&lt;Movie, Error&gt; {
          
          
          if let url = URL(string: baseURL + &quot;/&quot; + categoryType.rawValue + &quot;?&quot; + &quot;api_key=&quot; + apiKey + &quot;&amp;&quot; + language + &quot;&amp;&quot; + &quot;page=&quot; + &quot;\(page)&quot;) {
              
              let foo = performOperation(requestUrl: URLRequest(url: url), responseType: Movie.self)
              foo
                  .receive(on: DispatchQueue.main)
                  .sink { error in
                      switch error {
                          
                      case .finished:
                          print(&quot;end&quot;)
                      case .failure(let error):
                          print(error.localizedDescription)
                      }
                  } receiveValue: { movie in
                      print(movie)
                  }
                  .store(in: &amp;anyCancellable)
              
          } else {
              print(&quot;url sıkıntılı&quot;)
          }
          
          
          
          if let data = getData(from: baseURL, categoryType: categoryType, page: page) {
              do {
                  
                  let bookDecoder = try JSONDecoder().decode(Movie.self, from: data)
                  
                  return Future { promixe in
                      promixe(.success(bookDecoder))
                  }
              }catch {
                  debugPrint(error.localizedDescription)
                  debugPrint(&quot;errrrr: \(error)&quot;)
                  print(&quot;olmadı&quot;)
              }
          }
          return Future{ _ in }
      }
      
      private func performOperation&lt;T: Decodable&gt;(requestUrl: URLRequest, responseType: T.Type) -&gt; AnyPublisher&lt;T, NetworkingError&gt; {
          URLSession.shared.dataTaskPublisher(for: requestUrl)
              .jsonDecodingPublisher(type: T.self)
      }
      
      private func getData(from urlString: String, categoryType: CategoryType, page: Int) -&gt; Data? {
          
          guard let url = URL(string: urlString + &quot;/&quot; + categoryType.rawValue + &quot;?&quot; + &quot;api_key=&quot; + apiKey + &quot;&amp;&quot; + language + &quot;&amp;&quot; + &quot;page=&quot; + &quot;\(page)&quot;) else {
              fatalError(&quot;Couldn't load URL from provided string.&quot;)
          }
          
          do {
              return try Data(contentsOf: url)
          } catch {
              debugPrint(&quot;Couldn't load data from URL:\n\(error)&quot;)
              return nil
          }
      }
  }
  
  enum NetworkingError: Error {
      case decoding(DecodingError)
      case incorrectStatusCode(Int)
      case network(URLError)
      case nonHTTPResponse
      case unknown(Error)
  }
  
  extension Publisher {
      func mapErrorToNetworkingError() -&gt; AnyPublisher&lt;Output, NetworkingError&gt; {
          mapError { error -&gt; NetworkingError in
              switch error {
              case let decodingError as DecodingError:
                  return .decoding(decodingError)
              case let networkingError as NetworkingError:
                  return networkingError
              case let urlError as URLError:
                  return .network(urlError)
              default:
                  return .unknown(error)
              }
          }
          .eraseToAnyPublisher()
      }
  }
  
  extension URLSession.DataTaskPublisher {
      func emptyBodyResponsePublisher() -&gt; AnyPublisher&lt;Void, NetworkingError&gt; {
          httpResponseValidator()
              .map { _ in Void() }
              .eraseToAnyPublisher()
      }
  }
  
  extension URLSession.DataTaskPublisher {
      func httpResponseValidator() -&gt; AnyPublisher&lt;Output, NetworkingError&gt; {
          tryMap { data, response in
              guard let httpResponse = response as? HTTPURLResponse else { throw NetworkingError.nonHTTPResponse }
              let statusCode = httpResponse.statusCode
              guard (200..&lt;300).contains(statusCode) else { throw NetworkingError.incorrectStatusCode(statusCode) }
              return (data, httpResponse)
          }
          .mapErrorToNetworkingError()
      }
      
      func httpResponseValidatorDataPublisher() -&gt; AnyPublisher&lt;Data, NetworkingError&gt; {
          httpResponseValidator()
              .map(\.data)
              .eraseToAnyPublisher()
      }
      
      func jsonDecodingPublisher&lt;T:Decodable&gt;(type: T.Type) -&gt; AnyPublisher&lt;T, NetworkingError&gt; {
          httpResponseValidatorDataPublisher()
              .decode(type: T.self, decoder: JSONDecoder())
              .mapErrorToNetworkingError()
      }
  }
  </code></pre>