Skip to content

Instantly share code, notes, and snippets.

@anatoliy-t7
Created April 23, 2022 13:25
Show Gist options
  • Save anatoliy-t7/3c2adbcb3efd8a5cd81ff0d8b64fff33 to your computer and use it in GitHub Desktop.
Save anatoliy-t7/3c2adbcb3efd8a5cd81ff0d8b64fff33 to your computer and use it in GitHub Desktop.
ErrorCatcher for JavaScript (Laravel, Livewire)
<div>
<script>
var hasLoggedOnce = '';
window.onerror = function(msg, source, lineNo, columnNo, error) {
if (hasLoggedOnce !== msg) {
Livewire.emit("getError", msg, source, lineNo, columnNo, error, window.location.href);
}
hasLoggedOnce = msg;
}
</script>
</div>
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Log;
class ErrorCatcher extends Component
{
protected $listeners = ['getError'];
public function getError($msg, $source, $lineNo, $columnNo, $error, $url)
{
Log::channel('stackandtelegram')->error('javascript: '.$msg.PHP_EOL.'source: '.$source.PHP_EOL.'lineNo: '.$lineNo.PHP_EOL.'columnNo: '.$columnNo.PHP_EOL.'error: '.json_encode($error).'URL: '.$url);
}
public function render()
{
return view('livewire.error-catcher');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment