Commit c4c0efe1 authored by Jason Williams's avatar Jason Williams
Browse files

Fix some error cases in appx install and loose deploy.

parent 7e618b3f
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ namespace XboxWdpDriver
            "        Installs the given AppX package, along with any given dependencies.\n" +
            "  /folder:<path to loose folder> [/depend:<path to dependency1>;<path to dependency2> /cer:<path to certificate> /transfer:<SMB or HTTP, SMB is the default> /destfoldername:<folder name, defaults to the same as the loose folder>]\n" +
            "        Installs the appx from a loose folder, along with any given dependencies.\n" +
            "  /register:<subpath on DevelopmentFiles\\LooseFolder to app to register>\n" +
            "  /register:<subpath on DevelopmentFiles\\LooseApps to app to register>\n" +
            "        Registers a loose folder that is already present on the device.\n";

        /// <summary>
@@ -124,6 +124,12 @@ namespace XboxWdpDriver
                        operation.mreAppInstall.Reset();
                        Task installTask = portal.InstallApplication(null, dependency, new List<string>());
                        operation.mreAppInstall.WaitOne();

                        if (operation.installResults.Status != ApplicationInstallStatus.Completed)
                        {
                            Console.WriteLine("Deploy failed during dependency installation. {0}", operation.installResults.Message);
                            return;
                        }
                    }

                    if (!Directory.Exists(folderPath))
+11 −9
Original line number Diff line number Diff line
@@ -199,20 +199,22 @@ namespace Microsoft.Tools.WindowsDevicePortal
            {
                DevicePortalException dpe = e as DevicePortalException;

                HttpStatusCode status = (HttpStatusCode)0;
                Uri request = null;
                if (dpe != null)
                {
                    status = dpe.StatusCode;
                    request = dpe.RequestUri;
                    this.SendAppInstallStatus(
                        ApplicationInstallStatus.Failed,
                        ApplicationInstallPhase.Idle,
                        string.Format("Failed to install {0}: {1}", appName, dpe.Reason));
                }

                else
                {
                    this.SendAppInstallStatus(
                        ApplicationInstallStatus.Failed,
                        ApplicationInstallPhase.Idle,
                        string.Format("Failed to install {0}: {1}", appName, installPhaseDescription));
                }
            }
        }

        /// <summary>
        /// Uninstalls the specified application.
+53 −12
Original line number Diff line number Diff line
@@ -34,9 +34,44 @@ namespace Microsoft.Tools.WindowsDevicePortal
        /// <summary>
        /// Initializes a new instance of the <see cref="DevicePortalException"/> class.
        /// </summary>
        /// <param name="responseMessage">Http response message</param>
        /// <param name="message">Optional exception message</param>
        /// <param name="innerException">Optional inner exception</param>
        /// <param name="statusCode">The Http status code.</param>
        /// <param name="errorResponse">Http parsed error response message.</param>
        /// <param name="requestUri">Request URI which threw the exception.</param>
        /// <param name="message">Optional exception message.</param>
        /// <param name="innerException">Optional inner exception.</param>
        public DevicePortalException(
            HttpStatusCode statusCode,
            HttpErrorResponse errorResponse,
            Uri requestUri = null,
            string message = "",
            Exception innerException = null) : this(
                                                    statusCode,
                                                    errorResponse.Reason,
                                                    requestUri,
                                                    message,
                                                    innerException)
        {
            this.HResult = errorResponse.ErrorCode;
            this.Reason = errorResponse.ErrorMessage;

            // If we didn't get the Hresult and reason from these properties, try the other ones.
            if (this.HResult == 0)
            {
                this.HResult = errorResponse.Code;
            }

            if (string.IsNullOrEmpty(this.Reason))
            {
                this.Reason = errorResponse.Reason;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DevicePortalException"/> class.
        /// </summary>
        /// <param name="responseMessage">Http response message.</param>
        /// <param name="message">Optional exception message.</param>
        /// <param name="innerException">Optional inner exception.</param>
        public DevicePortalException(
            HttpResponseMessage responseMessage,
            string message = "",
@@ -113,11 +148,11 @@ namespace Microsoft.Tools.WindowsDevicePortal
        /// <summary>
        /// Initializes a new instance of the <see cref="DevicePortalException"/> class.
        /// </summary>
        /// <param name="statusCode">Http status code</param>
        /// <param name="reason">Reason for exception</param>
        /// <param name="requestUri">Request URI which threw the exception</param>
        /// <param name="message">Optional message</param>
        /// <param name="innerException">Optional inner exception</param>
        /// <param name="statusCode">Http status code.</param>
        /// <param name="reason">Reason for exception.</param>
        /// <param name="requestUri">Request URI which threw the exception.</param>
        /// <param name="message">Optional message.</param>
        /// <param name="innerException">Optional inner exception.</param>
        public DevicePortalException(
            HttpStatusCode statusCode,
            string reason,
@@ -133,17 +168,17 @@ namespace Microsoft.Tools.WindowsDevicePortal
        }

        /// <summary>
        /// Gets the HTTP Status code
        /// Gets the HTTP Status code.
        /// </summary>
        public HttpStatusCode StatusCode { get; private set; }
        
        /// <summary>
        /// Gets a reason for the exception
        /// Gets a reason for the exception.
        /// </summary>
        public string Reason { get; private set; }

        /// <summary>
        /// Gets the request URI that threw the exception
        /// Gets the request URI that threw the exception.
        /// </summary>
        public Uri RequestUri { get; private set; }

@@ -167,7 +202,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
        /// an HTTP response.
        /// </summary>
        [DataContract]
        private class HttpErrorResponse
        public class HttpErrorResponse
        {
            /// <summary>
            /// Gets or sets the ErrorCode
@@ -192,6 +227,12 @@ namespace Microsoft.Tools.WindowsDevicePortal
            /// </summary>
            [DataMember(Name = "Reason")]
            public string Reason { get; set; }

            /// <summary>
            /// Gets or sets the Success. For an error this should generally be false if present.
            /// </summary>
            [DataMember(Name = "Success")]
            public bool Success { get; set; }
        }

        #endregion
+10 −0
Original line number Diff line number Diff line
@@ -41,6 +41,16 @@ namespace Microsoft.Tools.WindowsDevicePortal
            await this.Post(
                RegisterPackageApi,
                string.Format("folder={0}", Utilities.Hex64Encode(folderName)));

            // Poll the status until complete.
            ApplicationInstallStatus status = ApplicationInstallStatus.InProgress;
            do
            {
                await Task.Delay(TimeSpan.FromMilliseconds(500));

                status = await this.GetInstallStatus();
            }
            while (status == ApplicationInstallStatus.InProgress);
        }

        /// <summary>
+45 −2
Original line number Diff line number Diff line
@@ -7,13 +7,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Security.Credentials;
using Windows.Storage.Streams;
using Windows.Web.Http;
using Windows.Web.Http.Filters;
using Windows.Web.Http.Headers;
using static Microsoft.Tools.WindowsDevicePortal.DevicePortalException;

namespace Microsoft.Tools.WindowsDevicePortal
{
@@ -61,8 +65,47 @@ namespace Microsoft.Tools.WindowsDevicePortal
                        if (response.StatusCode == HttpStatusCode.Ok)
                        {
                            // Status code: 200
                            if (response.Content != null)
                            {
                                Stream dataStream = null;

                                IBuffer dataBuffer = null;
                                using (IHttpContent messageContent = response.Content)
                                {
                                    IAsyncOperationWithProgress<IBuffer, ulong> bufferOperation = messageContent.ReadAsBufferAsync();
                                    while (bufferOperation.Status != AsyncStatus.Completed)
                                    {
                                    }

                                    dataBuffer = bufferOperation.GetResults();

                                    if (dataBuffer != null)
                                    {
                                        dataStream = dataBuffer.AsStream();
                                    }
                                }

                                if (dataStream != null)
                                {
                                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HttpErrorResponse));

                                    HttpErrorResponse errorResponse = (HttpErrorResponse)serializer.ReadObject(dataStream);

                                    if (errorResponse.Success)
                                    {
                                        status = ApplicationInstallStatus.Completed;
                                    }
                                    else
                                    {
                                        throw new DevicePortalException(response.StatusCode, errorResponse, uri);
                                    }
                                }
                                else
                                {
                                    throw new DevicePortalException(HttpStatusCode.Conflict, "Failed to deserialize GetInstallStatus response.");
                                }
                            }
                        }
                        else if (response.StatusCode == HttpStatusCode.NoContent)
                        {
                            // Status code: 204
@@ -71,7 +114,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                    }
                    else
                    {
                        status = ApplicationInstallStatus.Failed; 
                        throw new DevicePortalException(response);
                    }
                }
            }
Loading