xtrareports-400523-web-reporting-asp-net-mvc-reporting-end-user-report-designer-in-asp-net-mvc-applications-customization-handle-server-side-errors-in-the-report-designer.md
This document describes how to handle Report Designer server-side errors. It also explains how to display detailed error information instead of a generic “Internal Server Error” message.
Tip
Online Example : How to handle server-side errors in web reporting controls
The following table lists the classes and interfaces that allow you to process errors in the Report Designer.
|
Control
|
Class (Default Implementation)
|
Interface
| | --- | --- | --- | |
Report Designer
|
ReportDesignerExceptionHandler
|
IReportDesignerExceptionHandler
| |
Built-in Document Viewer
|
WebDocumentViewerExceptionHandler
|
IWebDocumentViewerExceptionHandler
| |
|
|
|
The Report Designer displays the “Internal Server Error” message in a browser for all exceptions except FaultException and DocumentCreationException. For instance, such unknown exceptions occur when you try to open a broken report layout or a file that does not exist.
All the classes listed above provide the GetUnknownExceptionMessage method that is called whenever an unknown exception is raised. You can override this method to return a custom text or the Exception.Message property’s value as shown in the following example.
using System;
using System.IO;
using DevExpress.XtraReports.Web.ReportDesigner.Services;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using DevExpress.XtraReports.Web.QueryBuilder.Services;
public class CustomReportDesignerExceptionHandler : ReportDesignerExceptionHandler {
public override string GetUnknownExceptionMessage(Exception ex) {
if (ex is FileNotFoundException) {
#if DEBUG
return ex.Message;
#else
return "File is not found.";
#endif
}
return ex.GetType().Name + " occurred. See the log file for more details.";
}
}
}
public class CustomWebDocumentViewerExceptionHandler : WebDocumentViewerExceptionHandler {
public override string GetUnknownExceptionMessage(Exception ex) {
return ex.GetType().Name + " occurred. See the log file for more details.";
}
}
public class CustomQueryBuilderExceptionHandler : QueryBuilderExceptionHandler {
public override string GetUnknownExceptionMessage(Exception ex) {
return ex.GetType().Name + " occurred. See the log file for more details.";
}
}
Imports System
Imports System.IO
Imports DevExpress.XtraReports.Web.ReportDesigner.Services
Imports DevExpress.XtraReports.Web.WebDocumentViewer
Imports DevExpress.XtraReports.Web.QueryBuilder.Services
Public Class CustomReportDesignerExceptionHandler
Inherits ReportDesignerExceptionHandler
Public Overrides Function GetUnknownExceptionMessage(ByVal ex As Exception) As String
If TypeOf ex Is FileNotFoundException Then
#If DEBUG Then
Return ex.Message
#Else
Return "File is not found."
#End If
End If
Return ex.GetType.Name & " occurred. See the log file for more details."
End Function
End Class
}
Public Class CustomWebDocumentViewerExceptionHandler
Inherits WebDocumentViewerExceptionHandler
Public Overrides Function GetUnknownExceptionMessage(ByVal ex As Exception) As String
Return ex.GetType.Name & " occurred. See the log file for more details."
End Function
End Class
Public Class CustomQueryBuilderExceptionHandler
Inherits QueryBuilderExceptionHandler
Public Overrides Function GetUnknownExceptionMessage(ByVal ex As Exception) As String
Return ex.GetType.Name & " occurred. See the log file for more details."
End Function
End Class
The Report Designer shows the Exception.Message property’s value when FaultException occurs (for instance, when you pass an invalid URL to a report storage).
Use the GetFaultExceptionMessage method to return a custom message for these exceptions as demonstrated below.
using System;
using System.ServiceModel;
using DevExpress.XtraReports.Web.ReportDesigner.Services;
public class CustomReportDesignerExceptionHandler : ReportDesignerExceptionHandler {
public override string GetFaultExceptionMessage(FaultException ex) {
return "FaultException occurred: " + faultException.Message + ".";
}
}
Imports System
Imports System.ServiceModel
Imports DevExpress.XtraReports.Web.ReportDesigner.Services
Public Class CustomReportDesignerExceptionHandler
Inherits ReportDesignerExceptionHandler
Public Overrides Function GetFaultExceptionMessage(ByVal ex As FaultException) As String
Return "FaultException occurred: " & faultException.Message & "."
End Function
End Class
The Document Viewer displays the Exception.Message property’s value for DocumentCreationException. This exception can occur when you switch to the Report Designer’s built-in Document Viewer and a report document is being generated (for instance, an error arises in the BeforePrint or AfterPrint event).
You can override the WebDocumentViewerExceptionHandler.GetDocumentCreationExceptionMessage method to return a custom text as shown below.
using DevExpress.XtraReports.Web.WebDocumentViewer;
public class CustomWebDocumentViewerExceptionHandler : WebDocumentViewerExceptionHandler {
public override string GetDocumentCreationExceptionMessage(DocumentCreationException ex) {
return "An exception occurred while document creation.";
}
}
Imports DevExpress.XtraReports.Web.WebDocumentViewer
Public Class CustomWebDocumentViewerExceptionHandler
Inherits WebDocumentViewerExceptionHandler
Public Overrides Function GetDocumentCreationExceptionMessage(ByVal ex As DocumentCreationException) As String
Return "An exception occurred while document creation."
End Function
End Class
You can implement the following interfaces to handle all server-side errors independently from their types.
Each interface contains the GetExceptionMessage method that is called when any server-side error occurs.
The code snippet below demonstrates how to implement these interfaces and return custom text.
using System;
using System.IO;
using DevExpress.XtraReports.Web.QueryBuilder.Services;
using DevExpress.XtraReports.Web.ReportDesigner.Services;
using DevExpress.XtraReports.Web.WebDocumentViewer;
public class CustomExceptionHandler : IReportDesignerExceptionHandler,
IWebDocumentViewerExceptionHandler, IQueryBuilderExceptionHandler {
public string GetExceptionMessage(Exception ex) {
return ex.GetType().Name + " occurred. See the log file for more details.";
}
}
Imports System
Imports System.IO
Imports DevExpress.XtraReports.Web.QueryBuilder.Services
Imports DevExpress.XtraReports.Web.ReportDesigner.Services
Imports DevExpress.XtraReports.Web.WebDocumentViewer
Public Class CustomExceptionHandler
Inherits IReportDesignerExceptionHandler
Implements IWebDocumentViewerExceptionHandler, IQueryBuilderExceptionHandler
Public Function GetExceptionMessage(ByVal ex As Exception) As String
Return ex.GetType.Name & " occurred. See the log file for more details."
End Function
End Class
To register the implemented exception handlers, use the following static methods at the application’s startup.
void Application_Start(object sender, EventArgs e) {
// ...
DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.
Register<IWebDocumentViewerExceptionHandler, CustomWebDocumentViewerExceptionHandler>();
DevExpress.XtraReports.Web.ReportDesigner.DefaultReportDesignerContainer.
Register<IReportDesignerExceptionHandler, CustomReportDesignerExceptionHandler>();
DevExpress.XtraReports.Web.QueryBuilder.DefaultQueryBuilderContainer.
Register<IQueryBuilderExceptionHandler, CustomQueryBuilderExceptionHandler>();
}
Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' ...
DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.
Register(Of IWebDocumentViewerExceptionHandler, CustomWebDocumentViewerExceptionHandler)()
DevExpress.XtraReports.Web.ReportDesigner.DefaultReportDesignerContainer.
Register(Of IReportDesignerExceptionHandler, CustomReportDesignerExceptionHandler)()
DevExpress.XtraReports.Web.QueryBuilder.DefaultQueryBuilderContainer.
Register(Of IQueryBuilderExceptionHandler, CustomQueryBuilderExceptionHandler)()
End Sub
See Also
Handle Server-Side Errors in the Document Viewer for APS.NET MVC