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!getAllEmployeesvoidpostFindEmployee(stringfname,stringlname) {importstd.uni; //so we can use the toUpper() functionstringfirst=toUpper(fname);stringlast=toUpper(lname);Employeee=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.