Repository: recurly/recurly-client-python

Issue:
Error trying to cast error response
**Describe the bug**

Seems there is an error while trying to cast an error response due to a missing error class.

**To Reproduce**

I'm doing a preview request with a wrong zip code (we are using avalara here) and the raw response from the api is the following:

```
{"error":{"type":"service_not_available","message":"Tax service currently not available, please try again later."}}
```

The exception I'm having with the python client is the following:

```
  File ".../lib/python3.8/site-packages/recurly/client.py", line 2718, in preview_purchase
    return self._make_request("POST", path, body, None)
  File ".../lib/python3.8/site-packages/recurly/base_client.py", line 45, in _make_request
    raise Resource.cast_error(resp)
  File .../lib/python3.8/site-packages/recurly/resource.py", line 52, in cast_error
    return klass(
TypeError: 'NoneType' object is not callable
```

Seems code around https://github.com/recurly/recurly-client-python/blob/master/recurly/resource.py#L33 tries to build a class with the name `ServiceNotAvailableError` (matching the returned error type in the json response) but that doesn't exist.

**Expected behavior**

Error response is parsed and a `recurly.ApiError` is raised.

**Your Environment**

- Which version of this library are you using? 3.4.1
- Which version of the language are you using? 3.8


Comments:

Patch:
diff --git a/recurly/resource.py b/recurly/resource.py
index a3b671c..8677ab4 100644
--- a/recurly/resource.py
+++ b/recurly/resource.py
@@ -42,9 +42,13 @@ class Resource:
         if not class_name.endswith("Error"):
             class_name += "Error"
         klass = locate("recurly.errors.%s" % class_name)
-        return klass(
-            error.message + ". Recurly Request Id: " + response.request_id, error
-        )
+        msg = error.message + ". Recurly Request Id: " + response.request_id
+        # Use a specific error class if we can find one, else
+        # fall back to a generic ApiError
+        if klass:
+            return klass(msg, error)
+        else:
+            return recurly.ApiError(msg, error)
 
     @classmethod
     def cast_json(cls, properties, class_name=None, response=None):


Test patch:
diff --git a/tests/test_resource.py b/tests/test_resource.py
index 9bca2bf..882d4bc 100644
--- a/tests/test_resource.py
+++ b/tests/test_resource.py
@@ -1,6 +1,7 @@
 import unittest
 import recurly
 import platform
+import json
 from datetime import datetime
 from datetime import timezone
 from recurly import Resource, Response, Request
@@ -15,6 +16,10 @@ def cast(obj, class_name=None, resp=None):
     return Resource.cast_json(obj, class_name, resp)
 
 
+def cast_error(obj, resp=None):
+    return Resource.cast_error(obj)
+
+
 class TestResource(unittest.TestCase):
     def test_cast_object_unknown_class(self):
         # should return the original dict
@@ -83,6 +88,29 @@ class TestResource(unittest.TestCase):
         self.assertEqual(type(page.data[0]), MyResource)
         self.assertEqual(page.data[0].my_string, "kmxu3f3qof17")
 
+    def test_cast_unknown_error(self):
+        resp = MagicMock()
+        resp.request_id = "1234"
+        resp.body = json.dumps(
+            {"error": {"type": "unknown", "message": "Error Message"}}
+        ).encode("UTF-8")
+        err = cast_error(resp)
+
+        # When the error class is unknown, it should fallback to ApiError
+        self.assertEqual(type(err), recurly.ApiError)
+        self.assertEqual(str(err), "Error Message. Recurly Request Id: 1234")
+
+    def test_cast_error(self):
+        resp = MagicMock()
+        resp.request_id = "1234"
+        resp.body = json.dumps(
+            {"error": {"type": "validation", "message": "Invalid"}}
+        ).encode("UTF-8")
+        err = cast_error(resp)
+
+        self.assertEqual(type(err), recurly.errors.ValidationError)
+        self.assertEqual(str(err), "Invalid. Recurly Request Id: 1234")
+
     def test_cast(self):
         obj = cast(
             {