Skip to content

Commit

Permalink
chore: better error handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welker committed Oct 15, 2024
1 parent 3cafe91 commit 3e8ec54
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,54 @@ public MobileControlApiService(string apiUrl)

public async Task<AuthorizationResponse> SendAuthorizationRequest(string apiUrl, string grantCode, string systemUuid)
{
var request = new HttpRequestMessage(HttpMethod.Get, $"{apiUrl}/system/{systemUuid}/authorize?grantCode={grantCode}");
try
{
var request = new HttpRequestMessage(HttpMethod.Get, $"{apiUrl}/system/{systemUuid}/authorize?grantCode={grantCode}");

Debug.Console(1, $"Sending authorization request to {request.RequestUri}");
Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Sending authorization request to {host}", null, request.RequestUri);

var response = await _client.SendAsync(request);
var response = await _client.SendAsync(request);

var authResponse = new AuthorizationResponse
{
Authorized = response.StatusCode == System.Net.HttpStatusCode.OK
};
var authResponse = new AuthorizationResponse
{
Authorized = response.StatusCode == System.Net.HttpStatusCode.OK
};

if (authResponse.Authorized)
{
return authResponse;
}
if (authResponse.Authorized)
{
return authResponse;
}

if (response.StatusCode == System.Net.HttpStatusCode.Moved)
{
var location = response.Headers.Location;
if (response.StatusCode == System.Net.HttpStatusCode.Moved)
{
var location = response.Headers.Location;

authResponse.Reason = $"ERROR: Mobile Control API has moved. Please adjust configuration to \"{location}\"";
authResponse.Reason = $"ERROR: Mobile Control API has moved. Please adjust configuration to \"{location}\"";

return authResponse;
}
return authResponse;
}

var responseString = await response.Content.ReadAsStringAsync();
var responseString = await response.Content.ReadAsStringAsync();

switch (responseString)
switch (responseString)
{
case "codeNotFound":
authResponse.Reason = $"Authorization failed. Code not found for system UUID {systemUuid}";
break;
case "uuidNotFound":
authResponse.Reason = $"Authorization failed. System UUID {systemUuid} not found. Check Essentials configuration.";
break;
default:
authResponse.Reason = $"Authorization failed. Response {response.StatusCode}: {responseString}";
break;
}

return authResponse;
} catch(Exception ex)
{
case "codeNotFound":
authResponse.Reason = $"Authorization failed. Code not found for system UUID {systemUuid}";
break;
case "uuidNotFound":
authResponse.Reason = $"Authorization failed. System UUID {systemUuid} not found. Check Essentials configuration.";
break;
default:
authResponse.Reason = $"Authorization failed. Response {response.StatusCode}: {responseString}";
break;
Debug.LogMessage(ex, "Error authorizing with Mobile Control");
return new AuthorizationResponse { Authorized = false, Reason = ex.Message };
}

return authResponse;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ public MobileAuthRequestHandler(MobileControlSystemController controller) : base

protected override async Task HandlePost(HttpCwsContext context)
{
var requestBody = EssentialsWebApiHelpers.GetRequestBody(context.Request);
try
{
var requestBody = EssentialsWebApiHelpers.GetRequestBody(context.Request);

var grantCode = JsonConvert.DeserializeObject<AuthorizationRequest>(requestBody);
var grantCode = JsonConvert.DeserializeObject<AuthorizationRequest>(requestBody);

if (string.IsNullOrEmpty(grantCode?.GrantCode))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Missing grant code";
context.Response.End();
return;
}
if (string.IsNullOrEmpty(grantCode?.GrantCode))
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Error, "Missing grant code");
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Missing grant code";
context.Response.End();
return;
}

var response = await mcController.ApiService.SendAuthorizationRequest(mcController.Host, grantCode.GrantCode, mcController.SystemUuid);
var response = await mcController.ApiService.SendAuthorizationRequest(mcController.Host, grantCode.GrantCode, mcController.SystemUuid);

Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, $"response received");
if (response.Authorized)
{
mcController.RegisterSystemToServer();
}

Debug.Console(1, $"response received");
if (response.Authorized)
{
mcController.RegisterSystemToServer();
}

try
{
context.Response.StatusCode = 200;
var responseBody = JsonConvert.SerializeObject(response, Formatting.None);
context.Response.ContentType = "application/json";
Expand All @@ -50,14 +52,7 @@ protected override async Task HandlePost(HttpCwsContext context)
}
catch (Exception ex)
{
Debug.Console(0, $"Exception handling MC Auth request: {ex.Message}");
Debug.Console(2, $"Stack Trace: {ex.StackTrace}");

if (ex.InnerException != null)
{
Debug.Console(0, $"Inner Exception: {ex.InnerException.Message}");
Debug.Console(2, $"Inner Exception Stack Trace: {ex.InnerException.StackTrace}");
}
Debug.LogMessage(ex, "Exception recieved authorizing system");
}
}
}
Expand Down

0 comments on commit 3e8ec54

Please sign in to comment.