1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 18:04:08 +02:00

better upload error handling

This commit is contained in:
Robin Appelman 2017-06-02 23:20:44 +02:00
commit af6be81db6
3 changed files with 28 additions and 8 deletions

View file

@ -3,6 +3,7 @@
namespace Demostf\API\Demo;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
/**
* Wrapper around demo.js parser
@ -18,10 +19,22 @@ class RawParser {
}
public function parse(string $path): ?array {
$client = new Client();
$response = $client->post($this->parserUrl, [
'body' => fopen($path, 'r')
]);
return json_decode($response->getBody()->getContents(), true);
try {
$client = new Client();
$response = $client->post($this->parserUrl, [
'body' => fopen($path, 'r')
]);
$result = json_decode($response->getBody()->getContents(), true);
if (is_null($result)) {
throw new \Exception('Failed to parse demo, unexpected result from parser');
} else {
return $result;
}
} catch (GuzzleException $e) {
if (strpos($e->getMessage(), 'cURL error 52') !== false) {
throw new \Exception('Failed to parse demo, can\'t reach demo parser');
}
throw new \Exception('Failed to parse demo, ' . $e->getMessage());
}
}
}