nkl-kst / the-sports-db

PHP library to get sports data from TheSportsDB (https://www.thesportsdb.com)
MIT License
22 stars 3 forks source link

Uncatchable error when a team is not found #13

Closed shutupflanders closed 3 years ago

shutupflanders commented 3 years ago

I'm dynamically passing in team names to try and retrieve information about them, but when the api cannot find any teams with the given query it throws the following error:

Example query: $this->client->search()->teams("TestyA");

{
    "message": "Invalid argument supplied for foreach()",
    "exception": "ErrorException",
    "file": "/app/vendor/netresearch/jsonmapper/src/JsonMapper.php",
    "line": 410,
    "trace": [
        {
            "file": "/app/vendor/netresearch/jsonmapper/src/JsonMapper.php",
            "line": 410,
            "function": "handleError",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        },
        {
            "file": "/app/vendor/nkl-kst/the-sports-db/src/Serializer/AbstractSerializer.php",
            "line": 76,
            "function": "mapArray",
            "class": "JsonMapper",
            "type": "->"
        },
        {
            "file": "/app/vendor/nkl-kst/the-sports-db/src/Serializer/Serializer.php",
            "line": 220,
            "function": "serialize",
            "class": "NklKst\\TheSportsDb\\Serializer\\AbstractSerializer",
            "type": "->"
        },
        {
            "file": "/app/vendor/nkl-kst/the-sports-db/src/Client/Endpoint/SearchEndpoint.php",
            "line": 91,
            "function": "serializeTeams",
            "class": "NklKst\\TheSportsDb\\Serializer\\Serializer",
            "type": "->"
        },
        {
            "file": "/app/src/BossLabs/TheSportsDb/src/Webservice/TheSportsDbWebservice.php",
            "line": 28,
            "function": "teams",
            "class": "NklKst\\TheSportsDb\\Client\\Endpoint\\SearchEndpoint",
            "type": "->"
        },
        ...

Can we look at throwing a catchable exception or at least return an empty array to avoid this? Happy to help if required.

nkl-kst commented 3 years ago

Thanks for opening this issue Martin. We should return an empty array on unmatched queries, because empty results are nothing special that should throw an exception. The API returns null instead of an empty array if no team is found, which causes the underlying JsonMapper to fail.

Fortunately the code is prepared for this, so I fixed it with b815982. This fix will be released with version 1.0.0 in the next couple of days. Feel free to open new issues if you find additional bugs, I would love to fix them before releasing version 1.0.0. If you find nothing more, I'm happy too 😄