Skip to content

Commit 13f1c61

Browse files
Kylmakalleryanrishiwing328
authored
[python] Re-merge Subclass Python exceptions (#7321) (#8095)
* Subclass Python exceptions (#7321) * Subclass Python exceptions: - UnauthorizedException (401) - ForbiddenException (403) - NotFoundException (404) - ServiceException [500 - 599] Fixes #2151 * add generated sample code * use Python 2 flavor inheritance * regenerate samples * update samples Co-authored-by: Ryan Rishi <[email protected]> Co-authored-by: William Cheng <[email protected]>
1 parent 3de5370 commit 13f1c61

File tree

10 files changed

+185
-5
lines changed

10 files changed

+185
-5
lines changed

modules/openapi-generator/src/main/resources/python/exceptions.mustache

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,30 @@ class ApiException(OpenApiException):
118118
return error_message
119119

120120

121+
class NotFoundException(ApiException):
122+
123+
def __init__(self, status=None, reason=None, http_resp=None):
124+
super(NotFoundException, self).__init__(status, reason, http_resp)
125+
126+
127+
class UnauthorizedException(ApiException):
128+
129+
def __init__(self, status=None, reason=None, http_resp=None):
130+
super(UnauthorizedException, self).__init__(status, reason, http_resp)
131+
132+
133+
class ForbiddenException(ApiException):
134+
135+
def __init__(self, status=None, reason=None, http_resp=None):
136+
super(ForbiddenException, self).__init__(status, reason, http_resp)
137+
138+
139+
class ServiceException(ApiException):
140+
141+
def __init__(self, status=None, reason=None, http_resp=None):
142+
super(ServiceException, self).__init__(status, reason, http_resp)
143+
144+
121145
def render_path(path_to_item):
122146
"""Returns a string representation of a path"""
123147
result = ""

modules/openapi-generator/src/main/resources/python/rest.mustache

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from urllib.parse import urlencode
1212
import certifi
1313
import urllib3
1414

15-
from {{packageName}}.exceptions import ApiException, ApiValueError
15+
from {{packageName}}.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError
1616

1717

1818
logger = logging.getLogger(__name__)
@@ -212,6 +212,18 @@ class RESTClientObject(object):
212212
logger.debug("response body: %s", r.data)
213213

214214
if not 200 <= r.status <= 299:
215+
if r.status == 401:
216+
raise UnauthorizedException(http_resp=r)
217+
218+
if r.status == 403:
219+
raise ForbiddenException(http_resp=r)
220+
221+
if r.status == 404:
222+
raise NotFoundException(http_resp=r)
223+
224+
if 500 <= r.status <= 599:
225+
raise ServiceException(http_resp=r)
226+
215227
raise ApiException(http_resp=r)
216228

217229
return r

samples/client/petstore/python/petstore_api/exceptions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ def __str__(self):
126126
return error_message
127127

128128

129+
class NotFoundException(ApiException):
130+
131+
def __init__(self, status=None, reason=None, http_resp=None):
132+
super(NotFoundException, self).__init__(status, reason, http_resp)
133+
134+
135+
class UnauthorizedException(ApiException):
136+
137+
def __init__(self, status=None, reason=None, http_resp=None):
138+
super(UnauthorizedException, self).__init__(status, reason, http_resp)
139+
140+
141+
class ForbiddenException(ApiException):
142+
143+
def __init__(self, status=None, reason=None, http_resp=None):
144+
super(ForbiddenException, self).__init__(status, reason, http_resp)
145+
146+
147+
class ServiceException(ApiException):
148+
149+
def __init__(self, status=None, reason=None, http_resp=None):
150+
super(ServiceException, self).__init__(status, reason, http_resp)
151+
152+
129153
def render_path(path_to_item):
130154
"""Returns a string representation of a path"""
131155
result = ""

samples/client/petstore/python/petstore_api/rest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import certifi
2121
import urllib3
2222

23-
from petstore_api.exceptions import ApiException, ApiValueError
23+
from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError
2424

2525

2626
logger = logging.getLogger(__name__)
@@ -220,6 +220,18 @@ def request(self, method, url, query_params=None, headers=None,
220220
logger.debug("response body: %s", r.data)
221221

222222
if not 200 <= r.status <= 299:
223+
if r.status == 401:
224+
raise UnauthorizedException(http_resp=r)
225+
226+
if r.status == 403:
227+
raise ForbiddenException(http_resp=r)
228+
229+
if r.status == 404:
230+
raise NotFoundException(http_resp=r)
231+
232+
if 500 <= r.status <= 599:
233+
raise ServiceException(http_resp=r)
234+
223235
raise ApiException(http_resp=r)
224236

225237
return r

samples/openapi3/client/extensions/x-auth-id-alias/python/x_auth_id_alias/exceptions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ def __str__(self):
126126
return error_message
127127

128128

129+
class NotFoundException(ApiException):
130+
131+
def __init__(self, status=None, reason=None, http_resp=None):
132+
super(NotFoundException, self).__init__(status, reason, http_resp)
133+
134+
135+
class UnauthorizedException(ApiException):
136+
137+
def __init__(self, status=None, reason=None, http_resp=None):
138+
super(UnauthorizedException, self).__init__(status, reason, http_resp)
139+
140+
141+
class ForbiddenException(ApiException):
142+
143+
def __init__(self, status=None, reason=None, http_resp=None):
144+
super(ForbiddenException, self).__init__(status, reason, http_resp)
145+
146+
147+
class ServiceException(ApiException):
148+
149+
def __init__(self, status=None, reason=None, http_resp=None):
150+
super(ServiceException, self).__init__(status, reason, http_resp)
151+
152+
129153
def render_path(path_to_item):
130154
"""Returns a string representation of a path"""
131155
result = ""

samples/openapi3/client/extensions/x-auth-id-alias/python/x_auth_id_alias/rest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import certifi
2121
import urllib3
2222

23-
from x_auth_id_alias.exceptions import ApiException, ApiValueError
23+
from x_auth_id_alias.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError
2424

2525

2626
logger = logging.getLogger(__name__)
@@ -220,6 +220,18 @@ def request(self, method, url, query_params=None, headers=None,
220220
logger.debug("response body: %s", r.data)
221221

222222
if not 200 <= r.status <= 299:
223+
if r.status == 401:
224+
raise UnauthorizedException(http_resp=r)
225+
226+
if r.status == 403:
227+
raise ForbiddenException(http_resp=r)
228+
229+
if r.status == 404:
230+
raise NotFoundException(http_resp=r)
231+
232+
if 500 <= r.status <= 599:
233+
raise ServiceException(http_resp=r)
234+
223235
raise ApiException(http_resp=r)
224236

225237
return r

samples/openapi3/client/features/dynamic-servers/python/dynamic_servers/exceptions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ def __str__(self):
126126
return error_message
127127

128128

129+
class NotFoundException(ApiException):
130+
131+
def __init__(self, status=None, reason=None, http_resp=None):
132+
super(NotFoundException, self).__init__(status, reason, http_resp)
133+
134+
135+
class UnauthorizedException(ApiException):
136+
137+
def __init__(self, status=None, reason=None, http_resp=None):
138+
super(UnauthorizedException, self).__init__(status, reason, http_resp)
139+
140+
141+
class ForbiddenException(ApiException):
142+
143+
def __init__(self, status=None, reason=None, http_resp=None):
144+
super(ForbiddenException, self).__init__(status, reason, http_resp)
145+
146+
147+
class ServiceException(ApiException):
148+
149+
def __init__(self, status=None, reason=None, http_resp=None):
150+
super(ServiceException, self).__init__(status, reason, http_resp)
151+
152+
129153
def render_path(path_to_item):
130154
"""Returns a string representation of a path"""
131155
result = ""

samples/openapi3/client/features/dynamic-servers/python/dynamic_servers/rest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import certifi
2121
import urllib3
2222

23-
from dynamic_servers.exceptions import ApiException, ApiValueError
23+
from dynamic_servers.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError
2424

2525

2626
logger = logging.getLogger(__name__)
@@ -220,6 +220,18 @@ def request(self, method, url, query_params=None, headers=None,
220220
logger.debug("response body: %s", r.data)
221221

222222
if not 200 <= r.status <= 299:
223+
if r.status == 401:
224+
raise UnauthorizedException(http_resp=r)
225+
226+
if r.status == 403:
227+
raise ForbiddenException(http_resp=r)
228+
229+
if r.status == 404:
230+
raise NotFoundException(http_resp=r)
231+
232+
if 500 <= r.status <= 599:
233+
raise ServiceException(http_resp=r)
234+
223235
raise ApiException(http_resp=r)
224236

225237
return r

samples/openapi3/client/petstore/python/petstore_api/exceptions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ def __str__(self):
126126
return error_message
127127

128128

129+
class NotFoundException(ApiException):
130+
131+
def __init__(self, status=None, reason=None, http_resp=None):
132+
super(NotFoundException, self).__init__(status, reason, http_resp)
133+
134+
135+
class UnauthorizedException(ApiException):
136+
137+
def __init__(self, status=None, reason=None, http_resp=None):
138+
super(UnauthorizedException, self).__init__(status, reason, http_resp)
139+
140+
141+
class ForbiddenException(ApiException):
142+
143+
def __init__(self, status=None, reason=None, http_resp=None):
144+
super(ForbiddenException, self).__init__(status, reason, http_resp)
145+
146+
147+
class ServiceException(ApiException):
148+
149+
def __init__(self, status=None, reason=None, http_resp=None):
150+
super(ServiceException, self).__init__(status, reason, http_resp)
151+
152+
129153
def render_path(path_to_item):
130154
"""Returns a string representation of a path"""
131155
result = ""

samples/openapi3/client/petstore/python/petstore_api/rest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import certifi
2121
import urllib3
2222

23-
from petstore_api.exceptions import ApiException, ApiValueError
23+
from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError
2424

2525

2626
logger = logging.getLogger(__name__)
@@ -220,6 +220,18 @@ def request(self, method, url, query_params=None, headers=None,
220220
logger.debug("response body: %s", r.data)
221221

222222
if not 200 <= r.status <= 299:
223+
if r.status == 401:
224+
raise UnauthorizedException(http_resp=r)
225+
226+
if r.status == 403:
227+
raise ForbiddenException(http_resp=r)
228+
229+
if r.status == 404:
230+
raise NotFoundException(http_resp=r)
231+
232+
if 500 <= r.status <= 599:
233+
raise ServiceException(http_resp=r)
234+
223235
raise ApiException(http_resp=r)
224236

225237
return r

0 commit comments

Comments
 (0)