diff --git a/src/modules/cmdpal/Exts/MastodonExtension/MastodonExtensionPage.cs b/src/modules/cmdpal/Exts/MastodonExtension/MastodonExtensionPage.cs index 9770d82100..71728f52db 100644 --- a/src/modules/cmdpal/Exts/MastodonExtension/MastodonExtensionPage.cs +++ b/src/modules/cmdpal/Exts/MastodonExtension/MastodonExtensionPage.cs @@ -201,7 +201,7 @@ public partial class MastodonPostForm : Form { "type": "Image", "url": "${author_avatar_url}", - "size": "Small", + "size": "Medium", "style": "Person" } ] diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/AddBookmarkForm.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/AddBookmarkForm.cs index 0995b0c019..a339add945 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/AddBookmarkForm.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/AddBookmarkForm.cs @@ -56,7 +56,7 @@ internal sealed partial class AddBookmarkForm : Form public override string DataJson() => throw new NotImplementedException(); - public override string StateJson() => throw new NotImplementedException(); + public override string StateJson() => "{}"; public override CommandResult SubmitForm(string payload) { diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/BookmarkPlaceholderForm.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/BookmarkPlaceholderForm.cs index 7b5556a53d..dad1cf45a2 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/BookmarkPlaceholderForm.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Bookmark/BookmarkPlaceholderForm.cs @@ -69,7 +69,7 @@ internal sealed partial class BookmarkPlaceholderForm : Form public override string DataJson() => throw new NotImplementedException(); - public override string StateJson() => throw new NotImplementedException(); + public override string StateJson() => "{}"; public override CommandResult SubmitForm(string payload) { diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.CmdPalSettings/SettingsForm.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.CmdPalSettings/SettingsForm.cs index e178cefc61..1ce54b7b43 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.CmdPalSettings/SettingsForm.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.CmdPalSettings/SettingsForm.cs @@ -75,7 +75,7 @@ internal sealed partial class SettingsForm : Form """; } - public override string StateJson() => throw new NotImplementedException(); + public override string StateJson() => "{}"; public override CommandResult SubmitForm(string payload) { diff --git a/src/modules/cmdpal/Exts/SamplePagesExtension/Forms/SampleForm.cs b/src/modules/cmdpal/Exts/SamplePagesExtension/Forms/SampleForm.cs index 401406d384..74ed6c63a2 100644 --- a/src/modules/cmdpal/Exts/SamplePagesExtension/Forms/SampleForm.cs +++ b/src/modules/cmdpal/Exts/SamplePagesExtension/Forms/SampleForm.cs @@ -55,7 +55,7 @@ internal sealed partial class SampleForm : Form return json; } - public override string StateJson() => throw new NotImplementedException(); + public override string StateJson() => "{}"; public override CommandResult SubmitForm(string payload) { diff --git a/src/modules/cmdpal/Exts/SpongebotExtension/SpongeSettingsForm.cs b/src/modules/cmdpal/Exts/SpongebotExtension/SpongeSettingsForm.cs index e0c13194d9..4ea80ee5cf 100644 --- a/src/modules/cmdpal/Exts/SpongebotExtension/SpongeSettingsForm.cs +++ b/src/modules/cmdpal/Exts/SpongebotExtension/SpongeSettingsForm.cs @@ -54,7 +54,7 @@ internal sealed partial class SpongeSettingsForm : Form public override string DataJson() => throw new NotImplementedException(); - public override string StateJson() => throw new NotImplementedException(); + public override string StateJson() => "{}"; public override CommandResult SubmitForm(string payload) { diff --git a/src/modules/cmdpal/Exts/YouTubeExtension/Pages/YouTubeAPIForm.cs b/src/modules/cmdpal/Exts/YouTubeExtension/Pages/YouTubeAPIForm.cs index 3234adafcc..56520c0ea3 100644 --- a/src/modules/cmdpal/Exts/YouTubeExtension/Pages/YouTubeAPIForm.cs +++ b/src/modules/cmdpal/Exts/YouTubeExtension/Pages/YouTubeAPIForm.cs @@ -50,7 +50,7 @@ internal sealed partial class YouTubeAPIForm : Form public override string DataJson() => throw new NotImplementedException(); - public override string StateJson() => throw new NotImplementedException(); + public override string StateJson() => "{}"; public override CommandResult SubmitForm(string payload) { diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FormViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FormViewModel.cs index 4e2a5b175a..4d5f0416f8 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FormViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FormViewModel.cs @@ -2,6 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Text.Json; using AdaptiveCards.ObjectModel.WinUI3; using AdaptiveCards.Templating; using Microsoft.CmdPal.Extensions; @@ -33,15 +34,36 @@ public partial class FormViewModel(IForm _form, IPageContext context) : Extensio return; } - TemplateJson = model.TemplateJson(); - StateJson = model.StateJson(); - DataJson = model.DataJson(); + try + { + TemplateJson = model.TemplateJson(); + StateJson = model.StateJson(); + DataJson = model.DataJson(); - AdaptiveCardTemplate template = new(TemplateJson); - var cardJson = template.Expand(DataJson); - Card = AdaptiveCard.FromJsonString(cardJson); + AdaptiveCardTemplate template = new(TemplateJson); + var cardJson = template.Expand(DataJson); + Card = AdaptiveCard.FromJsonString(cardJson); + } + catch (Exception e) + { + // If we fail to parse the card JSON, then display _our own card_ + // with the exception + AdaptiveCardTemplate template = new(ErrorCardJson); + + // todo: we could probably stick Card.Errrors in there too + var dataJson = $$""" +{ + "error_message": {{JsonSerializer.Serialize(e.Message)}}, + "error_stack": {{JsonSerializer.Serialize(e.StackTrace)}}, + "inner_exception": {{JsonSerializer.Serialize(e.InnerException?.Message)}}, + "template_json": {{JsonSerializer.Serialize(TemplateJson)}}, + "data_json": {{JsonSerializer.Serialize(DataJson)}} +} +"""; + var cardJson = template.Expand(dataJson); + Card = AdaptiveCard.FromJsonString(cardJson); + } - // TODO catch and replace with our error card UpdateProperty(nameof(Card)); } @@ -78,4 +100,45 @@ public partial class FormViewModel(IForm _form, IPageContext context) : Extensio } } } + + private static readonly string ErrorCardJson = """ +{ + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.5", + "body": [ + { + "type": "TextBlock", + "text": "Error parsing form from extension", + "wrap": true, + "style": "heading", + "size": "ExtraLarge", + "weight": "Bolder", + "color": "Attention" + }, + { + "type": "TextBlock", + "wrap": true, + "text": "${error_message}", + "color": "Attention" + }, + { + "type": "TextBlock", + "text": "${error_stack}", + "fontType": "Monospace" + }, + { + "type": "TextBlock", + "wrap": true, + "text": "Inner exception:" + }, + { + "type": "TextBlock", + "wrap": true, + "text": "${inner_exception}", + "color": "Attention" + } + ] +} +"""; } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/AdaptiveCardsConfig.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/AdaptiveCardsConfig.cs similarity index 100% rename from src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/AdaptiveCardsConfig.xaml.cs rename to src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/AdaptiveCardsConfig.cs