Skip to content

Commit

Permalink
Restore custom status code detection message feature
Browse files Browse the repository at this point in the history
  • Loading branch information
C0nquistadore committed Apr 25, 2024
1 parent e70d31d commit 734b775
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Dibix.Http.Host/Runtime/HttpActionDelegator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task Delegate(HttpContext httpContext, IDictionary<string, object>
IHttpResponseFormatter<HttpRequestDescriptor> 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)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Dibix.Http.Server/Runtime/HttpActionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public static class HttpActionInvoker
public static Task<object> Invoke(HttpActionDefinition action, HttpRequestMessage request, IDictionary<string, object> arguments, IParameterDependencyResolver parameterDependencyResolver, CancellationToken cancellationToken)
{
IHttpResponseFormatter<HttpRequestMessageDescriptor> 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<object> Invoke<TRequest>(HttpActionDefinition action, TRequest request, IHttpResponseFormatter<TRequest> responseFormatter, IDictionary<string, object> arguments, IParameterDependencyResolver parameterDependencyResolver, bool parseSqlHttpStatusCodeExceptions, CancellationToken cancellationToken) where TRequest : IHttpRequestDescriptor
public static async Task<object> Invoke<TRequest>(HttpActionDefinition action, TRequest request, IHttpResponseFormatter<TRequest> responseFormatter, IDictionary<string, object> arguments, IParameterDependencyResolver parameterDependencyResolver, CancellationToken cancellationToken) where TRequest : IHttpRequestDescriptor
{
try
{
Expand All @@ -25,7 +25,7 @@ public static async Task<object> Invoke<TRequest>(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
Expand Down
7 changes: 5 additions & 2 deletions src/Dibix.Http.Server/Runtime/SqlHttpStatusCodeParser.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -30,10 +32,11 @@ private static bool TryParse(DatabaseAccessException originalException, SqlExce
error = userResponse;

isClientError = HttpErrorResponseUtility.IsClientError(error.StatusCode);
IDictionary<string, object> caseInsensitiveArguments = new Dictionary<string, object>(arguments, StringComparer.OrdinalIgnoreCase);
string formattedErrorMessage = Regex.Replace(error.ErrorMessage, "{(?<ParameterName>[^}]+)}", 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static async Task<object> Execute<TRequest>(HttpActionDefinition action,
foreach (KeyValuePair<string, object> 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;
}

Expand Down

0 comments on commit 734b775

Please sign in to comment.