0

in flask i return simple json string like this : in case of error:

the Exception coming from this :

   try:
     
     self.data.add('data', self.request.data)
     
    except Exception as err:     
       print(traceback.format_exc())
       return _build_response_json()
 

the _build_response_json() function return string json as shown below :

getting this exception/Traceback:

  Unable to display children:Error resolving variables Traceback (most recent call last):
  File "C:\app\base.py", line 73, in get_name
    self.data.add('data', self.request.data)
AttributeError: 'ObjRequest' object has no attribute 'data'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_resolver.py", line 213, in resolve
    return dict[key]
KeyError: 'Exception'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 1195, in do_it
    _typeName, val_dict = pydevd_vars.resolve_compound_variable_fields(self.thread_id, self.frame_id, self.scope, self.attributes)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_vars.py", line 281, in resolve_compound_variable_fields
    var = getVariable(thread_id, frame_id, scope, attrs)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_vars.py", line 239, in getVariable
    var = resolver.resolve(var, k)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_resolver.py", line 215, in resolve
    return getattr(dict, key)
AttributeError: 'dict' object has no attribute 'Exception'

The json :

def _build_response_json()
       retur_json = {
            "status": "error",
            "title": "error",
            "message": "this is error massage from server",
            "type": "error",
            "status": 500,
            "id": "xxxxxi"
        }
        
        
    json_str = json.dumps(retur_json)
    return json_str 

but in the client side when error i keep getting : res.status = 500
error = INTERNAL SERVER ERROR
options = error

$.ajax({
            type: "POST",
            url: "/execute",
            data: data,
            success: function (res) {
               ....
            },
            error: function (res, options, error) {
                                 
                console.log(res.status);
                console.log(error);
                console.log(options);
            }
        });

what im doing wrong here ?

davidism
  • 121,510
  • 29
  • 395
  • 339
user63898
  • 29,839
  • 85
  • 272
  • 514
  • Can you show the trace back of the error message on the flask app? If there is no traceback, you have to set debugging to true. – Krerkkiat Chusap Aug 01 '21 at 11:05
  • im running in debug mode , how can i see the traceback? – user63898 Aug 01 '21 at 11:19
  • It should be in the terminal that you run `flask run` – Krerkkiat Chusap Aug 01 '21 at 11:20
  • i updated my question with the traceback – user63898 Aug 01 '21 at 11:35
  • That looks like you are trying to write a custom wrapper around flask? For a pure flask, access request data can just be `request.data` where `request` is from `from flask import request` See https://stackoverflow.com/a/16664376/10163723 for more detail. If you cannot use that we will need to see how `ObjectifyRequest` is declared, and how you pass the data to it. – Krerkkiat Chusap Aug 01 '21 at 11:41
  • But the thing as i understand it it go to the exception ,and the exception build the json string to return back just fine , but somewhere from the return to the client the json got lost – user63898 Aug 01 '21 at 11:47
  • Huh, interesting. Can you give us full traceback then? Like everything – Krerkkiat Chusap Aug 01 '21 at 11:53
  • 1
    That looks like error in pycharm. If you run it without pycharm or without debugging it in pycharm does the problem still there? And by debug mode I mentioned in the first comment I mean the debug mode of flask which is to set `debug=True` for `app.run(debug=True)` and not to run the flask application with a debugger. – Krerkkiat Chusap Aug 01 '21 at 12:12

0 Answers0