Home > Back-end >  How to correctly catch and handle errors in Http get request to Facebook api
How to correctly catch and handle errors in Http get request to Facebook api

Time:12-15

I am trying to receive an access token from Facebook's graph API using the received code from Facebook. This is my first time working with APIs in .NET CORE. The code seems to work fine however I am not sure if I am handling the response and catching exceptions in the right way. Although I can use TRY CATCH still do not feel very comfortable in that. I would like to make this method as robust as it can be.

Your help is greatly appreciated.

Handling Class

public class FacebookService : IFacebookService
{
        private readonly HttpClient _httpClient;
        private readonly IConfiguration configuration;

        public FacebookService(HttpClient httpClient, IConfiguration iConfig)
        {
            _httpClient = httpClient;
            configuration = iConfig;
        }

        public async Task<string> GetShortLiveToken(string code)
        {
            try
            {
                string appId = configuration.GetSection("FacebookApp").GetSection("AppID").Value;
                string appSecret = configuration.GetSection("FacebookApp").GetSection("AppSecret").Value;
                var getTokenUri = $"oauth/access_token?client_id={appId}&client_secret={appSecret}&code={code}&redirect_uri=https://localhost:44373/Home/HandleFbAccess";
                var response = await _httpClient.GetAsync(getTokenUri);
                if (response.IsSuccessStatusCode)
                {
                    return await response.Content.ReadAsStringAsync();
                }
                else
                {
                    return response.ReasonPhrase;
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
        }

        public async Task<string> GetLongLiveToken(string shortlivedtoken)
        {
            throw new NotImplementedException();
        }

}

Invoking Method

public async Task<IActionResult> HandleFbAccess(string code, string granted_scopes)
{
    try
    {
        var result = await _facebookService.GetShortLiveToken(code);
        // more things to do here
        //.....
        return View("Index");

    }
    catch(Exception ex)
    {
        throw ex;
    }
    
}

CodePudding user response:

try{}catch(Exception ex){} worked when your code burst into exception, such as nullpointexception, but you are now worrying about the httpclient send request, you should know that this method only could throw these exceptions and all these exceptions are not related to facebook api.

enter image description here

My idea on your scenario is keeping using try catch here but you need to set operations according to the exception type in catch{} and write custom logic code here, to deal with http response such as 404,400,503 issues etc:

if (response.IsSuccessStatusCode){
     return await response.Content.ReadAsStringAsync();
}else{
    return response.ReasonPhrase;
}
  • Related