对于早期运行良好的定义,引发ValueError异常

时间:2019-05-22 08:20:59

标签: python google-cloud-functions

我在Google Cloud Functions中部署了一些功能。到目前为止,它们工作正常,但现在引发了ValueError异常。可能是什么原因?

我试图在没有try:块的情况下重新排列代码,但只有崩溃了。

def bws_func(request):
    request_json = request.get_json()
    request_args = request.args

    try:
        lat = float(request_json['lat'])
        lon = float(request_json['lon'])
        if type(lat) == int or type(lat) == float or type(lon) == int or type(lon) == float:
            if lat < 89 and lat > -89 and lon < 180 and lon > -180:
                _bws = bws(float(lat),float(lon))
                return jsonify(_bws), 200
            else:
                return jsonify({"Error": "latitude and/or longitude is not valid"}), 400
        else:
            return jsonify({"Error": "latitude or longitude is non integer or float"}), 400
    except ValueError:
        return jsonify({"Error": "input cannot be converted to float"}), 400

正确的响应曾经是如下所示的JSON对象:

{
  "estimated_3sec_gust": {
    "100_recurrence": 38.84864760491245,
    "10y_recurrence": 33.672778503723734,
    "25y_recurrence": 36.17428872168459,
    "50y_recurrence": 37.63056054611294
  }
}
Date: Wed, 29 May 2019 04:17:38 GMT
Server: Wolfram Cloud
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-GeneratedFrom: Generated from the Wolfram Cloud: http://www.wolfram.com/cloud
X-Terms-of-Use: Terms of use: http://www.wolfram.com/legal/terms/wolfram-cloud.html
X-Data-Source-Information: Data source information: http://wolfram.com/knowledgebase/source-information
content-disposition: inline
Access-Control-Allow-Origin: *
Content-Type: text/plain;charset=utf-8
Vary: Accept-Encoding
Transfer-Encoding: chunked

网络调用的Python代码。

def bws_func(request):
    request_json = request.get_json()
    request_args = request.args

    try:
        lat = float(request_json['lat'])
        lon = float(request_json['lon'])
        if type(lat) == float and type(lon) == float:
            if lat < 89 and lat > -89 and lon < 180 and lon > -180:
                _bws = windfinder(lat,lon)
                return _bws, 200
            else:
                return jsonify({"Error": "latitude and/or longitude is not valid"}), 400
        else:
            return jsonify({"Error": "latitude or longitude is non integer or float"}), 400
    except ValueError:
        return jsonify({"Error": "input cannot be converted to float"}), 400

0 个答案:

没有答案