Improve error handling for hams.at API fetch

Adds robust error handling and user feedback for failures when fetching satellite data from the hams.at API. The controller now sets error states and messages for connection issues and invalid responses, and the view displays appropriate alerts to users.
这个提交包含在:
Peter Goodhall 2025-08-22 17:27:35 +01:00
父节点 c6413fc755
当前提交 a89b04966e
共有 2 个文件被更改,包括 54 次插入18 次删除

查看文件

@ -22,18 +22,49 @@ class Components extends CI_Controller {
$data['user_hamsat_key']='';
}
$url = 'https://hams.at/api/alerts/upcoming';
if ($data['user_hamsat_key'] ?? '' != '') {
$options = array(
'http' => array(
'method' => 'GET',
'header' => "Authorization: Bearer ".$data['user_hamsat_key']."\r\n"
)
);
$context = stream_context_create($options);
$json = file_get_contents($url, false, $context);
} else {
$json = file_get_contents($url);
// Initialize error state
$data['error'] = false;
$data['error_message'] = '';
try {
if ($data['user_hamsat_key'] ?? '' != '') {
$options = array(
'http' => array(
'method' => 'GET',
'header' => "Authorization: Bearer ".$data['user_hamsat_key']."\r\n",
'timeout' => 10 // Add timeout to prevent long hangs
)
);
$context = stream_context_create($options);
$json = @file_get_contents($url, false, $context);
} else {
$json = @file_get_contents($url, false, stream_context_create(array(
'http' => array('timeout' => 10)
)));
}
// Check if the request failed
if ($json === false) {
$data['error'] = true;
$data['error_message'] = 'Unable to connect to hams.at service. Please check your internet connection or try again later.';
$data['rovedata'] = null;
} else {
$decoded_data = json_decode($json, true);
if ($decoded_data === null && json_last_error() !== JSON_ERROR_NONE) {
$data['error'] = true;
$data['error_message'] = 'Invalid response received from hams.at service. Please try again later.';
$data['rovedata'] = null;
} else {
$data['rovedata'] = $decoded_data;
}
}
} catch (Exception $e) {
$data['error'] = true;
$data['error_message'] = 'An error occurred while fetching satellite data. Please try again later.';
$data['rovedata'] = null;
}
$hkey_opt=$this->user_options_model->get_options('hamsat',array('option_name'=>'hamsat_key','option_key'=>'workable'))->result();
if (count($hkey_opt)>0) {
$data['user_hamsat_workable_only'] = $hkey_opt[0]->option_value;
@ -42,7 +73,6 @@ class Components extends CI_Controller {
}
$this->load->model('stations');
$data['rovedata'] = json_decode($json, true);
$data['gridsquare'] = strtoupper($this->stations->find_gridsquare());
// load view

查看文件

@ -12,12 +12,19 @@
<div class="alert alert-warning" role="warning">
Private feed key empty. Please set the feed key in your profile.
</div>
<?php } elseif ($error) { ?>
<div class="alert alert-danger" role="alert">
<strong>Connection Error:</strong> <?php echo htmlspecialchars($error_message); ?>
</div>
<?php } elseif ($rovedata === null || !isset($rovedata['data'])) { ?>
<div class="alert alert-danger" role="alert">
<strong>Data Error:</strong> Unable to retrieve satellite data. Please try refreshing the page or check back later.
</div>
<?php } elseif ($rovedata['data'] == []) { ?>
<div class="alert alert-warning" role="warning">
<?php echo lang('hams_at_no_activations_found'); ?>
</div>
<?php } else { ?>
<?php if ($rovedata['data'] == []) { ?>
<div class="alert alert-warning" role="warning">
<?php echo lang('hams_at_no_activations_found'); ?>
</div>
<?php } else { ?>
<table class="table table-striped table-hover">
<thead>
<tr>
@ -152,6 +159,5 @@
<?php endforeach; ?>
</tbody>
</table>
<?php } ?>
<?php } ?>
</div>