Missing custom abort message under swagger-codegen architecture

75 views
Skip to first unread message

Paul Lo

unread,
Jun 26, 2015, 4:35:05 AM6/26/15
to swagger-sw...@googlegroups.com

My application is based on flask-restful-swaggerswagger-codegen, and python.

I universally use abort() in server side logic for unexpected situations, and it works fine on swagger UI (the abort reason is displayed fine).

from flask.ext.restful import abort

class PlatformInfo(Resource, Parameter):
    def __init__(self):
        pass

    @swagger.operation(
        notes='Get platform info',
        type=SomeType,
        responseClass=SomeType.__name__,
        nickname='get_platform_info',
        parameters=[
        ],
        responseMessages=[
            {
                "code": 200,
                "message": "Successfully get platform info."
            },
            {
                "code": 400,
                "message": "Error occurs while retrieving platform info."
            },
        ])
    def get(self):
        response_dict = dict()
        try:
            response_dict = get_platform_data()
except:
   abort(400, message='custom error messages here....')

        return response_dict, 200


However, if I use the client generated by swagger-codegen, the abort message would be missing. I can only get something like HTTP Error 400: BAD REQUEST
(swagger-codegen provides a generic API client which handles the client-server communication, as follows)

class ApiClient:
"""Generic API client for Swagger client library builds"""
def __init__(self, apiKey=None, apiServer=None):
if apiKey == None:
raise Exception('You must pass an apiKey when instantiating the '
'APIClient')
self.apiKey = apiKey
self.apiServer = apiServer
self.cookie = None
def callAPI(self, resourcePath, method, queryParams, postData,
headerParams=None):
url = self.apiServer + resourcePath
headers = {}
if headerParams:
for param, value in headerParams.iteritems():
headers[param] = value
headers['Content-type'] = 'application/json'
headers['api_key'] = self.apiKey
if self.cookie:
headers['Cookie'] = self.cookie
data = None
if method == 'GET':
...(omitted)
else:
raise Exception('Method ' + method + ' is not recognized.')
request = MethodRequest(method=method, url=url, headers=headers,
data=data)
# Make the request
response = urllib2.urlopen(request)
if 'Set-Cookie' in response.headers:
self.cookie = response.headers['Set-Cookie']
string = response.read()
try:
data = json.loads(string)
except ValueError: # PUT requests don't return anything
data = None
return data

Looks like abort() would be caught in 
HTTPDefaultErrorHandler of urllib2.py, and the message we put could not be found here anymore.

class HTTPDefaultErrorHandler(BaseHandler):
    def http_error_default(self, req, fp, code, msg, hdrs):
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

Does anybody have similar experience and ideas?

wing328

unread,
Jun 27, 2015, 4:48:04 AM6/27/15
to swagger-sw...@googlegroups.com
Discussion at https://github.com/swagger-api/swagger-codegen/issues/922

Paul Lo於 2015年6月26日星期五 UTC+8下午4時35分05秒寫道:
...
Reply all
Reply to author
Forward
0 new messages