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

daycry / auth / 21478354896

23 Jan 2026 11:45AM UTC coverage: 66.864%. Remained the same
21478354896

push

github

daycry
Update dependencies and Shield version

Replaced 'daycry/cronjob' with 'daycry/jobs' in composer.json and updated SHIELD_VERSION in Auth.php from 3.0.2 to 3.0.5.

2256 of 3374 relevant lines covered (66.86%)

33.12 hits per line

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

95.65
/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 ReflectionMethod;
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();
38✔
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
48
        if (property_exists($ex, 'authorized')) {
8✔
49
            $authorized = (new ReflectionProperty($ex, 'authorized'))->getValue($ex);
1✔
50
            $this->requestLogger->setRequestAuthorized($authorized);
1✔
51
        } else {
52
            $this->requestLogger->setRequestAuthorized(false);
7✔
53
        }
54

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

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

62
        // Handle response based on controller capabilities and request type
63
        if ($controller && method_exists($controller, 'fail')) {
8✔
64
            $reflection = new ReflectionMethod($controller, 'fail');
5✔
65
            if (! $reflection->isPublic()) {
5✔
66
                $reflection->setAccessible(true);
×
67
            }
68

69
            return $reflection->invoke($controller, $message, $code);
5✔
70
        }
71

72
        // Check if request is AJAX (assuming IncomingRequest which has isAJAX method)
73
        $isAjax = method_exists($request, 'isAJAX') ? $request->isAJAX() : false;
3✔
74

75
        if ($isAjax) {
3✔
76
            return $response->setStatusCode($code)->setJSON([
1✔
77
                'status' => false,
1✔
78
                'error'  => $message,
1✔
79
                'token'  => $token,
1✔
80
            ]);
1✔
81
        }
82

83
        // For non-AJAX requests without fail method, re-throw the exception
84
        throw $ex;
2✔
85
    }
86

87
    /**
88
     * Get the request logger instance
89
     */
90
    public function getRequestLogger(): RequestLogger
91
    {
92
        return $this->requestLogger;
2✔
93
    }
94

95
    /**
96
     * Static method to get a new instance
97
     */
98
    public static function getInstance(): self
99
    {
100
        return new self();
1✔
101
    }
102
}
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