Skip to content

Commit 137fc0a

Browse files
authored
Improve error handling in Pihole app (#818)
* Update config.blade.php * Update Pihole.php * Linting
1 parent 276a64b commit 137fc0a

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

Pihole/Pihole.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public function test()
2929
if ($test["valid"]) {
3030
echo "Successfully communicated with the API";
3131
} else {
32-
echo "Error while communicating with the API";
32+
echo "Error while communicating with the API: " . $test["message"];
3333
}
3434
}
3535
}
3636
public function livestats()
3737
{
38+
$data = [];
39+
$status = "inactive";
3840
$version = $this->config->version;
3941

4042
if ($version == 5) {
@@ -56,10 +58,12 @@ public function livestats()
5658
if ($version == 6) {
5759
$results = $this->getInfo();
5860

59-
$data["ads_blocked"] = $results["queries"];
60-
$data["ads_percentage"] = $results["percent"];
61+
if ($results["valid"]) {
62+
$data["ads_blocked"] = $results["queries"];
63+
$data["ads_percentage"] = $results["percent"];
6164

62-
$status = "active";
65+
$status = "active";
66+
}
6367
}
6468
return parent::getLiveStats($status, $data);
6569
}
@@ -106,10 +110,21 @@ public function getInfo()
106110
];
107111
}
108112

109-
// Create session and retreave data
113+
// Create session and retrieve data
110114
$response = parent::execute($this->url("api/auth"), $attrs, null, "POST");
111115
$auth = json_decode($response->getBody());
112116

117+
if (!$auth->session->valid) {
118+
$data = [
119+
'valid' => false,
120+
'validity' => -1,
121+
'message' => $auth->session->message,
122+
'queries' => 0,
123+
'percent' => 0
124+
];
125+
return $data;
126+
}
127+
113128
if ($ignoreTls) {
114129
$attrsid = [
115130
"body" => json_encode(['sid' => $auth->session->sid]),
@@ -142,8 +157,14 @@ public function getInfo()
142157
$valid = $auth->session->valid;
143158
$validity = $auth->session->validity;
144159
$message = $auth->session->message;
145-
$queriesblocked = $datasummary->queries->blocked;
146-
$percentblocked = round($datasummary->queries->percent_blocked, 2);
160+
161+
if (!$auth->session->valid) {
162+
$queriesblocked = 0;
163+
$percentblocked = 0;
164+
} else {
165+
$queriesblocked = $datasummary->queries->blocked;
166+
$percentblocked = round($datasummary->queries->percent_blocked, 2);
167+
}
147168

148169
$data = [
149170
'valid' => $valid,

Pihole/config.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="items">
33
<input type="hidden" data-config="dataonly" class="config-item" name="config[dataonly]" value="1" />
44
<div class="input">
5-
<label>{{ strtoupper(__('app.url')) }} (for v6 use https)</label>
5+
<label>{{ strtoupper(__('app.url')) }}</label>
66
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
77
</div>
88
<div class="input">

0 commit comments

Comments
 (0)