1

I have a route that calls a third-party API and returns its response. This route can take an ID as a parameter like so: /my/route/{id}.

When I request /my/route/1 I get a 200 success, but when I do /my/route/2, /my/route/3, /my/route/4, etc I get 500 error.

The funny thing is that I always get the correct response body. So both the 200 and 500 responses are returning the data that I need.

My issue is when getSponsor(..) is triggered here:

<?php

namespace App\Http\Controllers\Matrix;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class SponsorReadingController extends Controller
{
    /**
     * SponsorReadingController constructor.
     */
    public function __construct()
    {
        $this->cookieJar = app('MatrixCookieJar');
        $this->client = app('GuzzleClientForMatrix');
    }


    public function getSponsor($sponsorId, Request $request)
    {
        // TODO: Refactor this monstrous function

        if (!AuthController::isAlreadyLoggedIn($request)) {
            $loginRes = AuthController::loginToMatrixApi($this->cookieJar);

            if ($loginRes->getStatusCode() === 200) {
                $sessionId = AuthController::getSessionIdFromResponse($loginRes);
            } else {
                return $loginRes;
            }
        } else {
            $sessionId = null;

            AuthController::setAuthCookie(
                $this->cookieJar, $request->cookie('matrix_api_session')
            );
        }

        $respData = [
            'error' => null,
            'message' => null,
            'data' => json_decode(
                $this->client->get(
                    '/admin/sponsor/detail',
                    [
                        'query' => ['sponsorId' => $sponsorId],
                        'cookies' => $this->cookieJar,
                        'allow_redirects' => false
                    ]
                )->getBody())->response
        ];

        return $this->handleResponse($respData, $sessionId);
    }


    /**
     * Handle the response with the provided data and
     * cookie value.
     *
     * @param array $respData
     * @param string $cookieVal
     * @return Response
     */
public function handleResponse($respData, $cookieVal)
{
    if (!empty($cookieVal)) {
        return response()->json($respData)->withCookie(
            cookie('matrix_api_session', $cookieVal, 29, '/matrix/api')
        );
    }

    return response()->json($respData);
}

EDIT: If I do dd($res) instead of return $res inside handleResponse(...) I get a 200 status code, weird.

jstudios
  • 856
  • 1
  • 9
  • 26
  • So is the error in handle response, or in getSponsor? Seems like something might be happening when getSponsor tries to return your result from handleResponse. – Daniel Jul 15 '16 at 03:29
  • Well `getSponsor()` ultimately returns `handleResponse()`. The funny thing is that if I dump the status code I always get 200 on all calls. I am breaking my head. – jstudios Jul 15 '16 at 03:40
  • What happens if you dump $respData before sending to handleResponse? You mentioned some IDs work and some don't.Also, is the only thing coming back from $res a 200 response? When I tested it just now, I got a response object back that included the data I passed to Response(). – Daniel Jul 15 '16 at 03:48
  • @Daniel `dd($respData)` gives me a multidimensional array. Like I mentioned in my OP edit, if I `dd()` the response instead of returning it it comes back with a 200 success, all of them. – jstudios Jul 15 '16 at 13:55

1 Answers1

0

For reference, this answer helped me out: 500 Internal Server Error for php file not for html

So basically, I added ini_set('display_errors', 1) so the response would include any errors on the back-end that I wasn't seeing (turned out apache's error log had it), and sure enough there was an error that was not directly affecting the response so I would still get the correct response data.

The file were the cookies were being stored couldn't be seen from Guzzle's point of view, but the Laravel app itself could see it. For obvious reasons you need to specify the full path to a folder/file on the server. I ended up using Laravel's own storage folder to store them, so I had to change my service provider from this (where cookies/jar.json used to be in Laravel's public folder:

$this->app->bind('MatrixCookieJar', function ($app) {
    return new FileCookieJar(
        'cookies/jar.json', true
    );
});

To this:

$this->app->singleton('MatrixCookieJar', function ($app) {
    return new FileCookieJar(
        storage_path('cookies').'/'.'jar.json', true
    );
});
Community
  • 1
  • 1
jstudios
  • 856
  • 1
  • 9
  • 26