Skip to content

Commit 9de0fe9

Browse files
committed
pass error details if available in RequestError
1 parent 1512275 commit 9de0fe9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Adafruit_IO/errors.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
# SOFTWARE.
21+
22+
import json
23+
2124
class AdafruitIOError(Exception):
2225
"""Base class for all Adafruit IO request failures."""
2326
pass
@@ -26,8 +29,16 @@ class AdafruitIOError(Exception):
2629
class RequestError(Exception):
2730
"""General error for a failed Adafruit IO request."""
2831
def __init__(self, response):
29-
super(RequestError, self).__init__("Adafruit IO request failed: {0} {1}".format(
30-
response.status_code, response.reason))
32+
error_message = self._parse_error(response)
33+
super(RequestError, self).__init__("Adafruit IO request failed: {0} {1} - {2}".format(
34+
response.status_code, response.reason, error_message))
35+
36+
def _parse_error(self, response):
37+
try:
38+
content = json.loads(response.content)
39+
return ' - '.join(content['error'])
40+
except ValueError:
41+
return ""
3142

3243

3344
class ThrottlingError(AdafruitIOError):

0 commit comments

Comments
 (0)