Robb Hill
asked on
Help catching error on api response protocolerror .net 6
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.
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);
if (validated)
{
return await reportDataService.GetReportDataJson(requestQuery);
}
else
{
return "Invalid company data. We are not able to process this request.";
}
}
}
}
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));
}
}
}
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!! did you mean to post the same twice or would you suggest doing the task as well?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.GetRepor
Cheers