Link to home
Start Free TrialLog in
Avatar of Robb Hill
Robb HillFlag for United States of America

asked on

Help catching error on api response protocolerror .net 6

Open in new window

I have an api converted to .net 6.   this api makes calls to a db and makes a post to another solution to generate dashboards.   Within the API randomly I get in the response of some of the dashboard cards a response body of "protocolError"   The payload is there and I get the proper 200 return.  But the CARD in the UI will not display any data.  I am  trying to somehow log this with more info.   Here is the controller and service:  I have not put any try catch for fear that would mess up the async calls.

Open in new window

The post is the one in question:
using System.Threading.Tasks;
using DataQueryApi.Queries;
using DataQueryApi.Repository;
using DataQueryApi.Services;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;


namespace DataQueryApi.Controllers
{
    [Route("v1/reportdata/")]
    [ApiController]
    [ProducesResponseType(200)]
    [ProducesResponseType(400)]
    public class ReportDataController : ControllerBase
    {
        readonly ReportDataService reportDataService;
        readonly CompanyDataSecurityService _companyDataSecurityService;


        public ReportDataController(ReportDataService reportDataService
            ,AppMetricsService appMetricsService
            ,CompanyDataSecurityService companyDataSecurityService)
        {
            this.reportDataService = reportDataService;
            _companyDataSecurityService = companyDataSecurityService;
        }


        [HttpPost]
        public async Task<ActionResult<string>> ReportDataById([FromBody] ReportQuery requestQuery)
        {
            bool validated = await _companyDataSecurityService.ValidateCompanyData(requestQuery.CompanyId, requestQuery.CompanyUID);

Open in new window

            if (validated)
            {
                return await reportDataService.GetReportDataJson(requestQuery);
            }
            else
            {
                return "Invalid company data. We are not able to process this request.";
            }
        }
    }
}

Open in new window


using DataQueryApi.Queries;
using DataQueryApi.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;


namespace DataQueryApi.Services
{
    public class ReportDataService
    {
        readonly ReportDataRepository reportDataRepository;
        readonly AppMetricsService appMetricsService;


        public ReportDataService(ReportDataRepository reportDataRepository, AppMetricsService appMetricsService)
        {
            this.reportDataRepository = reportDataRepository;
            this.appMetricsService = appMetricsService;
        }


        public async Task<string> GetReportDataJson(ReportQuery query)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();


            string reportData = string.Empty;


            Task task = (Task)typeof(ReportDataRepository).GetMethod(query.ReportId).Invoke(reportDataRepository, new[] { query });
            await task.ConfigureAwait(false);
            var resultProperty = task.GetType().GetProperty("Result");


            stopwatch.Stop();


            appMetricsService.RecordReportDataRequest(query.ReportId);
            appMetricsService.RecordReportDataTime(query.ReportId, stopwatch.ElapsedMilliseconds);


            return JsonConvert.SerializeObject(resultProperty.GetValue(task));
        }
    }
}

Open in new window


Avatar of dfke
dfke

Hi,

It looks like there could be an issue with the way the API is communicating with the dashboard card. The "protocolError" response suggests there might be a problem with the format or content of the payload being sent to the dashboard card.

To troubleshoot this issue, you can try adding try-catch blocks around the relevant code in the ReportDataController and ReportDataService classes. This will allow you to catch any exceptions that may be occurring and log more information about the error, such as the specific exception message and stack trace. You can also add some logging statements in the ReportDataService.GetReportDataJson method, before and after the post call is made to the dashboard card, to see what the payload is and if it is the same when the "protocolError" is occurring. Or check the response returned by the dashboard card in more detail, to see if there is more information about the error. Another thing you could do is check the request and response headers to ensure that they are properly formatted and contain the appropriate information. That and you can also check the logs of the API and the dashboard card to see if they contain any more information.

Cheers
Avatar of Robb Hill

ASKER

@dfke

Thanks for the response.   Can you show me where you would put this.  The async has me questioning how to catch this and not mess up those calls.   I will need to see this in production but not to the client.  I would assume this would log to console or we use AWS cloudwatch.  
SOLUTION
Avatar of dfke
dfke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!! did you mean to post the same twice or would you suggest doing the task as well?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial