Skip to content

Commit

Permalink
Merge pull request #714 from aws/dev
Browse files Browse the repository at this point in the history
chore: release 1.5
  • Loading branch information
philasmar authored Oct 6, 2022
2 parents 4f04da1 + a7ffa90 commit c1bf3d1
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,14 @@ public IActionResult GetDeploymentStatus(string sessionId)
if (state.DeploymentTask.Exception != null)
{
var innerException = state.DeploymentTask.Exception.InnerException;
var message = innerException.GetTruncatedErrorMessage();
if (innerException is DeployToolException deployToolException)
{
output.Exception = new DeployToolExceptionSummary(deployToolException.ErrorCode.ToString(), deployToolException.Message, deployToolException.ProcessExitCode);
output.Exception = new DeployToolExceptionSummary(deployToolException.ErrorCode.ToString(), message, deployToolException.ProcessExitCode);
}
else
{
output.Exception = new DeployToolExceptionSummary(DeployToolErrorCode.UnexpectedError.ToString(), innerException?.Message ?? string.Empty);
output.Exception = new DeployToolExceptionSummary(DeployToolErrorCode.UnexpectedError.ToString(), message);
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/AWS.Deploy.Common/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public enum DeployToolErrorCode
ECRRepositoryNameIsNull = 10010300,
FailedToReadCdkBootstrapVersion = 10010400,
UnsupportedOptionSettingType = 10010500,
FailedToSaveDeploymentSettings = 10010600
FailedToSaveDeploymentSettings = 10010600,
InvalidWindowsManifestFile = 10010700
}

public class ProjectFileNotFoundException : DeployToolException
Expand Down Expand Up @@ -318,5 +319,27 @@ public static string PrettyPrint(this Exception? e)

return $"{Environment.NewLine}{e.Message}{Environment.NewLine}{e.StackTrace}{PrettyPrint(e.InnerException)}";
}

/// <summary>
/// Returns the truncated error message by only preserving the leading and trailing k characters.
/// The message will only be truncated if its length is greater than 2*k.
/// </summary>
/// <param name="numChars">Species the number of leading and trailing characters to preserve.</param>
/// <returns>The truncated error message or <see cref="string.Empty"/> if the error message is null or empty.</returns>
public static string GetTruncatedErrorMessage(this Exception? e, int numChars = 500)
{
var message = e?.Message;

if (string.IsNullOrEmpty(message))
return string.Empty;

if (message.Length <= 2*numChars)
return message;

var firstKChars = message.Substring(0, numChars);
var lastkChars = message.Substring(message.Length - numChars);
var seperator = $"{Environment.NewLine}...{Environment.NewLine}Error truncated to the first and last {numChars} characters{Environment.NewLine}...{Environment.NewLine}";
return $"{firstKChars}{seperator}{lastkChars}";
}
}
}
8 changes: 8 additions & 0 deletions src/AWS.Deploy.Orchestration/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,12 @@ public class InvalidDeployToolWorkspaceException : DeployToolException
{
public InvalidDeployToolWorkspaceException(DeployToolErrorCode errorCode, string message, Exception? innerException = null) : base(errorCode, message, innerException) { }
}

/// <summary>
/// Throw if the deploy tool detects an invalid windows elastic beanstalk manifest file.
/// </summary>
public class InvalidWindowsManifestFileException : DeployToolException
{
public InvalidWindowsManifestFileException(DeployToolErrorCode errorCode, string message, Exception? innerException = null) : base(errorCode, message, innerException) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public AWSElasticBeanstalkHandler(IAWSClientFactory awsClientFactory, IOrchestra
{
try
{
return JsonSerializer.Deserialize<T>(json?.ToString() ?? string.Empty);
return JsonSerializer.Deserialize<T>(json?.ToString() ?? string.Empty, new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip });
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,42 @@ private async Task<IDictionary<string, object>> GetBeanstalkEnvironmentConfigura
}

if (recipeId.Equals(Constants.RecipeIdentifier.EXISTING_BEANSTALK_WINDOWS_ENVIRONMENT_RECIPE_ID))
{
var windowsManifest = await GetBeanstalkWindowsManifest(projectPath);
if (windowsManifest != null && windowsManifest.Deployments.AspNetCoreWeb.Count != 0)
{
optionSettings[Constants.ElasticBeanstalk.IISWebSiteOptionId] = windowsManifest.Deployments.AspNetCoreWeb[0].Parameters.IISWebSite;
optionSettings[Constants.ElasticBeanstalk.IISAppPathOptionId] = windowsManifest.Deployments.AspNetCoreWeb[0].Parameters.IISPath;
}
}

return optionSettings;
}

private async Task<ElasticBeanstalkWindowsManifest?> GetBeanstalkWindowsManifest(string projectPath)
{
try
{
var manifestPath = Path.Combine(Path.GetDirectoryName(projectPath) ?? string.Empty, Constants.ElasticBeanstalk.WindowsManifestName);
if (_fileManager.Exists(manifestPath))
{
var manifest = JsonSerializer.Deserialize<ElasticBeanstalkWindowsManifest>(await _fileManager.ReadAllTextAsync(manifestPath));
if (manifest.Deployments.AspNetCoreWeb.Count != 0)
var manifest = JsonSerializer.Deserialize<ElasticBeanstalkWindowsManifest>(await _fileManager.ReadAllTextAsync(manifestPath), new JsonSerializerOptions
{
optionSettings[Constants.ElasticBeanstalk.IISWebSiteOptionId] = manifest.Deployments.AspNetCoreWeb[0].Parameters.IISWebSite;
optionSettings[Constants.ElasticBeanstalk.IISAppPathOptionId] = manifest.Deployments.AspNetCoreWeb[0].Parameters.IISPath;
}
ReadCommentHandling = JsonCommentHandling.Skip
});

return manifest;
}
}

return optionSettings;
return null;
}
catch (Exception ex)
{
throw new InvalidWindowsManifestFileException(
DeployToolErrorCode.InvalidWindowsManifestFile,
$"We detected a malformed Elastic Beanstalk Windows manifest file '{Constants.ElasticBeanstalk.WindowsManifestName}' in your project and were not able to load the previous settings from that file.",
ex);
}
}

private ConfigurationOptionSetting GetBeanstalkEnvironmentConfigurationSetting(List<ConfigurationOptionSetting> configurationSettings, string optionNameSpace, string optionName)
Expand Down
66 changes: 66 additions & 0 deletions test/AWS.Deploy.CLI.UnitTests/ExceptionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System;
using AWS.Deploy.Common;
using Xunit;

namespace AWS.Deploy.CLI.UnitTests
{
public class ExceptionExtensionsTests
{
[Fact]
public void GetTruncatedErrorMessage_EmptyMessage()
{
// ARRANGE
var ex = new Exception(string.Empty);

// ACT and ASSERT
Assert.Equal(string.Empty, ex.GetTruncatedErrorMessage());
}

[Fact]
public void GetTruncatedErrorMessage_NoTruncation()
{
// ARRANGE
var message = "This is an AWSDeployToolException";
var ex = new Exception(message);

// ACT and ASSERT
// No truncation is performed because the message length < 2 * k
Assert.Equal(message, ex.GetTruncatedErrorMessage(numChars: 50));
}

[Fact]
public void GetTruncatedErrorMessage()
{
// ARRANGE
var message =
"error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. " +
"error text. error text. error text. error text. error text. error text. error text.";


var ex = new Exception(message);

// ACT
var truncatedErrorMessage = ex.GetTruncatedErrorMessage(numChars: 50);

// ACT and ASSERT
var expectedMessage =
@"error text. error text. error text. error text. er
...
Error truncated to the first and last 50 characters
...
t. error text. error text. error text. error text.";

Assert.Equal(expectedMessage, truncatedErrorMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,38 @@ public async Task GetPreviousSettings_BeanstalkEnvironment()
Assert.Equal("false", optionSettings[Constants.ElasticBeanstalk.XRayTracingOptionId]);
}

[Fact]
public async Task GetPreviousSettings_BeanstalkWindowsEnvironment()
[Theory]

[InlineData(@"{
""manifestVersion"": 1,
""deployments"": {
""aspNetCoreWeb"": [
{
""name"": ""app"",
""parameters"": {
""iisPath"": ""/path"",
""iisWebSite"": ""Default Web Site Custom""
}
}
]
}
}")]
[InlineData(@"{
""manifestVersion"": 1,
// comments
""deployments"": {
""aspNetCoreWeb"": [
{
""name"": ""app"",
""parameters"": {
""iisPath"": ""/path"",
""iisWebSite"": ""Default Web Site Custom""
}
}
]
}
}")]
public async Task GetPreviousSettings_BeanstalkWindowsEnvironment(string manifestJson)
{
var application = new CloudApplication("name", "Id", CloudApplicationResourceType.BeanstalkEnvironment, "recipe");
var configurationSettings = new List<ConfigurationOptionSetting>
Expand Down Expand Up @@ -507,20 +537,6 @@ public async Task GetPreviousSettings_BeanstalkWindowsEnvironment()
_mockOrchestratorInteractiveService.Object,
_fileManager);

var manifestJson = @"{
""manifestVersion"": 1,
""deployments"": {
""aspNetCoreWeb"": [
{
""name"": ""app"",
""parameters"": {
""iisPath"": ""/path"",
""iisWebSite"": ""Default Web Site Custom""
}
}
]
}
}";
_fileManager.InMemoryStore.Add(Path.Combine("testPath", "aws-windows-deployment-manifest.json"), manifestJson);
var projectDefinition = new ProjectDefinition(null, Path.Combine("testPath", "project.csproj"), "", "net6.0");
var recipeDefinitiion = new RecipeDefinition("AspNetAppExistingBeanstalkWindowsEnvironment", "", "", DeploymentTypes.BeanstalkEnvironment, DeploymentBundleTypes.DotnetPublishZipFile, "", "", "", "", "");
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.4",
"version": "1.5",
"publicReleaseRefSpec": [
".*"
],
Expand Down

0 comments on commit c1bf3d1

Please sign in to comment.