title: Decoding an Error Response using Alamofire 5 and the responseDecodable function
body: <p>I'm writing an API Client for WooCommerce using Alamofire 5 (beta 1) which will allow me to get orders, coupons etc as well as create them. Note I am using the new <code>.responseDecodable</code> function.</p>
  
  <p>I've set up my API client using the following <code>performRequest</code> function that looks like this:</p>
  
  <pre><code>@discardableResult
  private static func performRequest&lt;T:Decodable&gt;(route: APIConfiguration,
                                                  decoder: JSONDecoder = JSONDecoder(),
                                                  completion: @escaping (Result&lt;T&gt;)-&gt;Void) -&gt; DataRequest {
      return AF.request(route)
          .responseDecodable(decoder: decoder) { (response: DataResponse&lt;T&gt;) in
              completion(response.result)
      }
  }
  </code></pre>
  
  <p>This works well, since I can, for instance, call a function <code>getCouponForId(_ id: Int)</code> which will execute this function and have the response returned through the completion handler.</p>
  
  <p>The only downfall is that, say the user tries to access a coupon that does not exist, they will receive an error (404 from the server). I can switch on the result to determine either a <code>success</code> or <code>failure</code> case, but Alamofire attempts to decode the body of the error response into the <code>Coupon</code> model I have created.</p>
  
  <p>Going forward, I have created an error model which I intend to have the error decoded using. But with that said, I'm having trouble implementing it into this function.</p>
  
  <p>Does anyone have any ideas on how I could handle this? </p>
  
  <p>(I have created this function through following this guide - hopefully, it might provide a bit more context to what I'm doing. <a href="https://github.com/AladinWay/NetworkingExample" rel="nofollow noreferrer">https://github.com/AladinWay/NetworkingExample</a>)</p>