2424
2525namespace block_vmchecker \backend ;
2626
27+ use \block_vmchecker \exceptions \api_exception ;
2728
2829/**
2930 * Definition of the backend API for VMChecker Next.
@@ -90,11 +91,16 @@ private function clean_url(string $url) {
9091 * @param string $endpoint
9192 * @param array $queryparams
9293 * @param array $payload
94+ * @throws \block_vmchecker\exceptions\api_exception
9395 * @return object
9496 */
95- private function query_service (string $ endpoint , ?array $ queryparams , ?array $ payload ) {
97+ private function query_service (string $ endpoint , ?array $ queryparams , ?array $ payload ): array {
9698 $ curl = new \curl ();
97- $ curl ->setopt (array ('CURLOPT_TIMEOUT ' => 5 , 'CURLOPT_CONNECTTIMEOUT ' => 2 ));
99+ $ curl ->setopt (
100+ array (
101+ 'CURLOPT_TIMEOUT ' => 10 ,
102+ 'CURLOPT_CONNECTTIMEOUT ' => 5 )
103+ );
98104
99105 $ fullurl = $ this ->apiurl . $ endpoint ;
100106 $ cleanurl = $ this ->clean_url ($ fullurl ); // Reduce multiple / to one 'http://aa///b' -> 'http://a/b'.
@@ -113,15 +119,31 @@ private function query_service(string $endpoint, ?array $queryparams, ?array $pa
113119 $ info = $ curl ->get_info ();
114120 if ($ curlerrno = $ curl ->get_errno ()) {
115121 // CURL connection error.
116- debugging ("Unexpected response from the backend server, CURL error number: $ curlerrno " );
117- return array ();
122+ throw new api_exception (
123+ 'vmchecker_backend_api_error ' ,
124+ 'block_vmchecker ' ,
125+ array ('error ' => "Unexpected cURL error! " ),
126+ 'cURL error number: ' . $ curlerrno
127+ );
118128 } else if ($ info ['http_code ' ] != 200 ) {
119- // Unexpected error from server.
120- debugging ('Unexpected response from the backend server, HTTP code: ' . $ info ['httpcode ' ]);
121- return array ();
129+ throw new api_exception (
130+ 'vmchecker_backend_api_error ' ,
131+ 'block_vmchecker ' ,
132+ array ('error ' => "Unexpected response from the backend server! " ),
133+ 'HTTP error code: ' . $ info ['http_code ' ]
134+ );
122135 }
123136
124137 $ response = json_decode ($ rawresponse , true );
138+ if (!is_array ($ response )) {
139+ throw new api_exception (
140+ 'vmchecker_backend_api_error ' ,
141+ 'block_vmchecker ' ,
142+ array ('error ' => "Invalid response format from the backend server! " ),
143+ 'Response is not an array '
144+ );
145+ }
146+
125147 return $ response ;
126148 }
127149
@@ -133,6 +155,7 @@ private function query_service(string $endpoint, ?array $queryparams, ?array $pa
133155 * moodle_username?: string,
134156 * count?: int
135157 * order?: str, “asc” | “desc” - by id; default: desc
158+ * @throws \block_vmchecker\exceptions\api_exception
136159 * @return array
137160 */
138161 public function info (array $ queryparams ) {
@@ -147,6 +170,7 @@ public function info(array $queryparams) {
147170 * gitlab_branch: string
148171 * username: string
149172 * archive: string, archive content - base64 encoded.
173+ * @throws \block_vmchecker\exceptions\api_exception
150174 * @return array
151175 */
152176 public function submit (array $ payload ) {
@@ -159,6 +183,7 @@ public function submit(array $payload) {
159183 * gitlab_private_token: string
160184 * gitlab_project_id: int
161185 * gitlab_branch: string
186+ * @throws \block_vmchecker\exceptions\api_exception
162187 * @return array
163188 */
164189 public function archive (array $ payload ) {
@@ -168,6 +193,7 @@ public function archive(array $payload) {
168193 /**
169194 * Get status endpoint.
170195 * @param string $uuid UUID of the task to check.
196+ * @throws \block_vmchecker\exceptions\api_exception
171197 * @return object
172198 */
173199 public function status (string $ uuid ) {
@@ -177,6 +203,7 @@ public function status(string $uuid) {
177203 /**
178204 * Get trace endpoint.
179205 * @param string $uuid UUID of the task to check.
206+ * @throws \block_vmchecker\exceptions\api_exception
180207 * @return object
181208 */
182209 public function trace (string $ uuid ) {
@@ -186,6 +213,7 @@ public function trace(string $uuid) {
186213 /**
187214 * Cancel submission endpoint
188215 * @param string $uuid UUID of the task to cancel.
216+ * @throws \block_vmchecker\exceptions\api_exception
189217 * @return object
190218 */
191219 public function cancel (string $ uuid ) {
@@ -194,6 +222,7 @@ public function cancel(string $uuid) {
194222
195223 /**
196224 * Checks if the API endpoint is alive.
225+ * @throws \block_vmchecker\exceptions\api_exception
197226 * @return bool
198227 */
199228 public function healthcheck () {
0 commit comments