2.2 ErrorController

Error Controller

When SuitUp found some error Exception it's thrown firstly the ErrorController on the current module, if this does not exist SuitUp throw the default ErrorController in src/ModuleError/Controllers/ErrorController.php. Implement your own ErrorController for each module allows you to personalize your system error pages.

How to do?

  • Create the controller class with the reserved name ErrorController inside controllers folder on the module;
  • Create the view ModuleDefault/views/error/error.phtml for general errors;
  • Create the view ModuleDefault/views/error/not-found.phtml for not found errors;
  • This error is launched if module, controller, action or view is not found.

Controller

Just create a controller with the reserved name ErrorController which extends AbstractController or MvcAbstractController, in this file you don't need to create any method, but you can override the init one to define a different layout for errors. You can too override the methods errorAction and notFoundAction if want, for example, create a system log before dispatch the view.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
// File: ModuleDefault/Controllers/ErrorController.php

namespace ModuleDefault\Controllers;

class ErrorController extends AbstractController
{
    public function init() {

        // With this line the system will use this file as specific error layout instead of current module layout.
        // Of course that for this the file `ModuleDefault/views/error/layout.phtml` must to exist.
        $this->setLayout("error/layout.phtml");
    }
}

Para entender melhor o que nós chamamos de layout [[clique aqui|3. Layouts (en)]]

Views

Obviously errors control are very important in the system to menage what is working and don't, so we made this easy way to follow errors on SuitUp. Otherwise don't make sense to show it as exceptions to the user, which it's a security failure so we use if DEVELOPMENT in the view. This prevents that the Exception description is shown to the user when system aren't in development environment.

Automatically the variable $exception will be available on the view with all information about the error occurred.

Follow the tip to the HTML for these errors page.

ModuleDefault/views/error/error.phtml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<div class="page-header">
    <h1>Ouch! Unexpected error.</h1>
</div>
<?php if (DEVELOPMENT && isset($exception)): ?>
    <div class="panel panel-primary">
        <div class="panel-heading">System error</div>
        <table class="table table-striped">
            <thead>
                <tr>
                    <td>Message: </td>
                    <td><?php echo $exception->getMessage(); ?></td>
                </tr>
            </thead>
            <tbody>
                <tr>
                 File: </td>
                    <td><?php echo $exception->getFile(); ?> :: (( <b><?php echo $exception->getLine(); ?></b> ))</td>
                </tr>
                <?php if ($exception->getTrace()): ?>
                    <tr>
                        <td>Stack Trace: </td>
                        <td>
                            <div class="row">
                                <?php foreach ($exception->getTrace() as $key => $trace): ?>
                                    <div class="col-sm-4">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">#<?php echo $key; ?></div>
                                            <div class="panel-body">
                                                <?php if ($trace['args']): ?>
                                                    <?php echo $trace['function']; ?>('<?php echo implode("','", $trace['args']); ?>');
                                                <?php else: ?>
                                                    <?php echo $trace['function']; ?>();
                                                <?php endif; ?>
                                                <br/>
                                                <br/>
                                                File: <?php echo $trace['file']; ?>::(<?php echo $trace['line']; ?>)
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td>String: </td>
                    <td><div class="well well-sm"><?php echo nl2br($exception->__toString()); ?></div></td>
                </tr>
            </tfoot>
        </table>
    </div>
<?php endif; ?>
<p>
    <a href="/">Back to the home</a>
</p>

ModuleDefault/views/error/not-found.phtml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<h1>Page not found</h1>
<?php if (DEVELOPMENT && isset($exception)): ?>
    <div class="panel panel-primary">
        <div class="panel-heading">System error</div>
        <table class="table table-striped">
            <thead>
                <tr>
                    <td>Message: </td>
                    <td><?php echo $exception->getMessage(); ?></td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>File: </td>
                    <td><?php echo $exception->getFile(); ?> :: (( <b><?php echo $exception->getLine(); ?></b> ))</td>
                </tr>
                <?php if ($exception->getTrace()): ?>
                    <tr>
                        <td>Stack Trace: </td>
                        <td>
                            <div class="row">
                                <?php foreach($exception->getTrace() as $key => $trace): ?>
                                    <div class="col-sm-4">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">#<?php echo $key; ?></div>
                                            <div class="panel-body">
                                                <?php if ($trace['args']): ?>
                                                    <?php echo $trace['function']; ?>('<?php echo implode("','", $trace['args']); ?>');
                                                <?php else: ?>
                                                    <?php echo $trace['function']; ?>();
                                                <?php endif; ?>
                                                <br/>
                                                <br/>
                                                File: <?php echo $trace['file']; ?>::(<?php echo $trace['line']; ?>)
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td>String: </td>
                    <td><div class="well well-sm"><?php echo nl2br($exception->__toString()); ?></div></td>
                </tr>
            </tfoot>
        </table>
    </div>
<?php endif; ?>
<p>
    <a href="/">Back to the home</a>
</p>