Custom Error Response in WCF Data Services

In my DataService derrised class, I overridden the HandleException method to perform custom exception handling (simplified example):

    protected override void HandleException(HandleExceptionArgs args)
    {
       args.Exception = new DataServiceException(
            args.ResponseStatusCode,
            "RecordAlreadyExist",
            "The record with the given ID already exists in the database",
            System.Globalization.CultureInfo.CurrentCulture.Name,
            null);

        base.HandleException(args);
    }

This will result in something like the following JSON response:

    {
       "error": { 
             "code": "RecordAlreadyExist",
             "Message": {
                  "Language": "en-US",
                  "Message": "The record with the given ID already exists in the database"
             }
        }
     }

But I would like to get more information in the error response, for example:

    {
       "error": { 
             "code": "RecordAlreadyExist",
             "Message": {
                  "Language": "en-US",
                  "Message": "The record with the given ID already exists in the database"
             },
             "RecordID": "1234"
        }
     }

How can i do this? DataServiceException only supports error code and message ...

+3
source share

Source: https://habr.com/ru/post/1763850/


All Articles