Skip to main content
Version: 2.0.0

Errors in Views

Sometimes it is necessary to display errors that occur within the view. To display and manage this category of errors, the framework provides the packages\base\view\Error class.

Error Code

For managing errors, it is strongly recommended that you assign a specific code to each error, indicating the cause of the raised error. You can use this code to locate the problem or to display an error message specific to it.
When creating an object of the Error class, you can specify the error code in its first argument. You can also use the setCode method after creating the object to set the error code.
By calling the getCode method, you can retrieve the specified error code.

controllers/Orders.php
namespace packages\packagename\controllers;

use themes\themename\views\packagenaem as views;
use packages\packagename\{hosts\Plan as HostModel, Order as OrderModel, Tld as TldModel};
use packages\base\{Controller, Response, View, Http, InputValidationException, view\Error};

class Orders extends Controller
{
public function domain($data): Response
{
$view = View::byName(views\domains\Order::class);
$this->response->setView($view);

if (Http::is_post()) {
$this->response->setStatus(false);
$inputs = $this->checkinputs(array(
'domain' => array(
"type" => "string",
),
'tld' => array(
'type' => array('number', 'string'),
),
'hostPlan' => array(
'type' => HostModel::class,
'optional' => true
)
));
if (!$inputs["tld"]) {
throw new InputValidationException("tld");
}
if (!$inputs["domain"]) {
throw new InputValidationException("domain");
}
if (is_number($inputs["tld"])) {
$inputs["tld"] = TldModel::byId($inputs["tld"]);
if (!$inputs["tld"]) {
throw new InputValidationException("tld");
}
$inputs["tld"] = $inputs["tld"]->code;
}
$isAvailable = OrderModel::checkDomainAvailability($inputs["domain"] . "." . $inputs["tld"]);
if (!$isAvailable) {
$error = new Error("domain_is_not_available");
// $error->setCode("domain_is_not_available");
$view->addError($error);
/**
* Or you can throw it; Jalno will catch it and add it to the view errors automatically.
*
* throw $error;
*/
}
$order = new Order();
$order->user_id = 1; // Current user id
$order->domain = $inputs["domain"] . "." . $inputs["tld"];
if (isset($inputs["hostPlan"])) {
$order->host_id = $inputs["hostPlan"]->id;
}
$order->status = OrderModel::NOT_CONFIGURED;
$order->save();

$this->response->setStatu(true);
} else {
$this->response->setStatu(true);
}
return $this->response;
}
}

Error Type

In the framework, errors are divided into the following four categories, each of which represents a severity level of the error.

The error type is stored in the class's type property, and its default value is fatal.

LevelPurpose
successConfirmation message
warningErrors that do not stop the program's execution flow but require more attention or scrutiny
fatalErrors for which the program's execution flow must be stopped
noticeNotification of problems that exist in the program
note

success is not an error; it is used to display messages in the view that convey the successful completion of an operation during the program's execution.

Setting the Error Type

The setType method is used to specify the error type. For errors, the class defines the constants SUCCESS, WARNING, FATAL, and NOTICE.

The input argument of the setType method is a string and can be one of the error types. If the method's input is anything other than the above values, a type exception is thrown.

The getType method also returns the error type as a string.

use packages\base\view\Error;

$error = new Error();
$error->setType(Error::NOTICE);

Error Message

To display an error, a message must be registered for it. By calling the setMessage method, you can register a message for the error. The input argument of this method is a string.

By calling the getMessage method, you can also access the error message text.

use packages\base\view\Error;

$error = new Error();
$error->setMessage('This is error message!');

Passing Data

In addition to the error message text, you can also specify data. By calling the setData method, you can register data. The setData method takes two input arguments. The first argument is the value of the data (which can be of any data type), and the second argument is the data key.

To add multiple pieces of data, the setData method must be called several times. To avoid extra code, you can pass a key-value array as the first argument. If the second argument is also provided, the entire array is assigned to that key.

The getData method returns the registered data. The method takes one input argument, which is the name of the key specified when the data was set. If no input argument is passed to the method, it returns an array of all the registered data.

Example

controllers/Classrooms.php
namespace packages\packagename\controllers;

use themes\themename\views;
use packages\base\{View, NotFound, Controller, view\Error};
use packages\packagename\{Student as StudentModel, ClassRoom as ClassroomModal, ClassStudent};

class Classrooms extends Controller
{
public function addStudent($data)
{
$student = StudentModel::byId($data['student']);
$classroom = ClassroomModal::byId($data['classroom']);
if (!$student or !$classroom) {
throw new NotFound();
}
$view = View::byName(views\classroom\AddStudent::class);
$this->response->setView($view);

if ($classroom->status == ClassroomModal::FINISHED) {
$error = new Error();
$error->setData($classroom->id, 'classroomId');
$error->setData($classroom, 'classroomObj');
$error->setMessage("The class has ended!");
$view->addError($error);
return $this->response;
}

$classStudent = new ClassStudent();
$classStudent->class = $classroom->id;
$classStudent->student = $student->id;
$classStudent->save();

$this->response->setStatus(true);
return $this->response;
}
}

In the example above, the addError method defined in the packages\base\View class is used to pass errors to the view.

Setting the Trace Mode

The Error class defines a traceMode property that determines which of the class's properties are shown in the output when the jsonSerialize() method is called.
The setTraceMode method is implemented for setting or changing the traceMode, and it receives one of the constants defined below as its only argument. When this property is not set by the programmer, the value FULL_TRACE is used automatically.

ModePurpose
NO_TRACEReturns only the error type and the error message
SHORT_TRACEIn the trace key, information about the files executed to render the page is displayed in a brief form
FULL_TRACEIn the trace key, information about the files executed to render the page is displayed as an array

By calling the getTraceMode method, you can also access the value of traceMode.

$error = new Error();
$error->setTraceMode(Error::SHORT_TRACE);

Retrieving Errors in the View

You can retrieve the registered errors using the getError and getErrors methods.
The getError method returns an object of the Error class, which is the first registered error, and the getErrors method returns an array of Error class objects containing all the registered errors.

<body>
<?php
$errors = $this->getErrors();
if ($errors) {
foreach ($errors as $error) {
$message = $error->getMessage();
if (!$message) {
$message = t($error->getCode());
}
echo '<div class="alert alert-danger" role="alert">
<strong>'. $message . '</strong>
</div>';
}
}
?>

The jsonSerialize Method

The jsonSerialize() method is used to convert an Error class object into an array that can be converted to JSON. This method determines the output information based on the mode specified in the traceMode property.

Serialize

When an object of the class is serialized, the output information is determined based on the mode specified in the traceMode property and returned in the output.