Skip to content

Commit

Permalink
Merge pull request #2233 from jaroslavbliznak/bugfix/2141-Wrong-login…
Browse files Browse the repository at this point in the history
…-message-on-servers

FIX-2141 Wrong login error message on some servers
  • Loading branch information
jaroslavbliznak authored Feb 6, 2024
2 parents ed3d393 + 59da417 commit 9f47172
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Src/Witsml/EndpointBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Net;
using System.Net.Http;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Security;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -28,10 +30,20 @@ public CustomDelegatingHandler(HttpMessageHandler handler)
InnerHandler = handler;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("user-agent", "witsml-explorer");
return base.SendAsync(request, cancellationToken);
var response = await base.SendAsync(request, cancellationToken);

switch (response.StatusCode)
{
case HttpStatusCode.InternalServerError:
throw new WitsmlRemoteServerRequestCrashedException("WITSML remote request failed on the server.");
case HttpStatusCode.Unauthorized:
case HttpStatusCode.Forbidden:
throw new MessageSecurityException("Not able to authenticate to WITSML server with given credentials");
}
return response;
}
}
}
11 changes: 11 additions & 0 deletions Src/Witsml/WitsmlRemoteServerRequestCrashedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ServiceModel;

namespace Witsml;

/// <summary>
/// This class represents a custom exception for cases where a WITSML remote server request crash.
/// </summary>
public class WitsmlRemoteServerRequestCrashedException : CommunicationException
{
public WitsmlRemoteServerRequestCrashedException(string message) : base(message) { }
}
12 changes: 12 additions & 0 deletions Src/WitsmlExplorer.Api/Middleware/ExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

using Serilog;

using Witsml;

using WitsmlExplorer.Api.Configuration;
using WitsmlExplorer.Api.Extensions;
using WitsmlExplorer.Api.HttpHandlers;
Expand Down Expand Up @@ -51,6 +53,16 @@ public async Task InvokeAsync(HttpContext httpContext)
};
await HandleExceptionAsync(httpContext, errorDetails);
}
catch (WitsmlRemoteServerRequestCrashedException ex)
{
Log.Error($"Invalid response from server: {ex}");
ErrorDetails errorDetails = new()
{
StatusCode = (int)HttpStatusCode.InternalServerError,
Message = "WITSML remote request failed on the server."
};
await HandleExceptionAsync(httpContext, errorDetails);
}
catch (MessageSecurityException ex)
{
Log.Debug($"Not valid credentials: {ex}");
Expand Down

0 comments on commit 9f47172

Please sign in to comment.