New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid error response for non-existent file #254

Closed
nwhitt opened this Issue Sep 6, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@nwhitt
Copy link

nwhitt commented Sep 6, 2017

Issue summary

Attempting to load a non-existent file throws a fatal error instead of a League\Csv\Exception.

System informations

Information Description
League\Csv version 9.0.1
PHP/HHVM version 7.0.22-0ubuntu0.16.04.1
OS Platform ubuntu

Standalone code, or other way to reproduce the problem

Reader::createFromPath('/does/not/exist.csv');

Expected result

Similar to Flysystem

[League\Flysystem\FileNotFoundException]
File not found at path: /does/not/exist.csv

Actual result

[Symfony\Component\Debug\Exception\FatalThrowableError]
Wrong parameters for League\Csv\Exception([string $message [, long $code [, Throwable $previous = NULL]]])
@nyamsprod

This comment has been minimized.

Copy link
Member

nyamsprod commented Sep 7, 2017

@nwhitt League CSV and Flysystem are two independent packages so they do not follow the same exception mechanism :) .
Having said that I failed to reproduce your error with you given code I get this:

PHP Fatal error:  Uncaught League\Csv\Exception: fopen(/does/not/exist.csv): failed to open stream: No such file or directory in ....

There's already a test to validate this output in the test suite. Are you sure about the error ? Maybe there's something else at play here

@nyamsprod nyamsprod added the question label Sep 7, 2017

@nwhitt

This comment has been minimized.

Copy link

nwhitt commented Sep 7, 2017

It must be in the error handling of my symfony package with suppression, as I get green on unit tests. I'll close this issue until I have more information.

@IllyaMoskvin

This comment has been minimized.

Copy link

IllyaMoskvin commented Oct 11, 2017

I had this exception happen because the user under which PHP was running (e.g. apache) did not have permissions to do anything except read the target file, which did indeed exist.

I fixed it by adding the 'r' mode to my createFromPath call (cf. fopen mode):

Reader::createFromPath('/file/that/actually/exists.csv', 'r');

I know it's somewhat off-topic, but this issue is the top result in Google for that error message, so maybe this will help someone.

@csiszarattila

This comment has been minimized.

Copy link

csiszarattila commented Nov 3, 2017

You can reproduce this error if you have a set_error_handler() somewhere in your code.

A custom set_error_handler() emits errors, and error_get_last() returns null, hence the exception throwing not working in this line, it will generate an error:

csv/src/Stream.php

Lines 177 to 178 in 6418c1c

if (!$resource = @fopen(...$args)) {
throw new Exception(error_get_last()['message']);

The error easily reproducable with this code:

set_error_handler(function($errno, $errstr) {
    var_dump($errno, $errstr);
});

@fopen("test.cvs", 'r');
var_dump(error_get_last());

--- outputs: 
int(2)
string(65) "fopen(test.cvs): failed to open stream: No such file or directory"
NULL

I think error_get_last()['message'] needs to be checked, and return a general message if its empty.

@nyamsprod

This comment has been minimized.

Copy link
Member

nyamsprod commented Nov 6, 2017

Good catch this is a easy fix. I'll try to release a patch this week or the next one... or you can submit a PR with the fix if you want

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment