Handler.php
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php namespace App\Exceptions;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Config;
use Auth;
use Request;
use Mail;
use Cache;
class Handler extends ExceptionHandler {
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
if(!empty($e))
{
$errorStr='';
if ($this->isHttpException($e) && $e->getStatusCode() == 404)$errorStr='MISSING';
else if(strstr(json_encode($e->getTrace(),true),"VerifyCsrfToken"))$errorStr='IGNORE';
else $errorStr='ERROR';
$accesslog=Config::get('runtime.accesslog_obj');
if($accesslog)
{
$postdata=(array)json_decode($accesslog->postdata);
$postdata["ErrorType"]=$errorStr;
$postdata["ErrorMsg"]=$e->getMessage();
$accesslog->postdata=json_encode($postdata, true);
$accesslog->stopLog();
}
if($errorStr=='ERROR')
{
$email=Config::get("app.email");
$user="guest";if(Auth::check())$user=Auth::user()->id." ".Auth::user()->dispname()." ".Auth::user()->username;
$ip=Request::getClientIp();
$key=$user.$ip.'ajaxerror'.Request::path();
if(Cache::get($key,'')!=$e->getMessage().json_encode($e->getTrace(),true)&&Cache::get($key.'cnt',0)<10)
{
Cache::put($key,$e->getMessage().json_encode($e->getTrace(),true), 24*60);//minutes in 1 day
Cache::put($key.'cnt',Cache::get($key.'cnt',0)+1, 24*60);
Mail::send('emails.ajaxerror', array('xhr'=>"app-fatal",'status'=>$e->getMessage(),'error'=>json_encode($e->getTrace(),true),'user'=>$user,'ip'=>$ip), function($message) use ($email)
{
$message->to($email, $email)->subject(Config::get("app.name")." : Error");
});
}
}
else if($errorStr=='MISSING'){}
else if($errorStr=='IGNORE'){}
}
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if(!empty($e))
{
$errorStr='';
if ($this->isHttpException($e) && $e->getStatusCode() == 404)$errorStr='MISSING';
else $errorStr='ERROR';
if($errorStr=='ERROR')
{
if(!Config::get('app.debug')) return response()->view("errors.exception");
}
else if($errorStr=='MISSING')
{
return response()->view('errors.missing', array());
}
}
return parent::render($request, $e);
}
}