Skip to content

Commit 818a640

Browse files
committed
feat(error-output): Improve the error messages
1 parent c3386f2 commit 818a640

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

app/Commands/PrefixCommand.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,42 @@ public function handle()
103103
$deleteBuild = (bool) $this->option('delete-build');
104104

105105
$processor = new Processor($personalAccessToken);
106-
$build = $processor->run($sourceDirectory, $targetDirectory, $projectId, $githubAccessToken);
106+
107+
try {
108+
$build = $processor->run($sourceDirectory, $targetDirectory, $projectId, $githubAccessToken);
109+
} catch (\GuzzleHttp\Exception\ClientException $clientException) {
110+
if (!$clientException->hasResponse()) {
111+
$this->error('PHP-Prefixer: '.$clientException->getMessage());
112+
113+
return 1;
114+
}
115+
116+
$response = $clientException->getResponse();
117+
$this->error('PHP-Prefixer: '.$response->getReasonPhrase());
118+
119+
$body = json_decode($response->getBody());
120+
121+
if (!$body) {
122+
return 1;
123+
}
124+
125+
if (isset($body->message)) {
126+
$this->error('PHP-Prefixer: '.$body->message);
127+
}
128+
129+
if (isset($body->errors->uploaded_source_file)) {
130+
$this->error('PHP-Prefixer: '.
131+
implode(' ', $body->errors->uploaded_source_file));
132+
}
133+
134+
return 1;
135+
}
107136

108137
if ($deleteBuild) {
109138
$processor->deleteBuild($projectId, $build->id);
110139
}
111140

112-
return $this->renderOutput($build->state, $start);
141+
return $this->renderOutput($build, $start);
113142
}
114143

115144
protected function initialize(InputInterface $input, OutputInterface $output)
@@ -140,11 +169,13 @@ private function optionOrEnv($input, $key)
140169
}
141170
}
142171

143-
private function renderOutput($state, $start)
172+
private function renderOutput($build, $start)
144173
{
145174
$processingTime = round(microtime(true) - $start, 2);
146175
$formattedProcessingTime = ' -- Processing time: '.number_format($processingTime).' seconds';
147176

177+
$state = $build->state;
178+
148179
switch ($state) {
149180
case 'success':
150181
$this->info('PHP-Prefixer: project prefixing completed');
@@ -154,12 +185,22 @@ private function renderOutput($state, $start)
154185
return 0;
155186
case 'cancelled':
156187
$this->error('PHP-Prefixer: project prefixing cancelled');
188+
189+
if (isset($build->state_message)) {
190+
$this->error('PHP-Prefixer: '.$build->state_message);
191+
}
192+
157193
$this->notify('PHP-Prefixer CLI', 'Project prefixing cancelled');
158194
$this->info($formattedProcessingTime);
159195

160196
return 1;
161197
case 'failed':
162198
$this->error('PHP-Prefixer: project prefixing failed');
199+
200+
if (isset($build->state_message)) {
201+
$this->error('PHP-Prefixer: '.$build->state_message);
202+
}
203+
163204
$this->notify('PHP-Prefixer CLI', 'Project prefixing failed');
164205
$this->info($formattedProcessingTime);
165206

0 commit comments

Comments
 (0)