xtrareports-400510-web-reporting-asp-net-webforms-reporting-document-viewer-in-asp-net-webforms-reporting-customization-handle-server-side-errors-in-the-document-viewer.md
This document describes how to handle Document Viewer 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
You can create a descendant from the WebDocumentViewerExceptionHandler class and override its methods to process all or specific errors. This class implements the IWebDocumentViewerExceptionHandler interface.
The Document Viewer 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.
You can override the GetUnknownExceptionMessage 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.WebDocumentViewer;
public class CustomWebDocumentViewerExceptionHandler : WebDocumentViewerExceptionHandler {
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.";
}
}
}
Imports System
Imports System.IO
Imports DevExpress.XtraReports.Web.WebDocumentViewer
Public Class CustomWebDocumentViewerExceptionHandler
Inherits WebDocumentViewerExceptionHandler
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
The Document Viewer 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.WebDocumentViewer;
public class CustomWebDocumentViewerExceptionHandler : WebDocumentViewerExceptionHandler {
public override string GetFaultExceptionMessage(FaultException ex) {
return "FaultException occurred: " + faultException.Message + ".";
}
}
Imports System
Imports System.ServiceModel
Imports DevExpress.XtraReports.Web.WebDocumentViewer;
Public Class CustomWebDocumentViewerExceptionHandler
Inherits WebDocumentViewerExceptionHandler
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 a report document is being generated (for instance, an error arises in the BeforePrint or AfterPrint event).
You can override the 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
Use the GetExceptionMessage method to handle all server-side errors independently from their types.
The code snippet below demonstrates how to override this method and return a custom text for all exceptions.
using System;
using System.IO;
using DevExpress.XtraReports.Web.WebDocumentViewer;
public class CustomWebDocumentViewerExceptionHandler : WebDocumentViewerExceptionHandler {
public override 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.WebDocumentViewer
Public Class CustomWebDocumentViewerExceptionHandler
Inherits WebDocumentViewerExceptionHandler
Public Overrides 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 handler, use the static DefaultWebDocumentViewerContainer.Register method at the application’s startup.
void Application_Start(object sender, EventArgs e) {
// ...
DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.
Register<IWebDocumentViewerExceptionHandler, CustomWebDocumentViewerExceptionHandler>();
}
Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' ...
DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.
Register(Of IWebDocumentViewerExceptionHandler, CustomWebDocumentViewerExceptionHandler)()
End Sub
See Also