Connexion: Any possibility to override default validation error message?

Created on 4 Feb 2016  路  7Comments  路  Source: zalando/connexion

Hello, default error message is formed in problem module, I would like to change this like I did for Flask app:

@flask_app.errorhandler(404)
def handle_not_found(error):
    my_error = myerrors.Error('Path not found', code=404, extra=str(type(error)))
    error_response = jsonify(my_error.to_dict())
    error_response.status_code = 404
    return error_response

However I didn't manage to do it with connexion swagger validation errors. Any help?

enhancement help wanted in progress

Most helpful comment

Just for the record I hacked this together from elsewhere to overwrite validation errors to my own needs:

@app.app.after_request
def rewrite_bad_request(response):
    if response.status_code == 400 and response.data.decode('utf-8').find('"title":') != None:
        logger.warning("Holy shit Batman!")
        original = loads(response.data.decode('utf-8'))
        response.data=dumps({'code': 'VALIDATION_ERROR', 'message': original["detail"]})
        response.headers['Content-Type'] = 'application/json'
    return response

Good enough but a better way to override would be super

All 7 comments

I think you can't override the Connexion error response right now (only with "monkey patching").

Probably a reasonable feature request...

Just for the record I hacked this together from elsewhere to overwrite validation errors to my own needs:

@app.app.after_request
def rewrite_bad_request(response):
    if response.status_code == 400 and response.data.decode('utf-8').find('"title":') != None:
        logger.warning("Holy shit Batman!")
        original = loads(response.data.decode('utf-8'))
        response.data=dumps({'code': 'VALIDATION_ERROR', 'message': original["detail"]})
        response.headers['Content-Type'] = 'application/json'
    return response

Good enough but a better way to override would be super

Hi @ilya-khaustov, were you still planning on making this enhancement or should someone else take over?

@LappleApple No, I'm not planning an enhancement. Everybody else can continue with it.

@LappleApple I think this is an interesting feature. So I would be glad to see someone working on it in the future.

Done in #281

apparently, this can get closed?

Was this page helpful?
0 / 5 - 0 ratings