diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eb695170..fced1ebe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `-ApplicationId` as alias for `-ClientId` in `Connect-PnPOnline` and `Request-PnPAccessToken` cmdlets. [#2805](https://github.com/pnp/powershell/pull/2805) - Added `-Connection` option to `Connect-PnPOnline` which allows of reusing an authenticated connection to connect to a different site [#2821](https://github.com/pnp/powershell/pull/2821) - Added `-UserAssignedManagedIdentityAzureResourceId` and `-UserAssignedManagedIdentityClientId` as alternatives to `-UserAssignedManagedIdentityObjectId` for `Connect-PnPOnline -ManagedIdentity` to provide an user managed identity to authenticate with. [#2813](https://github.com/pnp/powershell/pull/2813) +- Added clearer error message when connecting using an expired client secret and trying to execute a command ### Changed diff --git a/src/Commands/Base/PnPConnectedCmdlet.cs b/src/Commands/Base/PnPConnectedCmdlet.cs index dbc73018f..23045e0c5 100644 --- a/src/Commands/Base/PnPConnectedCmdlet.cs +++ b/src/Commands/Base/PnPConnectedCmdlet.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Management.Automation; @@ -69,6 +70,23 @@ protected override void ProcessRecord() errorMessage = (rex.Error as PnP.Core.SharePointRestError).Message; break; + case System.Reflection.TargetInvocationException tex: + Exception innermostException = tex; + while (innermostException.InnerException != null) innermostException = innermostException.InnerException; + + if (innermostException is System.Net.WebException wex) + { + using(var streamReader = new StreamReader (wex.Response.GetResponseStream())) + { + errorMessage = $"{wex.Status}: {wex.Message} Response received: {streamReader.ReadToEnd()}"; + } + } + else + { + errorMessage = innermostException.Message; + } + break; + default: errorMessage = ex.Message; break;