Search code examples
pythonjsonazureflaskhttp-error

API endpoint returning 500 server error - traceback returning attribute error


I have a Python API with three endpoints hosted on Azure. One of the endpoints started returning a server error last week. I checked the platform so everything is OK there.

I checked the logs and found that for this endpoint, the traceback returns this line at the end

File ".\app\models.py", line 280, in json
 return {'entity': otherentity.name, 'details': self.details, 'entity_id': otherentity.id,
AttributeError: 'NoneType' object has no attribute 'name'

This is the stdout and std error:

StdOut: 

StdErr: D:\home\site\wwwroot\env\Lib\site-
packages\flask_cache\jinja2ext.py:33: ExtDeprecationWarning: Importing 
flask.ext.cache is deprecated, use flask_cache instead.
 from flask.ext.cache import make_template_fragment_key

ErrorCode   Access is denied.
(0x5)

The block of code that line 280 references is part of a class. Here is the block:

def json(self, entityid):
    otherentity = self.entity_1 if entityid == self.entity_id2 else self.entity_2
    return {'entity': otherentity.name, 'details': self.details, 'entity_id': otherentity.id,
            'id': self.id}

This app was kind of thrown at me so I'm new at debugging and from what I can see, the issue is with calling .name because it doesn't exist? Also, not sure if flask_cache being deprecated has anything to do with this problem.

I found that flask_cache is no longer maintained and I should be using flask-caching instead.

Any ideas about how to proceed to fix this server error? I'm not sure why the other two endpoints using the same model works but not this one.


Solution

  • The solution was validating the code with an if/else statement on the back-end. User input accepted through front-end caused a failure in the connection and that fix corrected the issue.

    Next step is to add input validation to front-end to avoid future issues. Thanks to anyone who checked this out and helped.