Skip to content

Commit

Permalink
Handle Error Body in Responses from Broker (#3454)
Browse files Browse the repository at this point in the history
  • Loading branch information
luketomlinson authored Sep 5, 2024
1 parent 65764d9 commit 4c0a43f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Runner.Common/BrokerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Task ForceRefreshConnection(VssCredentials credentials)

public bool ShouldRetryException(Exception ex)
{
if (ex is AccessDeniedException ade && ade.ErrorCode == 1)
if (ex is AccessDeniedException ade)
{
return false;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Sdk/RSWebApi/Contracts/BrokerError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;

namespace GitHub.Actions.RunService.WebApi
{
[DataContract]
public class BrokerError
{
[DataMember(Name = "source", EmitDefaultValue = false)]
public string Source { get; set; }

[DataMember(Name = "errorKind", EmitDefaultValue = false)]
public string ErrorKind { get; set; }

[DataMember(Name = "statusCode", EmitDefaultValue = false)]
public int StatusCode { get; set; }

[DataMember(Name = "errorMessage", EmitDefaultValue = false)]
public string Message { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Sdk/RSWebApi/Contracts/BrokerErrorKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Runtime.Serialization;

namespace GitHub.Actions.RunService.WebApi
{
[DataContract]
public class BrokerErrorKind
{
public const string RunnerVersionTooOld = "RunnerVersionTooOld";
}
}
41 changes: 38 additions & 3 deletions src/Sdk/WebApi/WebApi/BrokerHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,29 @@ public async Task<TaskAgentMessage> GetRunnerMessageAsync(
new HttpMethod("GET"),
requestUri: requestUri,
queryParameters: queryParams,
readErrorBody: true,
cancellationToken: cancellationToken);

if (result.IsSuccess)
{
return result.Value;
}

// the only time we throw a `Forbidden` exception from Listener /messages is when the runner is
// disable_update and is too old to poll
if (TryParseErrorBody(result.ErrorBody, out BrokerError brokerError))
{
switch (brokerError.ErrorKind)
{
case BrokerErrorKind.RunnerVersionTooOld:
throw new AccessDeniedException(brokerError.Message)
{
ErrorCode = 1
};
default:
break;
}
}

// temporary back compat
if (result.StatusCode == HttpStatusCode.Forbidden)
{
throw new AccessDeniedException($"{result.Error} Runner version v{runnerVersion} is deprecated and cannot receive messages.")
Expand All @@ -120,7 +134,7 @@ public async Task<TaskAgentMessage> GetRunnerMessageAsync(
};
}

throw new Exception($"Failed to get job message: {result.Error}");
throw new Exception($"Failed to get job message. Request to {requestUri} failed with status: {result.StatusCode}. Error message {result.Error}");
}

public async Task<TaskAgentSession> CreateSessionAsync(
Expand Down Expand Up @@ -172,5 +186,26 @@ public async Task DeleteSessionAsync(

throw new Exception($"Failed to delete broker session: {result.Error}");
}

private static bool TryParseErrorBody(string errorBody, out BrokerError error)
{
if (!string.IsNullOrEmpty(errorBody))
{
try
{
error = JsonUtility.FromString<BrokerError>(errorBody);
if (error?.Source == "actions-broker-listener")
{
return true;
}
}
catch (Exception)
{
}
}

error = null;
return false;
}
}
}

0 comments on commit 4c0a43f

Please sign in to comment.