raise BadRequest 必须 raise ApiError return make_api_resp() """ @app.errorhandler(ApiError) def error_handler(error): return api_error_handler(error) from werkzeug.exceptions import HTTPException, MethodNotAllowed from werkzeug.http import HTTP_STATUS_CODES def default_error_handler(error): return make_api_resp(status_code=error.code, msg=error.name) for status_code in HTTP_STATUS_CODES.keys(): app.error_handler_spec[None][status_code] = default_error_handler """设置前处理和后处理""" @app.before_request def before_request(): """如果HTTP Body为Json数据,需要设置Header Content-Type: application/json """ logger.info(Logger.dumps(t='req')) request.on_json_loading_failed = on_json_loading_failed @app.after_request def after_request(resp): return resp
@app.errorhandler(ApiError) def error_handler(error): return api_error_handler(error) from werkzeug.exceptions import HTTPException, MethodNotAllowed from werkzeug.http import HTTP_STATUS_CODES def default_error_handler(error): return make_api_resp(status_code=error.code, msg=error.name) for status_code in HTTP_STATUS_CODES.keys(): app.error_handler_spec[None][status_code] = default_error_handler """设置前处理和后处理""" @app.before_request def before_request(): """如果HTTP Body为Json数据,需要设置Header Content-Type: application/json """ logger.info(Logger.dumps(t='req')) request.on_json_loading_failed = on_json_loading_failed @app.after_request def after_request(resp):
def message_for_failed_should(self): message = 'Expected the status code {0}, but got {1}.'.format( status, self._actual ) if self._response_data: response = 'Response Data:\n"{0}"'.format(self._response_data) message = '\n'.join([message, response]) return message def message_for_failed_should_not(self): return 'Expected the status code not to be {0}'.format(status) return Checker # Make be_xxx matchers for all the status codes _status_codes = HTTP_STATUS_CODES.keys() for code in _status_codes: matcher(make_status_checker('be', code)) matcher(make_status_checker('abort', code)) matcher(make_status_checker('return', code)) @matcher class RedirectMatcher(object): ''' A matcher to check for redirects ''' name = 'redirect_to' def __call__(self, location): self._expected = 'http://localhost' + location self._status_ok = True return self