From 734b775d6acc02a46c437f1c1c5045a1b2cb1a44 Mon Sep 17 00:00:00 2001 From: Tommy Date: Thu, 25 Apr 2024 12:19:03 +0200 Subject: [PATCH] Restore custom status code detection message feature --- src/Dibix.Http.Host/Runtime/HttpActionDelegator.cs | 2 +- src/Dibix.Http.Server/Runtime/HttpActionInvoker.cs | 6 +++--- src/Dibix.Http.Server/Runtime/SqlHttpStatusCodeParser.cs | 7 +++++-- .../Dibix.Http.Server.Tests/HttpActionInvokerTest.Base.cs | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Dibix.Http.Host/Runtime/HttpActionDelegator.cs b/src/Dibix.Http.Host/Runtime/HttpActionDelegator.cs index dc7a2b9b..ca7f67d5 100644 --- a/src/Dibix.Http.Host/Runtime/HttpActionDelegator.cs +++ b/src/Dibix.Http.Host/Runtime/HttpActionDelegator.cs @@ -26,7 +26,7 @@ public async Task Delegate(HttpContext httpContext, IDictionary IHttpResponseFormatter responseFormatter = new HttpResponseFormatter(httpContext.Response); try { - _ = await HttpActionInvoker.Invoke(actionDefinition, new HttpRequestDescriptor(httpContext.Request), responseFormatter, arguments, _parameterDependencyResolver, parseSqlHttpStatusCodeExceptions: false, cancellationToken).ConfigureAwait(false); + _ = await HttpActionInvoker.Invoke(actionDefinition, new HttpRequestDescriptor(httpContext.Request), responseFormatter, arguments, _parameterDependencyResolver, cancellationToken).ConfigureAwait(false); } catch (HttpRequestExecutionException httpRequestExecutionException) { diff --git a/src/Dibix.Http.Server/Runtime/HttpActionInvoker.cs b/src/Dibix.Http.Server/Runtime/HttpActionInvoker.cs index 13fec8ce..e2a096f1 100644 --- a/src/Dibix.Http.Server/Runtime/HttpActionInvoker.cs +++ b/src/Dibix.Http.Server/Runtime/HttpActionInvoker.cs @@ -10,9 +10,9 @@ public static class HttpActionInvoker public static Task Invoke(HttpActionDefinition action, HttpRequestMessage request, IDictionary arguments, IParameterDependencyResolver parameterDependencyResolver, CancellationToken cancellationToken) { IHttpResponseFormatter responseFormatter = new HttpResponseMessageFormatter(); - return Invoke(action, new HttpRequestMessageDescriptor(request), responseFormatter, arguments, parameterDependencyResolver, parseSqlHttpStatusCodeExceptions: true, cancellationToken); + return Invoke(action, new HttpRequestMessageDescriptor(request), responseFormatter, arguments, parameterDependencyResolver, cancellationToken); } - public static async Task Invoke(HttpActionDefinition action, TRequest request, IHttpResponseFormatter responseFormatter, IDictionary arguments, IParameterDependencyResolver parameterDependencyResolver, bool parseSqlHttpStatusCodeExceptions, CancellationToken cancellationToken) where TRequest : IHttpRequestDescriptor + public static async Task Invoke(HttpActionDefinition action, TRequest request, IHttpResponseFormatter responseFormatter, IDictionary arguments, IParameterDependencyResolver parameterDependencyResolver, CancellationToken cancellationToken) where TRequest : IHttpRequestDescriptor { try { @@ -25,7 +25,7 @@ public static async Task Invoke(HttpActionDefinition action, T object formattedResult = await responseFormatter.Format(result, request, action, cancellationToken).ConfigureAwait(false); return formattedResult; } - catch (DatabaseAccessException exception) when (parseSqlHttpStatusCodeExceptions) + catch (DatabaseAccessException exception) { // Sample: // THROW 404017, N'Feature not configured', 1 diff --git a/src/Dibix.Http.Server/Runtime/SqlHttpStatusCodeParser.cs b/src/Dibix.Http.Server/Runtime/SqlHttpStatusCodeParser.cs index a454a487..7bccde55 100644 --- a/src/Dibix.Http.Server/Runtime/SqlHttpStatusCodeParser.cs +++ b/src/Dibix.Http.Server/Runtime/SqlHttpStatusCodeParser.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Data.SqlClient; +using System.Linq; using System.Net; using System.Text.RegularExpressions; @@ -30,10 +32,11 @@ private static bool TryParse(DatabaseAccessException originalException, SqlExce error = userResponse; isClientError = HttpErrorResponseUtility.IsClientError(error.StatusCode); + IDictionary caseInsensitiveArguments = new Dictionary(arguments, StringComparer.OrdinalIgnoreCase); string formattedErrorMessage = Regex.Replace(error.ErrorMessage, "{(?[^}]+)}", x => { string parameterName = x.Groups["ParameterName"].Value; - return arguments.TryGetValue(parameterName, out object value) ? value?.ToString() : x.Value; + return caseInsensitiveArguments.TryGetValue(parameterName, out object value) ? value?.ToString() : x.Value; }); httpException = new HttpRequestExecutionException((HttpStatusCode)error.StatusCode, error.ErrorCode, formattedErrorMessage, isClientError, originalException); return true; diff --git a/tests/Dibix.Http.Server.Tests/HttpActionInvokerTest.Base.cs b/tests/Dibix.Http.Server.Tests/HttpActionInvokerTest.Base.cs index 7b8ed97a..1afc96b0 100644 --- a/tests/Dibix.Http.Server.Tests/HttpActionInvokerTest.Base.cs +++ b/tests/Dibix.Http.Server.Tests/HttpActionInvokerTest.Base.cs @@ -53,7 +53,7 @@ private static async Task Execute(HttpActionDefinition action, foreach (KeyValuePair parameter in parameters) arguments.Add(parameter); - object result = await HttpActionInvoker.Invoke(action, request, responseFormatter, arguments, parameterDependencyResolver.Object, parseSqlHttpStatusCodeExceptions: true, default).ConfigureAwait(false); + object result = await HttpActionInvoker.Invoke(action, request, responseFormatter, arguments, parameterDependencyResolver.Object, default).ConfigureAwait(false); return result; }