Skip to main content

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 customErrors mode
  • Place module-specific error pages with correct Area chrome

How It Works

Error handling layers

Layer 1 — Application_Error

File: Global.asax.csApplication_Error

StepAction
1Capture Server.GetLastError()
2Build route to ManageErrorController (root)
3HTTP 404 → action E404; else Index
4Pass route values: url, userAddress, error
5Server.ClearError()
6Response.TrySkipIisCustomErrors = true
7Manually execute ManageErrorController
8Log 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" />
ModeBehavior
RemoteOnlyDetailed errors locally; redirect remote clients to ManageError
defaultRedirectFallback 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();
}
ActionViewUse case
IndexViews/ManageError/Index.cshtmlGeneral errors (500-class)
E404Views/ManageError/E404.cshtmlNot 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:

AreaController pathViews
PayrollAreas/Payroll/Controllers/ManageErrorController.csAreas/Payroll/Views/ManageError/
BillsAreas/Bills/Controllers/Index
StockAreas/Stock/Controllers/Index
SalesAreas/Sales/Controllers/Index
ESSAreas/ESS/Controllers/Index
RegistrationAreas/Registration/Controllers/Index
AttendancePunchingAreas/AttendancePunching/Controllers/Index
OperationAreas/Operation/Controllers/Index
AdminisrationAreas/Adminisration/Controllers/Index
ManagmentDashBoardAreas/ManagmentDashBoard/Controllers/Index
ContractedRatesAreas/ContractedRates/Controllers/Index, E404
VerificationAndApprovalAreas/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_Error
  • Session_End
  • Login company load failures
  • Report viewer catch blocks
Error.cshtml naming

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 hit Application_Error.
  • 404 routing: Unknown MVC routes may hit Application_Error with HttpException code 404 → E404 view.
  • ManageError route values: url, userAddress, and error are 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.
$.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

Important Notes

Application_Error bypasses filters

Authorization filters on the original controller do not run for the error controller execution path. ManageError is [AllowAnonymous] by design.

Do not expose exception details remotely

Keep customErrors enabled for remote users in production. Use logs for diagnosis.

Pick correct Area in AJAX redirects

Use the current module's Area in Url.Action so users retain context and navigation options.

Common Mistakes

MistakeResult
Swallowing exceptions silentlyUser sees stale UI; no log entry
Redirecting AJAX errors to loginConfuses session expiry with server error
Using root ManageError from module viewUser loses module sidebar
Removing TrySkipIisCustomErrorsIIS generic error replaces MVC page
Expecting Error.cshtml filenameView is Index.cshtml / E404.cshtml

Next Steps

  1. Verify log file path and permissions on deployment servers.
  2. Read Request Pipeline for HTTPS and header handling adjacent to error flow.