Displaying error messages with _error

When a new employee record failed to be added to the database because of some error, that error should be shown to the user to let the user know why the insertion failed so the user can try to resolve the problem.

When an update attempt failed, the error should also be shown to let the user know the state of things.

After a failed search when the employee was not found, the message should be also be displayed.

When a login attempt failed, the login page should display again with an error message telling the user why the login attempt failed.

When a method in EmployeeController generates an error, the error message is captured in the _error variable which is generated automatically, so we can use it to display the error. The facility to display this _error message is provided by the @errorDisplay annotation. The @errorDisplay indicates which method and page will handle the error.

As an example, let us edit the postFindEmployee() method of EmployeeController.

  @errorDisplay!getAllEmployees
  void postFindEmployee(string fname, string lname)
  {
    import std.uni; //so we can use the toUpper() function
    string first = toUpper(fname);
    string last = toUpper(lname);
    Employee e = empModel.findEmployee(first, last);
    enforce(e != Employee.init, fname ~ " " ~ lname ~ " not found.");
    render!("employee.dt", e);
  }

Here, we indicated that the getAllEmployees() method will display the error message. We used the enforce() function to generate an error if the condition is not met. In this case, the getAllEmployees() method should be called with a new parameter named _error, which was generated automatically, and which it can pass to the emplistall.dt page for display.

So here is the edited getAllEmployees() method.

  void getAllEmployees(string _error = null)
  {
    string error = _error;
    Employee[] emps = empModel.getEmployees;
    render!("emplistall.dt", emps, error);
  }

We assigned the _error parameter with null as default in case the error message is blank.

Edit the views\emplistall.dt page so it can display the error, if there is an error.

extends layout
block maincontent
  include csstable.dt
  -if(error)
    div.error-div
      span.error-message #{error}
  div.table-wrapper
    table
      tr
        th Employee Id
        th First name
        th Last name
        th Department
        th Phone number
        th Email address
        th Street address
        th City
        th Province
        th PostCode
        th Action
      -foreach(e; emps)
        tr
          td #{e.empid}
          td #{e.fname}
          td #{e.lname}
          td #{e.deprt}
          td #{e.phone}
          td #{e.email}
          td #{e.street}
          td #{e.city}
          td #{e.province}
          td #{e.postcode}
          td  
            form.form-hidden(method="get", action="edit_employee")
              input(type="hidden", name="id", value="#{e.id}")
              input(type="image", src="images/pencil.ico", height="15px")
            |  
            form.form-hidden(method="get", action="delete_employee")
              input(type="hidden", name="id", value="#{e.id}")
              input(type="image", src="images/trash.ico", height="15px")
            |  

And here is our addition to the views\styles.dt file.

  .error-div
  {
    width: 90%;
    margin: 20px auto;
  }
  .error-message
  {
    color: brown;
    font-weight: bold;
  }

Compile, run and refresh your browser to test the Find employee link and look for a non-existent record.

After clicking Find, you should get this:

Now we can add error-trapping mechanisms to the add, edit and delete methods as well.

Now let’s talk about securing our site. Let’s talk about logging in, authentication and authorization.

Last updated