Skip to content

Commit 1d3e8bc

Browse files
committed
VMChecker API must throw on curl failure
1 parent a1b773b commit 1d3e8bc

File tree

4 files changed

+91
-20
lines changed

4 files changed

+91
-20
lines changed

block_vmchecker.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
require_once(__DIR__ . '/classes/form/block_form.php');
2828
require_once(__DIR__ . '/classes/form/submit_form.php');
29+
require_once(__DIR__ . '/classes/backend/api.php');
30+
require_once(__DIR__ . '/classes/exceptions/api_exception.php');
2931
require_once($CFG->dirroot . '/mod/assign/locallib.php');
3032

3133
/**
@@ -56,15 +58,7 @@ public function has_config() {
5658
* Set thte tile of the block.
5759
* @return void
5860
*/
59-
private function set_title() {
60-
if (!$this->config->assignment) {
61-
return;
62-
}
63-
64-
$cm = get_coursemodule_from_instance('assign', $this->config->assignment, 0, false, MUST_EXIST);
65-
$context = \context_module::instance($cm->id);
66-
67-
$assign = new \assign($context, null, null);
61+
private function set_title(assign $assign) {
6862
$this->title = get_string('vmchecker', 'block_vmchecker') . ' - ' . $assign->get_default_instance()->name;
6963
}
7064

@@ -190,7 +184,13 @@ public function get_content() {
190184
}
191185

192186
try {
193-
$cm = get_coursemodule_from_instance('assign', $this->config->assignment, 0, false, MUST_EXIST);
187+
$course_info = get_fast_modinfo($this->page->course->id);
188+
foreach ($course_info->instances['assign'] as $instance) {
189+
if ($instance->id == $this->config->assignment) {
190+
$cm = $instance;
191+
break;
192+
}
193+
}
194194
} catch (dml_missing_record_exception | dml_multiple_records_exception $e) {
195195
$this->content->text = get_string('no_assignment_selected', 'block_vmchecker');
196196
return $this->content;
@@ -199,7 +199,7 @@ public function get_content() {
199199

200200
$assign = new \assign($context, null, null);
201201

202-
$this->set_title();
202+
$this->set_title($assign);
203203
$backendurl = get_config('block_vmchecker', 'backend');
204204
$api = new \block_vmchecker\backend\api($backendurl);
205205

classes/backend/api.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace 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() {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Definition of vmchecker block entrypoint.
19+
*
20+
* @package block_vmchecker
21+
* @copyright 2022 Mihai Baruta <baruta.mihai99@gmail.com>
22+
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3 or later
23+
*/
24+
25+
namespace block_vmchecker\exceptions;
26+
27+
defined('MOODLE_INTERNAL') || die();
28+
29+
class api_exception extends \moodle_exception {
30+
31+
/**
32+
* Constructor for the API exception.
33+
*
34+
* @param string $message The error message.
35+
* @param string $module The module name, default is 'block_vmchecker'.
36+
* @param mixed $extra_data Additional data to be passed with the exception.
37+
* @param string|null $debuginfo Debug information, if any.
38+
*/
39+
public function __construct($message, $module = 'block_vmchecker', $extra_data = null, $debuginfo = null) {
40+
parent::__construct($message, $module, null, $extra_data, $debuginfo);
41+
}
42+
}

edit_form.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ protected function specific_definition($mform) {
6565
$mform->settype('config_gitlab_branch', PARAM_TEXT);
6666
$mform->setDefault('config_gitlab_branch', 'main');
6767

68-
$courseactivities = get_array_of_activities($this->page->course->id);
68+
$courseactivities = get_fast_modinfo($this->page->course->id)->get_cms();
6969
$assignments = array();
7070
foreach ($courseactivities as $activity) {
71-
if ($activity->mod != "assign") {
71+
if ($activity->modname != "assign") {
7272
continue;
7373
}
7474

0 commit comments

Comments
 (0)