1

My API is returning a Byte array but if there's an error I need to return the status code and message but can't due to the return value is a Byte array. Where do I put the error message string?

public async Task<byte[]> GetPolicyPDF(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
        ILogger log, ExecutionContext context)
    {
        byte[] ret = new byte[1024];

        try
        {
            _log.LogInformation("C# HTTP trigger GetPolicyPDF processed a request.");

            string effectiveDate = req.Query["EffectiveDate"].ToString();
            string policyNumber = req.Query["PolicyNumber"].ToString();
            string type = req.Query["Type"].ToString();
            string contactId = req.Query["ContactID"].ToString();

            //var contactOwnPolicy = await DataverseHelper.DoesContactOwnPolicy(context, contactId, policyNumber, _log);
            //if (contactOwnPolicy == false)
            //    //return new ObjectResult($"Contact does not own Policy") { StatusCode = 500 };
            //    return ret;

            //Process the request - pass everything that is needed
            ret = await PolicyRequest.GetPolicyPDF(policyNumber, effectiveDate, type);

            return ret;
        }
        catch (Exception ex)
        {
            return ret;
            //return new ObjectResult($"{ex.Message}") { StatusCode = 500 };
        }
    }
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Darin
  • 119
  • 2
  • 12
  • Does this answer your question? [Azure Function, returning status code + JSON, without defining return in every part of logic](https://stackoverflow.com/questions/54858701/azure-function-returning-status-code-json-without-defining-return-in-every-p) Or this: https://stackoverflow.com/questions/67045378/using-iactionresult-with-azure-functions-in-net-5 ? – Daniel Mann Oct 26 '22 at 01:29

0 Answers0