fix: pass http errors while polling to the UI

This commit is contained in:
Benjamin Nussbaum 2025-07-03 00:03:46 +02:00
parent e326ded933
commit b438457d32
2 changed files with 13 additions and 35 deletions

View file

@ -43,7 +43,7 @@ class Plugin extends Model
public function updateDataPayload(): void public function updateDataPayload(): void
{ {
if ($this->data_strategy === 'polling' && $this->polling_url) { if ($this->data_strategy === 'polling' && $this->polling_url) {
// Parse headers from polling_header string
$headers = ['User-Agent' => 'usetrmnl/byos_laravel', 'Accept' => 'application/json']; $headers = ['User-Agent' => 'usetrmnl/byos_laravel', 'Accept' => 'application/json'];
if ($this->polling_header) { if ($this->polling_header) {
@ -58,7 +58,6 @@ class Plugin extends Model
$httpRequest = Http::withHeaders($headers); $httpRequest = Http::withHeaders($headers);
// Add body for POST requests if polling_body is provided
if ($this->polling_verb === 'post' && $this->polling_body) { if ($this->polling_verb === 'post' && $this->polling_body) {
$httpRequest = $httpRequest->withBody($this->polling_body); $httpRequest = $httpRequest->withBody($this->polling_body);
} }

View file

@ -103,43 +103,18 @@ new class extends Component {
$this->plugin->update($validated); $this->plugin->update($validated);
} }
public function updateData() public function updateData(): void
{ {
if ($this->plugin->data_strategy === 'polling') { if ($this->plugin->data_strategy === 'polling') {
// Parse headers from polling_header string try {
$headers = ['User-Agent' => 'usetrmnl/byos_laravel', 'Accept' => 'application/json']; $this->plugin->updateDataPayload();
if ($this->plugin->polling_header) { $this->data_payload = json_encode($this->plugin->data_payload, JSON_PRETTY_PRINT);
$headerLines = explode("\n", trim($this->plugin->polling_header)); $this->data_payload_updated_at = $this->plugin->data_payload_updated_at;
foreach ($headerLines as $line) {
$parts = explode(':', $line, 2);
if (count($parts) === 2) {
$headers[trim($parts[0])] = trim($parts[1]);
}
}
}
// Prepare the HTTP request } catch (\Exception $e) {
$httpRequest = Http::withHeaders($headers); $this->dispatch('data-update-error', message: $e->getMessage());
// Add body for POST requests if polling_body is provided
if ($this->plugin->polling_verb === 'post' && $this->plugin->polling_body) {
$httpRequest = $httpRequest->withBody($this->plugin->polling_body, 'application/json');
} }
// Make the request based on the verb
if ($this->plugin->polling_verb === 'post') {
$response = $httpRequest->post($this->plugin->polling_url)->json();
} else {
$response = $httpRequest->get($this->plugin->polling_url)->json();
}
$this->plugin->update([
'data_payload' => $response,
'data_payload_updated_at' => now()
]);
$this->data_payload = json_encode($response, JSON_PRETTY_PRINT);
$this->data_payload_updated_at = now();
} }
} }
@ -626,5 +601,9 @@ HTML;
$wire.on('preview-error', ({message}) => { $wire.on('preview-error', ({message}) => {
alert('Preview Error: ' + message); alert('Preview Error: ' + message);
}); });
$wire.on('data-update-error', ({message}) => {
alert('Data Update Error: ' + message);
});
</script> </script>
@endscript @endscript