Skip to content

Commit f6b04a2

Browse files
committed
code refector and improvements
1 parent 1e57c31 commit f6b04a2

File tree

5 files changed

+93
-27
lines changed

5 files changed

+93
-27
lines changed

config/larvabug.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,41 @@
22

33
return [
44

5+
/*
6+
* Id of project from larvabug.com
7+
*
8+
*/
59
'project_id' => env('LB_PROJECT_ID',''),
610

11+
/*
12+
* Project secret from larvabug.com
13+
*
14+
*/
715
'project_secret' => env('LB_SECRET',''),
816

17+
/*
18+
* Array of environments to enable error reporting
19+
* If environment configured in the array will match with the laravel app environment
20+
* then error will be reported to larvabug.com
21+
*
22+
*/
923
'environment' => ['production','local'],
1024

25+
/*
26+
* Mention any error that should skip from reporting to laravbug
27+
* Must be mentioned as a string
28+
*
29+
*/
1130
'skip_errors' => [
1231
'\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class'
1332
],
1433

34+
/*
35+
* Add request parameters to be black listed
36+
* any parameter defined here will not be reported to larvabug.com and
37+
* all request, session and cookies will be filtered
38+
*
39+
*/
1540
'blacklist' => [
1641
'password'
1742
]

src/Client/HttpClient.php

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ class HttpClient
1717
*/
1818
private $projectSecret;
1919

20-
private const URL = 'http://larvabug.local/api/v1/exception';
20+
//Development
21+
private const POST_EXCEPTION = 'http://dev.larvabug.com/api/v1/exception';
22+
private const VALIDATE_CREDENTIALS = 'http://dev.larvabug.com/api/validate/credentials';
2123

2224
/**
2325
* @param string $projectId
2426
* @param string $projectSecret
2527
*/
26-
public function __construct(string $projectId, string $projectSecret)
28+
public function __construct(string $projectId = null, string $projectSecret = null)
2729
{
2830
$this->projectId = $projectId;
2931
$this->projectSecret = $projectSecret;
@@ -39,24 +41,14 @@ public function report($exceptionData)
3941
try {
4042
$data_string = json_encode($exceptionData);
4143

42-
$header = [
43-
'Content-Type:application/json',
44-
'Authorization-APP:' . $this->projectId,
45-
'Authorization-KEY:' . $this->projectSecret
46-
];
44+
$result = $this->postRequest($data_string,self::POST_EXCEPTION);
4745

48-
$ch = curl_init(self::URL);
49-
50-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
51-
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
52-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53-
54-
$result = curl_exec($ch);
55-
56-
curl_close($ch);
57-
58-
if ($result && $result != 404){
59-
Session::put('lb.lastExceptionId', $result);
46+
if ($result &&
47+
isset($result['status']) &&
48+
isset($result['exceptionId']) &&
49+
$result['status'] == 200
50+
){
51+
Session::put('lb.lastExceptionId', $result['exceptionId']);
6052
}
6153

6254
return true;
@@ -65,4 +57,37 @@ public function report($exceptionData)
6557
}
6658
}
6759

60+
public function validateCredentials(array $credentials)
61+
{
62+
$result = $this->postRequest($credentials,self::VALIDATE_CREDENTIALS);
63+
64+
if ($result && isset($result['status']) && $result['status'] == 200){
65+
return true;
66+
}
67+
68+
return false;
69+
70+
}
71+
72+
private function postRequest($requestData, $url)
73+
{
74+
$header = [
75+
'Content-Type:application/json',
76+
'Authorization-APP:' . $this->projectId,
77+
'Authorization-KEY:' . $this->projectSecret
78+
];
79+
80+
$ch = curl_init($url);
81+
82+
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
83+
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
84+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
85+
86+
$result = curl_exec($ch);
87+
88+
curl_close($ch);
89+
90+
return $result;
91+
}
92+
6893
}

src/Commands/LarvaBugTestCommand.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@ public function handle()
5151
$this->info('2. ✗ [Larvabug] Could not find LarvaBug secret, please set this in your .env');
5252
}
5353

54+
$requestData = [
55+
'projectId' => config('larvabug.project_id'),
56+
'projectSecret' => config('larvabug.project_secret')
57+
];
5458

55-
}
56-
57-
public function testCredentials()
58-
{
59+
if (app('larvabug') && app('larvabug')->validateCredentials($requestData)){
60+
$this->info('3. ✓ [Larvabug] Validation Success');
61+
}else{
62+
$this->info('3. ✗ [Larvabug] Project id and secret do not match our records');
63+
}
5964

6065
}
66+
6167
}

src/Handler/LarvaBugExceptionHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function handle(\Throwable $exception)
5353
*
5454
* @return bool
5555
*/
56-
public function checkAppEnvironment()
56+
private function checkAppEnvironment()
5757
{
5858
if (!config('larvabug.environment')){
5959
return false;
@@ -80,4 +80,9 @@ protected function skipError($class)
8080

8181
return false;
8282
}
83+
84+
public function validateCredentials(array $credentials)
85+
{
86+
return $this->client->validateCredentials($credentials);
87+
}
8388
}

src/Handler/PrepareExceptionData.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,20 @@ private function getExceptionData(\Throwable $exception)
4545
$data['exception'] = $exception->getMessage();
4646
$data['class'] = get_class($exception);
4747
$data['file'] = $exception->getFile();
48-
$data['enviroment'] = App::environment();
48+
$data['php_version'] = PHP_VERSION;
49+
$data['server_ip'] = $_SERVER['SERVER_ADDR'] ?? null;
50+
$data['environment'] = App::environment();
4951
$data['server_name'] = @gethostname();
5052
$data['browser'] = $this->getUserBrowser();
5153
$data['userOs'] = $this->getUserOS();
5254
$data['host'] = Request::server('SERVER_NAME');
5355
$data['method'] = Request::method();
5456
$data['fullUrl'] = Request::fullUrl();
57+
$data['url'] = Request::path();
58+
$data['userIp'] = Request::ip();
5559
$data['line'] = $exception->getLine();
56-
$data['date_time'] = date("Y-m-d H:i:s");;
60+
$data['date_time'] = date("Y-m-d H:i:s");
61+
$data['session_id'] = Session::getId();
5762
$data['storage'] = [
5863
'SERVER' => [
5964
'USER' => Request::server('USER'),
@@ -79,7 +84,7 @@ private function getExceptionData(\Throwable $exception)
7984
$count = config('larvabug.lines_count');
8085

8186
if (!$count || $count > 12) {
82-
$count = 5;
87+
$count = 10;
8388
}
8489

8590
$lines = file($data['file']);

0 commit comments

Comments
 (0)