Skip to content

Commit c109a6f

Browse files
committed
add server error handler
1 parent a927796 commit c109a6f

4 files changed

Lines changed: 67 additions & 41 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
2-
.env
2+
.env
3+
logs/

‎config/database.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public function connect(): ?PDO {
2424
$this->conn = null;
2525

2626
try {
27-
$dsn = "mysql:host=" . $this->DB_HOST . ";dbname=" . $this->DB_NAME . ";charset=utf8";
27+
$dsn = "mysql:host={$this->DB_HOST};dbname={$this->DB_NAME};charset=utf8";
2828
$this->conn = new PDO($dsn, $this->DB_USER, $this->DB_PASS);
2929
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
3030
} catch (PDOException $e) {
31-
echo "Connection failed: " . $e->getMessage();
31+
error_log("[" . date("Y-m-d H:i:s") . "] Connection failed: " . $e->getMessage() . "\r\n", 3, '../logs/error.log');
3232
}
3333

3434
return $this->conn;

‎src/controllers/UsersController.php‎

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,44 @@ public function __construct() {
1010
}
1111

1212
public function getAllUser() {
13-
$result = $this->user->getAllUser();
14-
15-
http_response_code(200);
16-
header('Content-Type: application/json');
17-
$response = [
18-
"status"=>"success",
19-
"message"=> "successfully get all user data from database",
20-
"data"=> $result
21-
];
22-
echo json_encode($response);
13+
try {
14+
$result = $this->user->getAllUser();
15+
16+
header('Content-Type: application/json');
17+
http_response_code(200);
18+
echo json_encode([
19+
"status"=>"success",
20+
"message"=> "successfully get all user data from database",
21+
"data"=> $result
22+
]);
23+
} catch (Exception $e) {
24+
http_response_code(500);
25+
echo json_encode([
26+
"status"=> "error",
27+
"message"=> "server error occured"
28+
]);
29+
}
2330
}
2431

2532
public function getUserById($id) {
26-
$result = $this->user->getUserById($id);
27-
28-
header('Content-Type: application/json');
29-
30-
if (empty($result)) {
31-
http_response_code(404);
32-
$message = "user not found";
33-
} else {
34-
http_response_code(200);
35-
$message = "successfully get user data from database";
33+
try {
34+
$result = $this->user->getUserById($id);
35+
36+
$message = empty($result) ? "User not found" : "Successfully retrieved user data";
37+
38+
header('Content-Type: application/json');
39+
http_response_code(empty($result) ? 404 : 200);
40+
echo json_encode([
41+
"status"=>"success",
42+
"message"=> $message,
43+
"data"=> $result
44+
]);
45+
} catch (Exception $e) {
46+
http_response_code(500);
47+
echo json_encode([
48+
"status"=> "error",
49+
"message"=> "server error occured"
50+
]);
3651
}
37-
38-
$response = [
39-
"status"=>"success",
40-
"message"=> $message,
41-
"data"=> $result
42-
];
43-
echo json_encode($response);
4452
}
4553
}

‎src/models/User.php‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,43 @@ class User {
66
private $conn;
77

88
public function __construct() {
9-
$this->conn = (new Database())->connect();
9+
try {
10+
$this->conn = (new Database())->connect();
11+
} catch (Exception $e) {
12+
$this->conn = null;
13+
error_log("[" . date("Y-m-d H:i:s") . "] Connection failed: " . $e->getMessage(), 3, '../../logs/error.log');
14+
}
1015
}
1116

12-
public function getAllUser() : array {
13-
if ($this->conn) {
17+
public function getAllUser() : ?array {
18+
try {
1419
$query = "SELECT * FROM user";
20+
21+
if ($this->conn === null) {
22+
throw new Exception("Database connection is null.");
23+
}
24+
1525
$stmt = $this->conn->query($query);
1626
return $stmt->fetchAll(PDO::FETCH_ASSOC);
17-
} else {
18-
return [];
27+
} catch (PDOException $e) {
28+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
1929
}
2030
}
2131

22-
public function getUserById($id) : array {
23-
if ($this->conn) {
24-
$query = "SELECT * FROM user WHERE id=" . $id;
25-
$stmt = $this->conn->query($query);
32+
public function getUserById($id) : ?array {
33+
try {
34+
$query = "SELECT * FROM user WHERE id=:id";
35+
36+
if ($this->conn === null) {
37+
throw new Exception("Database connection is null.");
38+
}
39+
40+
$stmt = $this->conn->prepare($query);
41+
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
42+
$stmt->execute();
2643
return $stmt->fetchAll(PDO::FETCH_ASSOC);
27-
} else {
28-
return [];
44+
} catch (PDOException $e) {
45+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
2946
}
3047
}
3148
}

0 commit comments

Comments
 (0)