Update script error handling

finally found the correct exception that captures a connect error when a
source url cannot be loaded, so it logs the error and just keep
executing the script.

Now automation can be enabled since the system knows how to handle
errors. Fuck. Yes.
This commit is contained in:
ro 2024-02-18 22:18:56 -06:00
parent 7abd6d44a0
commit 3edd6e5521

View file

@ -7,6 +7,7 @@ use App\Repositories\LocationRepository;
use App\Models\Source; use App\Models\Source;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Carbon\Carbon; use Carbon\Carbon;
use GuzzleHttp\Exception\ConnectException;
class UpdateService class UpdateService
{ {
@ -26,18 +27,27 @@ class UpdateService
$checked = []; $checked = [];
//checks source url to make sure they valid //checks source url to make sure they valid
foreach ($sources as $source) { foreach ($sources as $source) {
if ($this->urlExists('https://' . $source->url)) { $result = [];
$result = []; if ($source['type'] == 'mastodon') {
if ($source['type'] == 'mastodon') { if ($source['token'] == null) {
if ($source['token'] == null) { try {
$result = \Mastodon::domain('https://' . $source['url']) $result = \Mastodon::domain('https://' . $source['url'])
->get('/instance/domain_blocks'); ->get('/instance/domain_blocks');
} else { array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
array_push($missing, ['source' => $source->url]);
}
} else {
try {
$result = \Mastodon::domain('https://' . $source['url']) $result = \Mastodon::domain('https://' . $source['url'])
->token($source['token']) ->token($source['token'])
->get('/instance/domain_blocks'); ->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
} }
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') { }
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') {
try {
$denylist = array_map('str_getcsv', file('https://' . $source['url'])); $denylist = array_map('str_getcsv', file('https://' . $source['url']));
foreach ($denylist as $item) { foreach ($denylist as $item) {
array_push($result, [ array_push($result, [
@ -45,11 +55,12 @@ class UpdateService
'severity' => $item[1], 'severity' => $item[1],
'comment' => $item[2]]); 'comment' => $item[2]]);
} }
array_push($checked, ['source' => $source->url]);
} catch (Exception $e) {
array_push($missing, ['source' => $source->url]);
} }
array_push($checked, ['source' => $source->url]); }
} else {
array_push($missing, ['source' => $source->url]);
};
$source->list_data = json_encode($result); $source->list_data = json_encode($result);
$source->last_updated = Carbon::now(); $source->last_updated = Carbon::now();
$source->save(); $source->save();