Skip to content

Commit

Permalink
add:configuration Discord notifier and test route in web.php
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-velasquez committed Nov 28, 2024
1 parent a0c37e0 commit ac505bf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
44 changes: 28 additions & 16 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@

class Handler extends ExceptionHandler
{
/**
* Lista de excepciones que no serán reportadas.
*
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
\Illuminate\Validation\ValidationException::class,
\Illuminate\Session\TokenMismatchException::class,
];

/**
* Reporta las excepciones.
*
* @param \Throwable $exception
* @return void
* @throws \Throwable
*/
public function report(Throwable $exception)
{
parent::report($exception);

// Reporta la excepción a Discord si debe reportarse
// Notifica a Discord si no está en la lista de exclusión
if ($this->shouldReport($exception)) {
DiscordNotifier::notifyException($exception);
try {
$this->notifyDiscord($exception);
} catch (Throwable $e) {
// Evitar que fallos en la notificación rompan el flujo
logger()->error("Error al enviar notificación a Discord: " . $e->getMessage());
}
}

parent::report($exception);
}

protected function notifyDiscord(Throwable $exception)
{
$details = [
'message' => $exception->getMessage(),
'file' => "{$exception->getFile()}:{$exception->getLine()}",
'trace' => substr($exception->getTraceAsString(), 0, 1800),
];

// Logea el error antes de enviarlo a Discord
logger()->error('Excepción capturada', $details);

// Envía la notificación a Discord
DiscordNotifier::notifyEvent(
'Excepción en el sistema 🚨',
$details,
asset('images/logo.png')
);
}
}
30 changes: 30 additions & 0 deletions app/Http/Middleware/HandleExceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Throwable;
use App\Helpers\DiscordNotifier;

class HandleExceptions
{
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (Throwable $e) {
$this->reportToDiscord($e); // Enviar notificación a Discord
throw $e; // Rethrow para permitir que el sistema maneje la excepción
}
}

protected function reportToDiscord(Throwable $exception)
{
$message = "**🚨 Excepción Crítica**\n"
. "**Mensaje:** {$exception->getMessage()}\n"
. "**Archivo:** {$exception->getFile()}:{$exception->getLine()}\n"
. "**Trace:** ```" . substr($exception->getTraceAsString(), 0, 1800) . "```";

DiscordNotifier::send($message);
}
}
1 change: 1 addition & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
$middleware->web(append: [
\App\Http\Middleware\HandleInertiaRequests::class,
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
\App\Http\Middleware\HandleExceptions::class,
]);

// Registra los alias de los middlewares de Spatie
Expand Down
15 changes: 0 additions & 15 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,10 @@
'phpVersion' => PHP_VERSION,
]);
});
Route::get('/test-exception', function () {
throw new Exception("Esto es una prueba de excepción para Discord.");
});

Route::get('/test-discord', function () {
\App\Helpers\DiscordNotifier::send("Prueba directa de notificación a Discord.");
return "Mensaje enviado.";
});

Route::get('/test-missing-user', function () {
// Lanzará ModelNotFoundException
return User::findOrFail(99999);
});

Route::get('/test-sql-error', function () {
// Lanzará QueryException
return DB::table('non_existent_table')->get();
});

// Grupo de rutas autenticadas
Route::middleware(['auth:sanctum', config('jetstream.auth_session')])->group(function () {
Route::get('/dashboard', function () {
Expand Down

0 comments on commit ac505bf

Please sign in to comment.