nkl-kst/the-sports-db

Uncatchable error when a team is not found

Closed this issue · 1 comments

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.

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 😄