Skip to content

Commit

Permalink
Better exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed May 14, 2024
1 parent 8b74f51 commit e48f0a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
18 changes: 12 additions & 6 deletions classes/ai/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ai {
* @param string $model
*/
public function __construct($model = null) {
$this->model = $model ?? get_config('tool_aiconnect', 'model');
$this->model = $model ?? trim(explode(',',get_config('tool_aiconnect','model'))[0]);
$this->openaiapikey = get_config('tool_aiconnect', 'apikey');
$this->temperature = get_config('tool_aiconnect', 'temperature');
$this->endpoint = trim(get_config('tool_aiconnect', 'endpoint'));
Expand Down Expand Up @@ -99,16 +99,22 @@ private function make_request($data, $apikey, $multipart = null) {
"CURLOPT_HTTPHEADER" => $headers,
];
$start = microtime(true);
$jsonresponse = $curl->post($this->endpoint, json_encode($data), $options);
$response = json_decode($jsonresponse,true);
if ($response == null) {
return ['curl_error' => $curl->get_info()->http_code, 'execution_time' => $executiontime];
}
xdebug_break();

$response = $curl->post($this->endpoint, json_encode($data), $options);
if(isset($response['error'])){
throw new moodle_exception('endpointerror', 'tool_aiconnect', '', null,
$response['error']['message']);
}

$end = microtime(true);
$executiontime = round($end - $start, 2);

if (json_decode($response) == null) {
return ['curl_error' => $response, 'execution_time' => $executiontime];
}
return ['response' => json_decode($response, true), 'execution_time' => $executiontime];
return ['response' => $response, 'execution_time' => $executiontime];
}

/**
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_aiconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@


$string['misssingmodelerror'] = 'No model has been provided for the call';
$string['endpointerror'] = 'Endpoint error';
$string['pluginname'] = 'AI Connect tool';

$string['endpoint'] = 'Endpoint';
Expand Down

0 comments on commit e48f0a7

Please sign in to comment.