Show/Hide Toolbars

TMS XData Documentation

Navigation: Server-Side Events

OnModuleException Event

Scroll Prev Top Next More

Occurs when an unexpected exception is raised during server request processing. By default, when that happens XData will send a response to the client with the property HTTP status code (usually 500 but other codes might be provided as well) and a JSON response with details of the error (a JSON object with properties ErrorCode and ErrorMessage). You can use this event to change that behavior when a specific exception occurs.

 

Example:

 

Module.Events.OnModuleException.Subscribe(
  procedure(Args: TModuleExceptionArgs)
  begin
    // code here, for example:
    if Args.Exception is EInvalidJsonProperty then
      Args.StatusCode := 400;
  end

);

 

TModuleExceptionArgs Properties:

 

Name

Description

Exception: Exception

The Exception object raised while processing the requested.

StatusCode: Integer

The HTTP status code to be returned to the client. You can change this property to tell XData to send a different status code.

ErrorCode: string

The value of the ErrorCode property in the JSON response to be sent to the client. You can modify this value.

ErrorMessage: string

The value of the ErrorMessage property in the JSON response to be sent to the client. You can modify this value.

Action: TModuleExceptionAction

The action to be performed. Default value is SendError which means XData will send the HTTP response to the client with the specified StatusCode and with a JSON response that includes ErrorCode and ErrorMessage.

You can optionally use RaiseException, which means re-raising the original exception and let it propagate through the code. This gives an opportunity for some Sparkle middleware to catch the raise exception. If that doesn't happen, the exception will be handled by the Sparkle dispatcher.

The third option is simply choose Ignore. Use this option if you want to send a custom HTTP response yourself. In this case XData will simply do nothing and finish processing request silently.

 

  TModuleExceptionAction = (SendError, RaiseException, Ignore);