libvips / php-vips

php binding for libvips
MIT License
618 stars 25 forks source link

Better error message about disabled FFI #172

Closed levmv closed 2 years ago

levmv commented 2 years ago

Spent almost an hour trying to fix non-existent issue. It's was silly of me, but vips was keeping to throw "Unable to find library" exception and it's confusing. In the end issue was with ffi.enabled=off, of course :( (In my defense: debugging on a remote server with a different OS isn't so easy, and its php-fpm, and the project's exception handler clears stdout and I only see exceptions from vips in the project log)

Wanted to make a small PR, but not sure how to fix this better. One idea so far - to simple check for string ffi.enable in exception message (https://github.com/libvips/php-vips/blob/master/src/FFI.php#L252):

 $lastException = null; 
 foreach ($libraryPaths as $path) {
    try {
        ...
    } catch (\FFI\Exception $e) {
        $lastException = $e;
        Utils::debugLog("init", ["msg" => "library load failed", "exception" => $e]);
    }
}

 if ($vips === null) {
    if ($lastException && strpos($lastException->getMessage(), "ffi.enable") !== false) {
        throw new Exception("Please, check your FFI configuration", 0, $lastException);
    }
    ...
 }

Or may be without message and just re-throw $lastException?

jcupitt commented 2 years ago

I think I found a nice solution. If FFI is not enabled I now get:

$ ./thumbnail-loop.php ~/pics/k2.jpg
iteration, now (kb), growth (kb)
PHP Fatal error:  Uncaught FFI\Exception: FFI API is restricted by "ffi.enable" configuration directive in /home/john/GIT/php-vips/src/FFI.php:208
Stack trace:
#0 /home/john/GIT/php-vips/src/FFI.php(208): FFI::cdef()

If there's no libvips library I see:

$ ./thumbnail-loop.php ~/pics/k2.jpg
iteration, now (kb), growth (kb)
PHP Fatal error:  Uncaught Jcupitt\Vips\Exception: Unable to open library 'libvips.so.42' in any of ['/home/john/vips/lib64/', '/home/john/vips/lib/']. Make sure that you've installed libvips and that 'libvips.so.42' is on your system's library search path. in /home/john/GIT/php-vips/src/FFI.php:268
Stack trace:
WilliamDEdwards commented 4 months ago

This is still an issue.

In my case, ffi.enable had to be set to true, but it was set to preload (default). Causing the following error when calling FFI::cdef:

FFI API is restricted by "ffi.enable" configuration directive

The error reported in this issue said "Unable to find library". The 'solution' was replacing 'find' by 'open', which is correct. However, the error doesn't mention the exception that occurred - which leaves users guessing needlessly.

I think for my specific case, this branch tries to raise early. But it only catches ffi.enable not being set. So the non-descriptive error still occurs in my case, because ffi.enable is set, just to the wrong value...