Skip to main content

Handle the Report Designer's Server-Side Errors

  • 3 minutes to read

This document describes how to handle server-side Report Designer errors. It also explains how to display detailed error information instead of a generic “Internal Server Error” message.

View 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

Query Builder

QueryBuilderExceptionHandler

IQueryBuilderExceptionHandler

Handle Unknown Exceptions

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.";
    }
}

Handle FaultException

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 DevExpress.XtraReports.Web.ClientControls;
#if NETFRAMEWORK
using System.ServiceModel;
# endif
using DevExpress.XtraReports.Web.ReportDesigner.Services;

public class CustomReportDesignerExceptionHandler : ReportDesignerExceptionHandler {
    public override string GetFaultExceptionMessage(FaultException ex) {
        return $"FaultException occurred: {faultException.Message}.";
    }
}

Handle DocumentCreationException

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.";
    }
}

Handle All Exceptions

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.";
    }
}

Register Exception Handlers

Register the implemented exception handlers at the application startup.

using Microsoft.Extensions.DependencyInjection;
using DevExpress.XtraReports.Web.WebDocumentViewer;

var builder = WebApplication.CreateBuilder(args);

builder.Services
        .AddSingleton<IWebDocumentViewerExceptionHandler, CustomWebDocumentViewerExceptionHandler>()
        .AddSingleton<IReportDesignerExceptionHandler, CustomReportDesignerExceptionHandler>()
        .AddSingleton<IQueryBuilderExceptionHandler, CustomQueryBuilderExceptionHandler>();
}

var app = builder.Build();
See Also