Created
April 23, 2022 13:25
-
-
Save anatoliy-t7/3c2adbcb3efd8a5cd81ff0d8b64fff33 to your computer and use it in GitHub Desktop.
ErrorCatcher for JavaScript (Laravel, Livewire)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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