fix: 捕获解析YAML时发生的错误

This commit is contained in:
jackfiled 2023-10-17 18:04:17 +08:00
parent 02f7ca63dc
commit d3833fdfda
4 changed files with 32 additions and 6 deletions

View File

@ -10,7 +10,7 @@
<div class="control-zone">
<Space Size="@("0")">
<SpaceItem>
<Button Type="@ButtonType.Text">
<Button Type="@ButtonType.Text" OnClick="@QuitButtontClicked">
退出
</Button>
</SpaceItem>
@ -28,7 +28,9 @@
<div class="logging-zone">
<AntList TItem="@string" DataSource="@_logs" Split="@false" @ref="@_logList">
<p style="margin-bottom: 0">@context</p>
<ListItem class="logging-item">
<p>@context</p>
</ListItem>
</AntList>
</div>
</div>
@ -60,7 +62,7 @@
Log("未设置文法");
grammarText = string.Empty;
}
return new StandaloneEditorConstructionOptions
{
Language = "yaml",
@ -84,10 +86,14 @@
}
}
private void QuitButtontClicked()
{
Navigation.NavigateTo("/", replace: true);
}
private void Log(string message)
{
_logs.Add($"{DateTime.Now:HH:mm:ss} {message}");
StateHasChanged();
}
}
}

View File

@ -10,4 +10,9 @@
.logging-zone {
height: 15%;
overflow-y: auto;
margin-bottom: 5px;
}
.logging-item {
padding: 0 0 0;
}

View File

@ -11,6 +11,7 @@
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5163",
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -1,4 +1,5 @@
using Katheryne.Abstractions;
using Katheryne.Exceptions;
using Katheryne.Models;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
@ -27,13 +28,26 @@ public class KatheryneChatRobotFactory
_defaultChatRobot = defaultChatRobot;
}
/// <summary>
/// 设置当前机器人使用的文法
/// </summary>
/// <param name="grammarText">文法字符串</param>
/// <exception cref="GrammarException">编译文法失败抛出的异常</exception>
public void SetGrammar(string grammarText)
{
_factoryLogger.LogInformation("Receive new grammar: {}.", grammarText);
GrammarText = grammarText;
IDeserializer deserializer = _deserializerFactory.GetDeserializer();
LexicalModel model = deserializer.Deserialize<LexicalModel>(grammarText);
LexicalModel model;
try
{
model = deserializer.Deserialize<LexicalModel>(grammarText);
}
catch (Exception ex)
{
throw new GrammarException("Failed to parse lexical model.", ex);
}
_grammar = new Grammar(new GrammarTree(model), model.RobotName, model.BeginStageName);
}