0

i have a web api that has a response model when it gets results back it assigns the results to the values set as follows:

 public class ResponseHandler
    {

        public string id { get; set; }
        public double balance { get; set; }
        public string currency { get; set; }
        public int errorcode { get; set; }
        public string errorDescription { get; set; }

    }

and this response handler gets called in bellow:

public async Task<ResponseHandler> ExecuteAsync(CheckCommand command)
        {        
                var check = await _client.CheckAsync( command.id);
                _httpContextAccessor.HttpContext.Items["CommandResult"] = check;
                return new ResponseHandler
                {
                    id = check.ID.ToString(),
                    balance = decimal.ToDouble(check.AccountBalance),
                    currency = check.CurrencyCode,
                    errorcode = 0,
                    errorDescription = "null"

                };
        }

so basically if theres no error that comes back then i will assign the errocode to 0. So my question is how do i check if my request is successful then assign the errocode to 0 if its not successful then how do i assign that error to the error description

this is what i tried

        public async Task<ResponseHandler> ExecuteAsync(CheckCommand command)
                {        try{
                        var check = await _client.CheckAsync( command.id); // it fails here and doesnt even hit my catch method when it returns an error
                        _httpContextAccessor.HttpContext.Items["CommandResult"] = check;
                        return new ResponseHandler
                        {
                            id = check.ID.ToString(),
                            balance = decimal.ToDouble(check.AccountBalance),
                            currency = check.CurrencyCode,
                            errorcode = 0,
                            errorDescription = "null"

                        };
    }
    catch(exception ex){

    }
                }

how do i catch the error coming from CheckAsync

josh
  • 29
  • 5

1 Answers1

0

Declare a new ResponseHandler before try catch and set properties after CheckAsync ends correctly and in the catch if you get error.

Something like this:

public async Task<ResponseHandler> ExecuteAsync(CheckCommand command)
{
    ResponseHandler resp = new ResponseHandler();
    try
    {
        var check = await _client.CheckAsync(command.id); // it fails here and doesnt even hit my catch method when it returns an error
        _httpContextAccessor.HttpContext.Items["CommandResult"] = check;
        resp.id = check.ID.ToString();
        resp.balance = decimal.ToDouble(check.AccountBalance);
        resp.currency = check.CurrencyCode;
        resp.errorcode = 0;
        resp.errorDescription = "null";

    }
    catch (exception ex)
    {
        resp.id = "";
        resp.balance = 0;
        resp.currency = "";
        resp.errorcode = 1;
        resp.errorDescription = ex.StackTrace.toString();

    }
    return resp;
}
Ryozzo
  • 92
  • 8