Skip to content

Commit

Permalink
Merged PR 865991: Stopping a stoppedModule should not throw
Browse files Browse the repository at this point in the history
- Swallowing the exception thrown by NSwag generated code when server returns Http Code < 400.
- Not making the change in the generated code to allow updating the generated code easily, and to make the change generic.
- Note that i have updated the NSwag generated "server" code manually to be able to test this change properly.
  • Loading branch information
varunpuranik committed May 30, 2018
1 parent 25a677d commit 2eea0f8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async Task<T> Execute<T>(Func<Task<T>> func, string operation)
try
{
Events.ExecutingOperation(operation, this.managementUri.ToString());
T result = await ExecuteWithRetry(func, (r) => Events.RetryingOperation(operation, this.managementUri.ToString(), r));
T result = await ExecuteWithRetry(func, r => Events.RetryingOperation(operation, this.managementUri.ToString(), r));
Events.SuccessfullyExecutedOperation(operation, this.managementUri.ToString());
return result;
}
Expand All @@ -166,9 +166,18 @@ async Task<T> Execute<T>(Func<Task<T>> func, string operation)
switch (ex)
{
case SwaggerException<ErrorResponse> errorResponseException:
throw new EdgeletCommunicationException($"Error calling {operation}: {errorResponseException.Result?.Message ?? string.Empty}", errorResponseException.StatusCode);
throw new EdgeletCommunicationException($"Error calling {operation}: {errorResponseException.Result?.Message ?? string.Empty}", errorResponseException.StatusCode);

case SwaggerException swaggerException:
throw new EdgeletCommunicationException($"Error calling {operation}: {swaggerException.Response ?? string.Empty}", swaggerException.StatusCode);
if (swaggerException.StatusCode < 400)
{
Events.SuccessfullyExecutedOperation(operation, this.managementUri.ToString());
return default(T);
}
else
{
throw new EdgeletCommunicationException($"Error calling {operation}: {swaggerException.Response ?? string.Empty}", swaggerException.StatusCode);
}
default:
throw;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//----------------------
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v11.17.10.0 (NJsonSchema v9.10.49.0 (Newtonsoft.Json v9.0.0.0)) (http://NSwag.org)
// </auto-generated>
Expand Down Expand Up @@ -847,7 +847,7 @@ public async System.Threading.Tasks.Task RestartModuleAsync(string api_version,
ProcessResponse(client_, response_);

var status_ = ((int)response_.StatusCode).ToString();
if (status_ == "204" || status_ == "204")
if (status_ == "200" || status_ == "204")
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public async Task ModulesTest()
Assert.NotNull(moduleDetails);
Assert.Equal("Stopped", moduleDetails.Status.RuntimeStatus.Status);

// Act - Stopping a stopped module should not throw
await client.StopModuleAsync(moduleSpec.Name);
moduleDetails = (await client.GetModules(CancellationToken.None)).FirstOrDefault();

// Assert
Assert.NotNull(moduleDetails);
Assert.Equal("Stopped", moduleDetails.Status.RuntimeStatus.Status);

// Act
await client.DeleteModuleAsync(moduleSpec.Name);
moduleDetails = (await client.GetModules(CancellationToken.None)).FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//----------------------
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v11.17.10.0 (NJsonSchema v9.10.49.0 (Newtonsoft.Json v9.0.0.0)) (http://NSwag.org)
// </auto-generated>
Expand Down Expand Up @@ -51,8 +51,8 @@ public interface IController
/// <summary>Stop a module.</summary>
/// <param name="api_version">The version of the API.</param>
/// <param name="name">The name of the module to stop. (urlencoded)</param>
/// <returns>No Content</returns>
System.Threading.Tasks.Task StopModuleAsync(string api_version, string name);
/// <returns>Status code</returns>
System.Threading.Tasks.Task<int> StopModuleAsync(string api_version, string name);

/// <summary>Restart a module.</summary>
/// <param name="api_version">The version of the API.</param>
Expand Down Expand Up @@ -161,9 +161,10 @@ public System.Threading.Tasks.Task StartModule(string api_version, string name)
/// <param name="name">The name of the module to stop. (urlencoded)</param>
/// <returns>No Content</returns>
[Microsoft.AspNetCore.Mvc.HttpPost, Microsoft.AspNetCore.Mvc.Route("modules/{name}/stop")]
public System.Threading.Tasks.Task StopModule(string api_version, string name)
public async System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.IActionResult> StopModule(string api_version, string name)
{
return _implementation.StopModuleAsync(api_version, name);
int statusCode = await _implementation.StopModuleAsync(api_version, name);
return StatusCode(statusCode);
}

/// <summary>Restart a module.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,21 @@ public Task StartModuleAsync(string apiVersion, string name)
return Task.CompletedTask;
}

public Task StopModuleAsync(string apiVersion, string name)
public Task<int> StopModuleAsync(string apiVersion, string name)
{
if (!this.modules.TryGetValue(name, out ModuleDetails module))
{
throw new InvalidOperationException("Module not found");
}
module.Status.RuntimeStatus.Status = "Stopped";
return Task.CompletedTask;
if (module.Status.RuntimeStatus.Status == "Stopped")
{
return Task.FromResult(304);
}
else
{
module.Status.RuntimeStatus.Status = "Stopped";
return Task.FromResult(204);
}
}

public Task<ModuleDetails> UpdateModuleAsync(string apiVersion, string name, bool start, ModuleSpec module)
Expand Down

0 comments on commit 2eea0f8

Please sign in to comment.