ADD error headling.

This commit is contained in:
E98Developer 2026-03-29 07:36:57 +02:00
parent eb88812d89
commit e3ecda2f3e
7 changed files with 122 additions and 19 deletions

View File

@ -3,6 +3,8 @@
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;
use Throwable;
class Handler extends ExceptionHandler
@ -44,5 +46,47 @@ public function register(): void
$this->reportable(function (Throwable $e) {
//
});
// AJAX kérések esetén mindig JSON választ adunk, egyedi referencia kóddal
$this->renderable(function (Throwable $e, $request) {
if ($request->ajax() || $request->wantsJson()) {
return $this->handleAjaxException($e, $request);
}
});
}
private function handleAjaxException(Throwable $e, $request): JsonResponse
{
// Validációs hibákat nem piszkáljuk azokat a Laravel alapból kezeli
if ($e instanceof \Illuminate\Validation\ValidationException) {
return parent::render($request, $e);
}
// Egyedi referencia kód generálása
$errorRef = 'ERR-' . now()->format('ymdHis') . '-' . strtoupper(Str::random(6));
// Teljes technikai részlet a logba
\Log::error("[$errorRef] Unhandled exception", [
'error_ref' => $errorRef,
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'url' => $request->fullUrl(),
'method' => $request->method(),
'user_id' => auth()->id(),
'input' => $request->except($this->dontFlash),
'trace' => $e->getTraceAsString(),
]);
$statusCode = method_exists($e, 'getStatusCode')
? $e->getStatusCode()
: 500;
return response()->json([
'success' => false,
'error_ref' => $errorRef,
'message' => "Váratlan hiba történt. Hivatkozási kód: $errorRef",
], $statusCode);
}
}

View File

@ -3,6 +3,9 @@
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
@ -14,5 +17,42 @@
//
})
->withExceptions(function (Exceptions $exceptions): void {
//
// AJAX kérések esetén mindig JSON választ adunk, egyedi referencia kóddal
$exceptions->renderable(function (Throwable $e, Request $request) {
if ($request->ajax() || $request->wantsJson()) {
// Validációs hibákat nem piszkáljuk azokat a Laravel alapból kezeli
if ($e instanceof ValidationException) {
return null;
}
// Egyedi referencia kód generálása
$errorRef = 'ERR-' . now()->format('ymdHis') . '-' . strtoupper(Str::random(6));
// Teljes technikai részlet a logba
\Log::error("[$errorRef] Unhandled exception", [
'error_ref' => $errorRef,
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'url' => $request->fullUrl(),
'method' => $request->method(),
'user_id' => auth()->id(),
'input' => $request->except(['current_password', 'password', 'password_confirmation']),
'trace' => $e->getTraceAsString(),
]);
$statusCode = method_exists($e, 'getStatusCode')
? $e->getStatusCode()
: 500;
return response()->json([
'success' => false,
'error_ref' => $errorRef,
'message' => "Váratlan hiba történt. Hivatkozási kód: $errorRef",
], $statusCode);
}
return null;
});
})->create();

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"/js/app.js": "/js/app.js?id=b2b26ba4c8485e78de1ad23b8c72997d",
"/js/admin.js": "/js/admin.js?id=a3f796dee735ea8029b653e812781e63",
"/js/app.js": "/js/app.js?id=e8e72c61f1eed7ccaa5bd55f8edc43da",
"/js/admin.js": "/js/admin.js?id=2324b5d3c13edd33c62f910a1d8ef0c2",
"/js/module.js": "/js/module.js?id=9baa3aeb550477277796c6ebeff868c4",
"/js/components.js": "/js/components.js?id=34668cee8ac371c98657beffcff579b8",
"/css/app.css": "/css/app.css?id=11474616a7ef31af1acf43a25d65f4b6",

View File

