0

I am trying to send the proper response from Web API i.e. If any error send error else send the content. The code flow for the same is as follow.

 [HttpPost]
        public IActionResult GetInfo([FromBody] InfoModel info)
        {
            try
            {
                var result = new Info().ProcessInfoResponse(info);
                if (result==null)
                    return BadRequest();
                else
                    return Ok(result);

            }
            catch (Exception e)
            {
                Log.Error("some exception", e);
                return StatusCode(500, e.Message);
            }
        }

and from middle layer i.e. from Info class we are having different method with there own returning type and from here we are calling the third party APIs which are in another class.

public InfoResponse ProcessInfoResponse(InfoModel info)
{
    try
        {
            var result = serviceLayer.Post<InfoModel>(info);

            if (result != null)
            {
               // Do Something
            }
            else
            {
                Log.Error("some error");
                return null;
            }
        }
        catch (Exception ex)
        {
            Log.Error("some error");
            return null;
        }
}

public InfoRequest ProcessInfoRequest()
{

}

And in service layer we are calling the third party api like below

public HttpResponseMessage Post<T>(T parm) where T : class
        {
            try
            {
                _client.DefaultRequestHeaders.Clear();

                var postTask = _client.PostAsync("some third party url", Serialize<T>(parm));
                postTask.Wait();

                if (postTask.Result.IsSuccessStatusCode)
                {
                    return postTask.Result;
                }
                else
                {
                    Log.Error("some error in service layer");
                }
            }
            catch (Exception ex)
            {
                Log.Error("some error in service layer");
            }

            return default(HttpResponseMessage);
        }

So my question is how can return exceptions/errors if there are any and if there is no exceptions/error then send the response as it is. This is possible by keeping middle layer returning type as is

Right now if there are no errors then I am able to send the response properly, as my middle layer is getting the expected returning type.

The issue is my middle layer methods has own returning type which is causing me to send the exception/error as is. Because I am not able to map it to proper class OR same class.

I was thinking will add new Property under all returning classes/types which will refer to the exception class, then will bind the exception/error details to that class. This will save doing lot of code changes in all places.

Any help on this appreciated !

1 Answer 1

1

Why not create a custom response object so that:

public IActionResult<MyCustomResponseObject> GetInfo([FromBody] InfoModel info)

public class MyCustomResponseObject
{
    public string Message { get; set; }
    public object Content { get; set; }
    public enum State { get; set; }
}
Sign up to request clarification or add additional context in comments.

6 Comments

So you are saying i need to return MyCustomResponseObject from all methods ?
Yes. This is a strategy I have used and makes API consumption uniform and easy to implement.
Yeah, But we have implemented the methods through single interface in multiple middle layer classes. So this will result in lot of code changes. Where the methods has different returning types.
Ok, but aren't you asking how to return Content and/or Error responses? If so, there is no other way but to have a return model that can accommodate those values.
Yes I am asking the same thing, to avoid lot of code changes I can add that particular Exception class in all returning types as a property OR by inheriting it. This makes sense I guess
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.