# Path: autopush/exceptions.py
# class RouterException(AutopushException):
#     """Exception if routing has failed, may include a custom status_code and
#     body to write to the response.
# 
#     """
#     def __init__(self, message, status_code=500, response_body="",
#                  router_data=None, headers=None, log_exception=True,
#                  errno=None, logged_status=None, **kwargs):
#         """Create a new RouterException"""
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.headers = {} if headers is None else headers
#         self.log_exception = log_exception
#         self.response_body = response_body or message
#         self.errno = errno
#         self.logged_status = logged_status
#         self.extra = kwargs


import json
import hyper.tls
from collections import deque
from decimal import Decimal
from hyper import HTTP20Connection
from hyper.http20.exceptions import HTTP20Error
from autopush.exceptions import RouterException
                # We'll treat that as a premature socket closure.
                response = connection.get_response(stream_id)
                if response.status != 200:
                    reason = json.loads(
                            response.read().decode('utf-8'))['reason']
                    raise RouterException(
                        "APNS Transmit Error {}:{}".format(response.status,
                                                           reason),
                        status_code=response.status,
                        response_body="APNS could not process "
                                      "your message {}".format(reason),
                        log_exception=False
                    )
                break
            except (HTTP20Error, IOError):
                connection.close()
                attempt += 1
                if attempt < self._max_retry:
                    continue
                raise
            finally:
                # Returning a closed connection to the pool is ok.
                # hyper will reconnect on .request()
                self._return_connection(connection)

    def _get_connection(self):
        try:
            connection = self.connections.pop()
            return connection
        except IndexError:

# Path: autopush/exceptions.py
# class RouterException(AutopushException):
#     """Exception if routing has failed, may include a custom status_code and
#     body to write to the response.
# 
#     """
#     def __init__(self, message, status_code=500, response_body="",
#                  router_data=None, headers=None, log_exception=True,
#                  errno=None, logged_status=None, **kwargs):
#         """Create a new RouterException"""
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.headers = {} if headers is None else headers
#         self.log_exception = log_exception
#         self.response_body = response_body or message
#         self.errno = errno
#         self.logged_status = logged_status
#         self.extra = kwargs


# Path: autopush/router/apns2.py
import json
import hyper.tls
from collections import deque
from decimal import Decimal
from hyper import HTTP20Connection
from hyper.http20.exceptions import HTTP20Error
from autopush.exceptions import RouterException

                # We'll treat that as a premature socket closure.
                response = connection.get_response(stream_id)
                if response.status != 200:
                    reason = json.loads(
                            response.read().decode('utf-8'))['reason']
                    raise RouterException(
                        "APNS Transmit Error {}:{}".format(response.status,
                                                           reason),
                        status_code=response.status,
                        response_body="APNS could not process "
                                      "your message {}".format(reason),
                        log_exception=False
                    )
                break
            except (HTTP20Error, IOError):
                connection.close()
                attempt += 1
                if attempt < self._max_retry:
                    continue
                raise
            finally:
                # Returning a closed connection to the pool is ok.
                # hyper will reconnect on .request()
                self._return_connection(connection)

    def _get_connection(self):
        try:
            connection = self.connections.pop()
            return connection
        except IndexError:

            raise RouterException(