Error Handling
Introduction
Damsy ERP handles failures through a combination of global ASP.NET error hooks, customErrors configuration, and module-local ManageErrorController instances. Users see friendly error pages instead of raw exception details in production, while developers receive file-based logs via ErrorHandling.LogFileWrite.
Client-side AJAX calls often redirect to ManageError when JSON endpoints fail.
Purpose
This page documents how errors propagate from exceptions to visible pages — globally and inside MVC Areas — so you can handle failures consistently when adding controllers or JavaScript.
When to Use
Use this page when you:
- Debug 404 vs 500 user experiences
- Add AJAX error handling in a new view
- Configure production
customErrorsmode - Place module-specific error pages with correct Area chrome
How It Works
Error handling layers
Layer 1 — Application_Error
File: Global.asax.cs — Application_Error
| Step | Action |
|---|---|
| 1 | Capture Server.GetLastError() |
| 2 | Build route to ManageErrorController (root) |
| 3 | HTTP 404 → action E404; else Index |
| 4 | Pass route values: url, userAddress, error |
| 5 | Server.ClearError() |
| 6 | Response.TrySkipIisCustomErrors = true |
| 7 | Manually execute ManageErrorController |
| 8 | Log exception to file |
This runs for unhandled exceptions outside normal MVC filter handling.
Layer 2 — customErrors
File: Web.config
<customErrors mode="RemoteOnly" defaultRedirect="~/ManageError/Index" />
| Mode | Behavior |
|---|---|
RemoteOnly | Detailed errors locally; redirect remote clients to ManageError |
defaultRedirect | Fallback URL when ASP.NET handles error page redirection |
Works alongside Application_Error — IIS custom errors are skipped when TrySkipIisCustomErrors is set in code.
Layer 3 — ManageErrorController
Root: DamsyERP.UI/Controllers/ManageErrorController.cs
[AllowAnonymous]
public class ManageErrorController : Controller
{
public ActionResult Index() => View();
public ActionResult E404() => View();
}
| Action | View | Use case |
|---|---|---|
Index | Views/ManageError/Index.cshtml | General errors (500-class) |
E404 | Views/ManageError/E404.cshtml | Not found |
AllowAnonymous ensures error pages render even when session expired.
Area ManageError controllers
Most modules duplicate ManageError under their Area for consistent sidebar/layout when AJAX redirects from module pages:
| Area | Controller path | Views |
|---|---|---|
| Payroll | Areas/Payroll/Controllers/ManageErrorController.cs | Areas/Payroll/Views/ManageError/ |
| Bills | Areas/Bills/Controllers/ | Index |
| Stock | Areas/Stock/Controllers/ | Index |
| Sales | Areas/Sales/Controllers/ | Index |
| ESS | Areas/ESS/Controllers/ | Index |
| Registration | Areas/Registration/Controllers/ | Index |
| AttendancePunching | Areas/AttendancePunching/Controllers/ | Index |
| Operation | Areas/Operation/Controllers/ | Index |
| Adminisration | Areas/Adminisration/Controllers/ | Index |
| ManagmentDashBoard | Areas/ManagmentDashBoard/Controllers/ | Index |
| ContractedRates | Areas/ContractedRates/Controllers/ | Index, E404 |
| VerificationAndApproval | Areas/VerificationAndApproval/Controllers/ | Index, E404 |
Area error views keep users inside module chrome instead of root layout.
AJAX redirect pattern
Module Razor views and partials commonly use:
// Root error (no area)
window.location.href = '@Url.Action("Index", "ManageError", new { area = "" })';
// Module-scoped error
window.location.href = '@Url.Action("Index", "ManageError", new { area = "Payroll" })';
Examples appear in Payroll attendance views, Stock vendor partials, Operation grievance screens, and AttendancePunching entry forms.
When a JSON action throws or returns unexpected data, client scripts navigate to ManageError rather than showing raw exception text.
Crystal / Web Forms errors
Report viewer .aspx pages may catch failures and redirect:
Response.Redirect("~/Bills/ManageError/Index");
See Crystal Reports.
Logging
DamsyERP.UI.ErrorLog.ErrorHandling.LogFileWrite writes exception details to log files on disk (path configured in error log helper). Used by:
Application_ErrorSession_End- Login company load failures
- Report viewer catch blocks
Root views are Index.cshtml and E404.cshtml under Views/ManageError/ — there is no separate Error.cshtml at root; some documentation refers generically to "error views."
Developer Notes
- Try/catch in controllers: Many actions catch exceptions, log, and return JSON
{ isError: true }— those may never hitApplication_Error. - 404 routing: Unknown MVC routes may hit
Application_ErrorwithHttpExceptioncode 404 →E404view. - ManageError route values:
url,userAddress, anderrorare passed in global handler — views may optionally display reference ID from logs. - Production mode: Set
customErrors mode="On"for production if not already overridden by transform.
Recommended AJAX pattern for new views
$.ajax({
url: saveUrl,
type: 'POST',
success: function (data) {
if (data.isError) {
window.location.href = manageErrorUrl;
}
},
error: function () {
window.location.href = manageErrorUrl;
}
});
Examples
Global unhandled exception
NullReference in Area controller (uncaught)
→ Application_Error
→ ManageErrorController.Index (root)
→ Views/ManageError/Index.cshtml
→ Log file entry
404 unknown URL
GET /NonExistent/Action
→ HttpException 404
→ Application_Error → E404 action
→ Views/ManageError/E404.cshtml
Payroll AJAX save failure
POST /Payroll/SomeAction returns 500
→ JavaScript error handler
→ Redirect /Payroll/ManageError/Index
→ Area error view with Payroll layout
Related Pages
Important Notes
Authorization filters on the original controller do not run for the error controller execution path. ManageError is [AllowAnonymous] by design.
Keep customErrors enabled for remote users in production. Use logs for diagnosis.
Use the current module's Area in Url.Action so users retain context and navigation options.
Common Mistakes
| Mistake | Result |
|---|---|
| Swallowing exceptions silently | User sees stale UI; no log entry |
| Redirecting AJAX errors to login | Confuses session expiry with server error |
| Using root ManageError from module view | User loses module sidebar |
| Removing TrySkipIisCustomErrors | IIS generic error replaces MVC page |
| Expecting Error.cshtml filename | View is Index.cshtml / E404.cshtml |
Next Steps
- Verify log file path and permissions on deployment servers.
- Read Request Pipeline for HTTPS and header handling adjacent to error flow.