user2463514
user2463514

Reputation: 273

Best Practice in OData for exceptions

We are consuming OData service using dot-net.

When any exception throws in the odata service code or any business logic problem fails, service code handles that exception or error and return dotnet consumer a new error object with that error details.

Is it good way of doing as I am thinking it should throw an exception and at consuming end I should handle it in my own way.

What are your suggestions ?

Upvotes: 1

Views: 3474

Answers (2)

ThomasBecker
ThomasBecker

Reputation: 388

You can also try something like this also :

            try
            {
               //your Odata query and response code 
            }
            catch (DataServiceClientException dsce)
            {
                logger.WarnFormat("Client Exception, Status Code - {0}", dsce.StatusCode.ToString());
            }
            catch (DataServiceRequestException dsre)
            {
                logger.WarnFormat("Request Exception - {0}", dsre.Message);
            }
            catch (DataServiceQueryException dsqe)
            {
                logger.WarnFormat("Query Exception, Status code - {0}", dsqe.Response.StatusCode.ToString());
            }

Hope it helps :)

Upvotes: 1

Miroslav Popov
Miroslav Popov

Reputation: 3383

Both are OK. In both cases you need a specific manner to transfer the service status and error info:

// First case - the returned object contains status and error info.
IResponse response = OData.Serve();
if (response.Status == Status.Ok)
    ManageResponse(response );
else 
    ManageError(response.Status, response.Error);

// Second case - service rises an exception.
IResponse response;
try
{
    response = OData.Serve();
    ManageResponse(response);
}
catch (ODataException e)
{
    ManageError(e.Status, e.Error);
}

// Third case: Service returns correct response or null.
// In case of error Service contains error info.
IResponse response = OData.Serve();
if (response != null)
    ManageResponse(response);
else
    ManageError(OData.LastError);

Upvotes: 1

Related Questions