From 17c4ebe5f669ac37f103c27344083136018821e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Wed, 24 Apr 2019 08:11:51 +0200 Subject: [PATCH] Better handling of error with git forges APIs Note to self: Gitlab deprecated APIv3, had to update the link to v4 --- index.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index d9ef115..2ed1010 100644 --- a/index.php +++ b/index.php @@ -348,7 +348,8 @@ function updateGitKeys($api, $keys) function apiRequest($api, $route, $meth = 'GET', $data = null) { $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $api['api'].'/'.$route); + $url = $api['api'].'/'.$route; + curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $meth); curl_setopt($ch, CURLOPT_USERAGENT, 'Machines Frogeye'); if ($data) { @@ -366,7 +367,12 @@ function updateGitKeys($api, $keys) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $raw = curl_exec($ch); curl_close($ch); - return json_decode($raw); + $json = json_decode($raw); + if ($json !== null && isset($json->{"error"})) { + echo("Error with API ".$url.": ".$json->{"error"}."\n"); + return null; + } + return $json; } global $SSH_KEY_REGEX; @@ -403,7 +409,6 @@ function updateGitKeys($api, $keys) } foreach ($toDelete as $id => $ekey) { - echo "387 deleting $id $ekey\n"; apiRequest($api, 'user/keys/'.$id, 'DELETE'); } } @@ -657,7 +662,7 @@ case 'config': foreach ($machines as $dMachineName => $dMachine) { if ($network['secure'] == 'true') { - + } }