Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 1638ccd5 authored by Francois Gannaz's avatar Francois Gannaz
Browse files

better curl errors handling

parent cc2c8001
......@@ -11,10 +11,14 @@ require_once dirname(dirname(__DIR__)) . '/vendor/firebase/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
/**
* Fetch info from the LabNbook API for the current user.
* Fetch info from the LabNbook API.
*
* This class does not depend on the environment (Moodle user, config, role, etc).
*/
class fetcher
{
const TIMEOUT = 2; // seconds to wait for LabNbook connection
/**
* @var string Root URL to the API
*/
......@@ -31,7 +35,7 @@ class fetcher
private $environment;
/**
* @var array { code, message }, e.g. { code: 403, message: "not auth" }
* @var \Stdclass { code, message }, e.g. { code: 403, message: "not auth" }
*/
private $lastError;
......@@ -48,7 +52,7 @@ class fetcher
* @return \Stdclass {code, message}
*/
public function getLastError() : ?\Stdclass {
return $this->lastError;
return (object) $this->lastError;
}
/**
......@@ -70,8 +74,15 @@ class fetcher
'Accept: application/json',
'Authorization: Bearer ' . $this->encodeJwtToken($urlPath, $payload),
],
CURLOPT_CONNECTTIMEOUT => self::TIMEOUT,
]);
$response = curl_exec($curl);
if (curl_errno($curl) !== 0) {
$this->lastError = (object) [
'code' => 0,
'message' => curl_error($curl),
];
}
$error = $this->validateResponse($curl, $response);
if ($error) {
curl_close($curl);
......@@ -138,7 +149,7 @@ EOJS;
*/
protected function validateResponse($curl, $response) {
if ($response === false) {
$this->lastError = ['message' => "Network error? " . curl_error($curl)];
$this->lastError = (object) ['message' => "Network error? " . curl_error($curl)];
return "Network error? " . curl_error($curl);
}
$responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment