/// <inheritdoc />
        public HttpErrorBodyResult GetHttpErrorBodyResult(
            HttpStatusCode httpStatusCode,
            int errorIdentifierInt,
            params string[] messageArguments)
        {
            HttpErrorBodyResult toReturn = null;

            string errorIdentifier = string.Format(
                CultureInfo.InvariantCulture,
                ErrorIdentifierFormat,
                this.systemErrorIdentifier,
                errorIdentifierInt);

            string name = string.Format(
                CultureInfo.InvariantCulture,
                ResourceIdentifierFormat,
                errorIdentifierInt);

            string message = this.resourceManager.GetString(
                name,
                CultureInfo.InvariantCulture);

            message = string.Format(
                CultureInfo.InvariantCulture,
                message,
                messageArguments);

            toReturn = new HttpErrorBodyResult(
                httpStatusCode,
                errorIdentifier,
                message);

            return(toReturn);
        }
Exemplo n.º 2
0
        /// <summary>
        /// A base implementation of the function's entry point method. This
        /// should be over-ridden and the base implementation called from
        /// within the over-ridden implementation.
        /// </summary>
        /// <param name="httpRequest">
        /// An instance of <see cref="HttpRequest" />.
        /// </param>
        /// <param name="runContext">
        /// An instance of <see cref="FunctionRunContext" />.
        /// </param>
        /// <param name="cancellationToken">
        /// An instance of <see cref="CancellationToken" />.
        /// </param>
        /// <returns>
        /// An instance of type <see cref="IActionResult" />.
        /// </returns>
        public async Task <IActionResult> ValidateAndRunAsync(
            HttpRequest httpRequest,
            FunctionRunContext runContext,
            CancellationToken cancellationToken)
        {
            IActionResult toReturn = null;

            if (httpRequest == null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

            this.httpSpiExecutionContextManager.SetContext(
                httpRequest.Headers);

            TRequest request = null;

            try
            {
                request =
                    await this.ParseAndValidateRequestAsync(
                        httpRequest)
                    .ConfigureAwait(false);
            }
            catch (JsonReaderException jsonReaderException)
            {
                this.loggerWrapper.Info(
                    $"A {nameof(JsonReaderException)} was thrown during the " +
                    $"parsing of the body of the request.",
                    jsonReaderException);

                toReturn = this.GetMalformedErrorResponse(runContext);
            }
            catch (JsonSchemaValidationException jsonSchemaValidationException)
            {
                this.loggerWrapper.Info(
                    $"A {nameof(JsonSchemaValidationException)} was thrown " +
                    $"during the parsing of the body of the request.",
                    jsonSchemaValidationException);

                toReturn = this.GetSchemaValidationResponse(jsonSchemaValidationException, runContext);
            }

            if (request != null)
            {
                // The JSON is valid and not null, but at this point, it's
                // unknown if its valid according to the *schema*.
                toReturn = await this.ProcessWellFormedRequestAsync(
                    request,
                    runContext,
                    cancellationToken)
                           .ConfigureAwait(false);
            }

            if (toReturn is HttpErrorBodyResult)
            {
                HttpErrorBodyResult httpErrorBodyResult =
                    (HttpErrorBodyResult)toReturn;

                object value = httpErrorBodyResult.Value;

                this.loggerWrapper.Info(
                    $"This HTTP request failed. Returning: {value}.");
            }

            return(toReturn);
        }