~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to libphutil/src/future/exec/CommandException.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Exception thrown when a system command fails.
 
5
 */
 
6
final class CommandException extends Exception {
 
7
 
 
8
  protected $command;
 
9
  protected $stdout;
 
10
  protected $stderr;
 
11
  protected $error;
 
12
 
 
13
  public function __construct($message, $command, $error, $stdout, $stderr) {
 
14
    $this->command = $command;
 
15
    $this->error = $error;
 
16
    $this->stdout = $stdout;
 
17
    $this->stderr = $stderr;
 
18
 
 
19
    $summary = array();
 
20
    $summary[] = $this->summarize($message);
 
21
 
 
22
    $summary[] = 'COMMAND';
 
23
    $summary[] = $this->summarize($command);
 
24
 
 
25
    $summary[] = null;
 
26
    $summary[] = 'STDOUT';
 
27
    $summary[] = $this->summarize($stdout);
 
28
 
 
29
    $summary[] = null;
 
30
    $summary[] = 'STDERR';
 
31
    $summary[] = $this->summarize($stderr);
 
32
 
 
33
    $summary = implode("\n", $summary);
 
34
 
 
35
    parent::__construct($summary);
 
36
  }
 
37
 
 
38
  public function getCommand() {
 
39
    return $this->command;
 
40
  }
 
41
 
 
42
  public function getError() {
 
43
    return $this->error;
 
44
  }
 
45
 
 
46
  public function getStdout() {
 
47
    return $this->stdout;
 
48
  }
 
49
 
 
50
  public function getStderr() {
 
51
    return $this->stderr;
 
52
  }
 
53
 
 
54
  private function summarize($string) {
 
55
    if (!strlen($string)) {
 
56
      return '(empty)';
 
57
    }
 
58
 
 
59
    $limit = 1000;
 
60
 
 
61
    $len = strlen($string);
 
62
    if ($len > $limit) {
 
63
      $cut = $len - $limit;
 
64
      $suffix = '... ('.number_format($cut).' more bytes) ...';
 
65
      if ($cut > strlen($suffix)) {
 
66
        $string = substr($string, 0, $limit).$suffix;
 
67
      }
 
68
    }
 
69
 
 
70
    // Strip out any credentials for the purpose of building a human readable
 
71
    // summary of the exception, since these are rarely-if-ever useful when
 
72
    // debugging, but can expose otherwise sensitive information.
 
73
    $string = phutil_censor_credentials($string);
 
74
 
 
75
    return $string;
 
76
  }
 
77
 
 
78
}