• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / auth / 16343465380

17 Jul 2025 11:07AM UTC coverage: 59.224% (-0.6%) from 59.854%
16343465380

push

github

web-flow
Merge pull request #23 from daycry/development

Improvements

57 of 292 new or added lines in 16 files covered. (19.52%)

6 existing lines in 4 files now uncovered.

1939 of 3274 relevant lines covered (59.22%)

22.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

5.0
/src/Services/ExceptionHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth\Services;
15

16
use CodeIgniter\HTTP\RequestInterface;
17
use CodeIgniter\HTTP\ResponseInterface;
18
use CodeIgniter\Validation\Validation;
19
use Exception;
20
use ReflectionProperty;
21
use Throwable;
22

23
/**
24
 * Service for handling exceptions in Auth controllers
25
 *
26
 * Extracts exception handling logic from BaseControllerTrait
27
 */
28
class ExceptionHandler
29
{
30
    protected RequestLogger $requestLogger;
31

32
    public function __construct()
33
    {
34
        $this->requestLogger = new RequestLogger();
15✔
35
    }
36

37
    /**
38
     * Handle exceptions with proper authorization status and response
39
     */
40
    public function handleException(
41
        Throwable $ex,
42
        RequestInterface $request,
43
        ResponseInterface $response,
44
        ?Validation $validator = null,
45
        ?object $controller = null,
46
    ): ResponseInterface {
47
        // Extract authorization status from exception if available
NEW
48
        if (property_exists($ex, 'authorized')) {
×
NEW
49
            $authorized = (new ReflectionProperty($ex, 'authorized'))->getValue();
×
NEW
50
            $this->requestLogger->setRequestAuthorized($authorized);
×
51
        } else {
NEW
52
            $this->requestLogger->setRequestAuthorized(false);
×
53
        }
54

55
        // Get error message
NEW
56
        $message = $validator?->getErrors() ?: $ex->getMessage();
×
NEW
57
        $code    = $ex->getCode() ?: 400;
×
58

59
        // Generate CSRF token for response
NEW
60
        $token = ['name' => csrf_token(), 'hash' => csrf_hash()];
×
61

62
        // Handle response based on controller capabilities and request type
NEW
63
        if ($controller && method_exists($controller, 'fail')) {
×
NEW
64
            return $controller->fail($message, $code);
×
65
        }
66

67
        // Check if request is AJAX (assuming IncomingRequest which has isAJAX method)
NEW
68
        $isAjax = method_exists($request, 'isAJAX') ? $request->isAJAX() : false;
×
69

NEW
70
        if ($isAjax) {
×
NEW
71
            return $response->setStatusCode($code)->setJSON([
×
NEW
72
                'status' => false,
×
NEW
73
                'error'  => $message,
×
NEW
74
                'token'  => $token,
×
NEW
75
            ]);
×
76
        }
77

78
        // For non-AJAX requests without fail method, re-throw the exception
NEW
79
        throw $ex;
×
80
    }
81

82
    /**
83
     * Get the request logger instance
84
     */
85
    public function getRequestLogger(): RequestLogger
86
    {
NEW
87
        return $this->requestLogger;
×
88
    }
89

90
    /**
91
     * Static method to get a new instance
92
     */
93
    public static function getInstance(): self
94
    {
NEW
95
        return new self();
×
96
    }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc