Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclass Python exceptions #7321

Merged
merged 4 commits into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ class ApiException(OpenApiException):
return error_message


class NotFoundException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(NotFoundException, self).__init__(status, reason, http_resp)


class UnauthorizedException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(UnauthorizedException, self).__init__(status, reason, http_resp)


class ForbiddenException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ForbiddenException, self).__init__(status, reason, http_resp)


class ServiceException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ServiceException, self).__init__(status, reason, http_resp)


def render_path(path_to_item):
"""Returns a string representation of a path"""
result = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import six
from six.moves.urllib.parse import urlencode
import urllib3

from {{packageName}}.exceptions import ApiException, ApiValueError
from {{packageName}}.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -213,6 +213,18 @@ class RESTClientObject(object):
logger.debug("response body: %s", r.data)

if not 200 <= r.status <= 299:
if r.status == 401:
raise UnauthorizedException(http_resp=r)

if r.status == 403:
raise ForbiddenException(http_resp=r)

if r.status == 404:
raise NotFoundException(http_resp=r)

if 500 <= r.status <= 599:
raise ServiceException(http_resp=r)

raise ApiException(http_resp=r)

return r
Expand Down
24 changes: 24 additions & 0 deletions samples/client/petstore/python-asyncio/petstore_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ def __str__(self):
return error_message


class NotFoundException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(NotFoundException, self).__init__(status, reason, http_resp)


class UnauthorizedException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(UnauthorizedException, self).__init__(status, reason, http_resp)


class ForbiddenException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ForbiddenException, self).__init__(status, reason, http_resp)


class ServiceException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ServiceException, self).__init__(status, reason, http_resp)


def render_path(path_to_item):
"""Returns a string representation of a path"""
result = ""
Expand Down
24 changes: 24 additions & 0 deletions samples/client/petstore/python-tornado/petstore_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ def __str__(self):
return error_message


class NotFoundException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(NotFoundException, self).__init__(status, reason, http_resp)


class UnauthorizedException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(UnauthorizedException, self).__init__(status, reason, http_resp)


class ForbiddenException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ForbiddenException, self).__init__(status, reason, http_resp)


class ServiceException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ServiceException, self).__init__(status, reason, http_resp)


def render_path(path_to_item):
"""Returns a string representation of a path"""
result = ""
Expand Down
24 changes: 24 additions & 0 deletions samples/client/petstore/python/petstore_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ def __str__(self):
return error_message


class NotFoundException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(NotFoundException, self).__init__(status, reason, http_resp)


class UnauthorizedException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(UnauthorizedException, self).__init__(status, reason, http_resp)


class ForbiddenException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ForbiddenException, self).__init__(status, reason, http_resp)


class ServiceException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ServiceException, self).__init__(status, reason, http_resp)


def render_path(path_to_item):
"""Returns a string representation of a path"""
result = ""
Expand Down
14 changes: 13 additions & 1 deletion samples/client/petstore/python/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from six.moves.urllib.parse import urlencode
import urllib3

from petstore_api.exceptions import ApiException, ApiValueError
from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -221,6 +221,18 @@ def request(self, method, url, query_params=None, headers=None,
logger.debug("response body: %s", r.data)

if not 200 <= r.status <= 299:
if r.status == 401:
raise UnauthorizedException(http_resp=r)

if r.status == 403:
raise ForbiddenException(http_resp=r)

if r.status == 404:
raise NotFoundException(http_resp=r)

if 500 <= r.status <= 599:
raise ServiceException(http_resp=r)

raise ApiException(http_resp=r)

return r
Expand Down
24 changes: 24 additions & 0 deletions samples/openapi3/client/petstore/python/petstore_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ def __str__(self):
return error_message


class NotFoundException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(NotFoundException, self).__init__(status, reason, http_resp)


class UnauthorizedException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(UnauthorizedException, self).__init__(status, reason, http_resp)


class ForbiddenException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ForbiddenException, self).__init__(status, reason, http_resp)


class ServiceException(ApiException):

def __init__(self, status=None, reason=None, http_resp=None):
super(ServiceException, self).__init__(status, reason, http_resp)


def render_path(path_to_item):
"""Returns a string representation of a path"""
result = ""
Expand Down
14 changes: 13 additions & 1 deletion samples/openapi3/client/petstore/python/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from six.moves.urllib.parse import urlencode
import urllib3

from petstore_api.exceptions import ApiException, ApiValueError
from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -221,6 +221,18 @@ def request(self, method, url, query_params=None, headers=None,
logger.debug("response body: %s", r.data)

if not 200 <= r.status <= 299:
if r.status == 401:
raise UnauthorizedException(http_resp=r)

if r.status == 403:
raise ForbiddenException(http_resp=r)

if r.status == 404:
raise NotFoundException(http_resp=r)

if 500 <= r.status <= 599:
raise ServiceException(http_resp=r)

raise ApiException(http_resp=r)

return r
Expand Down