Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Process] [DX] throw Exceptions for exitcodes != 0 #11796

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

Closed
thkoch2001 opened this issue Aug 29, 2014 · 3 comments
Closed

[Process] [DX] throw Exceptions for exitcodes != 0 #11796

thkoch2001 opened this issue Aug 29, 2014 · 3 comments

Comments

@thkoch2001
Copy link

I've a ProcessUtil class in my project that does two things.

  1. Throw an Exception for exit codes !=0 and return process output instead of the exit code:
    public static function runOrThrow(Process $process) {
        $process->run();

        if($process->isSuccessful()) return $process->getOutput();
        else throw new \Exception(self::formatErrorOutput($process));
    }

It would be nice, if this logic could be provided by the Process component. I expect it to be a common usecase. But I see how integrating this might grow the Process class. It might make sense to think about splitting the definition of a Process from the strategy how a Process should be run. The current Process class already implements two strategies:

  • run the process synchronously and return the exit code, don't throw
  • run the process asynchrounously, don't throw
  1. The second thing my ProcessUtil class does is formatting an error string for the Exception:
    public static function formatErrorOutput(Process $process) {
        return sprintf("Error code %d while running command line:\n%s\nstderr:\n%s\nstdout:\n%s",
            $process->getExitCode(),
            $process->getCommandLine(),
            $process->getErrorOutput(),
            $process->getOutput()
        );
    }
@stof
Copy link
Member

stof commented Aug 29, 2014

This is exactly what $process->mustRun() is about (except it does not return the output but the Process object itsel)f.

@stof stof closed this as completed Aug 29, 2014
@stof
Copy link
Member

stof commented Aug 29, 2014

And for the formatting, we already have the ProcessFailedException which does a similar job (and is used in mustRun)

@xabbuh
Copy link
Member

xabbuh commented Aug 29, 2014

But we could indeed describe it somehow in the documentation to make it more popular (see symfony/symfony-docs#4187).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants