Isit possible to set various status code into single flask error handler function?

  error-handling, flask, python, python-3.x

is there any way to convert my current structure to something close to what I expected? This will help to reduce alot of duplicated code as there might be 401, 402, 403,…

  • my current flask error handling’s structure
@error.app_errorhandler(400)
def error_400(error):
    message = error.description
    logger.info('post request fail : {}'.format(message))
    return make_response(jsonify({}), 400)

@error.app_errorhandler(429)
def error_429(error):
    message = error.description
    logger.info('post request fail : {}'.format(message))
    return make_response(jsonify({}), 429)
  • my expected flask error handling’s structure (not supported)
@error.app_errorhandler(400,429)
def error_4xx(error):
    message = error.description
    logger.info('post request fail : {}'.format(message))
    return make_response(jsonify({}), error.status_code)

Source: Python Questions

LEAVE A COMMENT