Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim framework Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Cannot create LogWriter

I recently started using Slim framework. I am currently attempting to authenticate using Middleware. This is the where the middleware is called:

$app->post('/profile', 'authenticate', function () {
     // code here
}

This is where my middleware takes care of the authentication:

function authenticate(\Slim\Route $route) {
    // Getting request headers
    $headers = apache_request_headers();
    $response = array();
    $app = \Slim\Slim::getInstance();

    // Verifying Authorization Header
    if (isset($headers['Authorization'])) {
        $db = new DbHandler();

        // get the api key
        $api_key = $headers['Authorization'];
        // validating api key
        if (!$db->isValidApiKey($api_key)) {
            // api key is not present in users table
            $response["error"] = true;
            $response["message"] = "Access Denied. Invalid Api key";
            echoRespnse(401, $response);
            $app->stop();
        } else {
            global $user_id;
            // get user primary key id
            $user_id = $db->getUserId($api_key);
        }
    } else {
        // api key is missing in header
        $response["error"] = true;
        $response["message"] = "Api key is misssing";
        echoRespnse(400, $response);
        $app->stop();
    }
};

I receive this error when I attempt the POST call. Other calls not requiring authentication all work correctly.

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Cannot create LogWriter. Invalid resource handle.' in D:\Liftmo\Liftmo.com\api\Slim\LogWriter.php:60 Stack trace: #0 D:\Liftmo\Liftmo.com\api\Slim\Slim.php(191): Slim\LogWriter->__construct(false) #1 D:\Liftmo\Liftmo.com\api\Slim\Helper\Set.php(228): Slim\Slim->Slim{closure}(Object(Slim\Helper\Set)) #2 D:\Liftmo\Liftmo.com\api\Slim\Helper\Set.php(88): Slim\Helper\Set->Slim\Helper{closure}(Object(Slim\Helper\Set)) #3 D:\Liftmo\Liftmo.com\api\Slim\Helper\Set.php(185): Slim\Helper\Set->get('logWriter') #4 D:\Liftmo\Liftmo.com\api\Slim\Slim.php(196): Slim\Helper\Set->offsetGet('logWriter') #5 D:\Liftmo\Liftmo.com\api\Slim\Helper\Set.php(228): Slim\Slim->Slim{closure}(Object(Slim\Helper\Set)) #6 D:\Liftmo\Liftmo.com\api\Slim\Helper\Set.php(88): Slim\Helper\Set->Slim\Helper{closure}(Object(Slim\Helper\Set)) #7 D:\Liftmo\Liftmo.com\api\Slim\Slim.php(234): Slim\Helper\Set->get('log') #8 D:\Liftmo\Liftmo.com\api\Slim\Slim.php(402): Slim\Slim->__get('log') in D:\Liftmo\Liftmo.com\api\Slim\LogWriter.php on line 60

I've run through the code line by line and it seems the issue lies with the line

$db = new DbHandler();

I'm pretty confused though because the class was used without issue everywhere else in my code.

require_once 'include/DbHandler.php'; // DbHandler included here at the top

I'd really appreciate it if someone could help me out. I took the authentication from HERE. Literally could not find this issue resolved anywhere. Using Google App Engine with Cloud SQL to deploy.

like image 391
jiaweizhang Avatar asked Feb 18 '26 20:02

jiaweizhang


1 Answers

I was having the same issue in my app, and i found this two tips that helped a lot:

  1. Set the google_app_engine.disable_readonly_filesystem = 1 property in php.ini file stored in the root of your app. reference--> https://gae-php-tips.appspot.com/2015/03/

  2. Use a diferent way than deafault to handle logging, to be able to see which part of your code or database have the problem. I found a great advice of how to do it in: https://leolutz.com/2014/11/slim-framework-on-google-app-engine/

Hope it helps!

like image 129
Santiago Salinas Avatar answered Feb 20 '26 08:02

Santiago Salinas