@ -177,7 +177,7 @@ window.SupplierAdmin=function (options,SupplierItem) {
//Opts.onAfterInitViewDetails=root.autofillDetails;
Opts.dataTable.orderDirective=[0, 'desc'];
Toast.create("Sikeres rögzítés!", ajaxData.get('name')+" nevű beszálllító elmentve", TOAST_STATUS.SUCCESS,5000);
Toast.create({ title: "Sikeres rögzítés!", message: ajaxData.get('name')+" nevű beszálllító elmentve", status: TOAST_STATUS.SUCCESS, timeout: 5000 });
APP.loadMainContent(Opts.URL.index+'?init=0',root.initViewList);
}
}
@ -775,7 +775,7 @@ window.SupplierAdmin=function (options,SupplierItem) {
});
root.Supplier.contact=newContacts;
root.resetContactTable();
Toast.create("Kapcsolat adatai módosítva!", contactData.name+" nevű kapocsolattartó módosítva", TOAST_STATUS.SUCCESS,5000);
Toast.create({ title: "Kapcsolat adatai módosítva!", message: contactData.name+" nevű kapcsolattartó módosítva", status: TOAST_STATUS.SUCCESS, timeout: 5000 });
}
this.addContact=function (contactData){
/*
@ -788,7 +788,7 @@ window.SupplierAdmin=function (options,SupplierItem) {
$('.contactTable tbody').append(render);
root.bindContactClickAction();
Toast.enableTimers(false);
Toast.create("Kapcsolat tartó hozzáadva!", contactData.name+" nevű kapocsolattartó hozzáadva", TOAST_STATUS.SUCCESS,5000);
Toast.create({ title: "Kapcsolattartó hozzáadva!", message: contactData.name+" nevű kapcsolattartó hozzáadva", status: TOAST_STATUS.SUCCESS, timeout: 5000 });
}

View File

@ -185,6 +185,14 @@ window.ajaxErrorHandler= function (xhr, ajaxOptions, thrownError){
console.error(xhr);
console.error(ajaxOptions);
console.error(thrownError);
// Biztonsági ellenőrzés ha nincs responseJSON (pl. HTML válasz)
if (!xhr.responseJSON) {
ajaxErrorFieldHandler({'detail': 'Váratlan szerverhiba. Kérjük próbálja újra később.'});
return;
}
// Validációs hibák (422) ezeket részletesen megjelenítjük
if (xhr.responseJSON.errors) {
console.error("d2---------------");
if("jsonapi" in xhr.responseJSON){
@ -192,18 +200,29 @@ window.ajaxErrorHandler= function (xhr, ajaxOptions, thrownError){
}else{
ajaxErrorFieldHandler(xhr.responseJSON.errors);
}
}else{
if(xhr.status==404){
console.error("d3---------------");
ajaxErrorFieldHandler({'detail':'Nincs ilyen elem.'});
}else{
console.error("d4---------------");
ajaxErrorFieldHandler({'detail':'Általános hiba.'});
}
}
// Egyedi referencia kódos hiba (500, stb.)
else if (xhr.responseJSON.error_ref) {
console.error("error_ref: " + xhr.responseJSON.error_ref);
Toast.create({
title: "Szerverhiba",
message: "Váratlan hiba történt a feldolgozás során.<br>" +
"<small>Hivatkozási kód: <strong>" + xhr.responseJSON.error_ref + "</strong></small><br>" +
"<small>Kérjük, ezt a kódot adja meg a hibajelentésben.</small>",
status: TOAST_STATUS.DANGER,
timeout: 0
});
}
// 404
else if (xhr.status == 404) {
console.error("d3---------------");
ajaxErrorFieldHandler({'detail':'Nincs ilyen elem.'});
}
// Egyéb ismeretlen hiba
else {
console.error("d4---------------");
ajaxErrorFieldHandler({'detail':'Általános hiba.'});
}
}
window.ajaxErrorFieldHandlerJsonAPI= function (errors, container) {
console.error("d6---------------");