FileChecker、DeserializeJson

This commit is contained in:
龙腾猫跃 committed 2026-06-11 00:36:04 +08:00
1 parent ec1ad309d8
commit 9470a6160a
4 files changed
+113 -9

No files matched your search

+4 -4
View File
@@ -31,9 +31,9 @@ public static class ExceptionExtensions {
/// <summary>
/// 获取 <paramref name="ex"/> 的用户友好描述。
/// 若 <paramref name="showMultilineStacks"/> 为 true,则返回多行的详细描述与堆栈信息;否则不整理堆栈,仅将 <see cref="Exception.Message"/> 汇总到一行。
/// 若 <paramref name="multiline"/> 为 true,则返回多行的详细描述与堆栈信息;否则不整理堆栈,仅将 <see cref="Exception.Message"/> 汇总到一行。
/// </summary>
public static string GetDisplay(this Exception? ex, bool showMultilineStacks) {
public static string GetDisplay(this Exception? ex, bool multiline) {
if (ex is null) return "无可用错误信息!";
// 提取堆栈信息
@@ -45,7 +45,7 @@ public static class ExceptionExtensions {
_ => ""
});
foreach (var currentEx in ex.Flatten()) {
if (showMultilineStacks) {
if (multiline) {
lines.Add((isInnerException ? "→ " : "") + getExceptionMessage(currentEx).ReplaceLineEndings("\r\n", true));
var stackLines = (currentEx.StackTrace?.SplitLines(true) ?? [])
.Select(l => l.BeforeLast("(") + l.AfterLast(")"))
@@ -73,7 +73,7 @@ public static class ExceptionExtensions {
commonReason = "你的网络环境不佳,请稍后再试,或使用 VPN 改善网络环境。";
// 输出
if (showMultilineStacks) {
if (multiline) {
if (commonReason is null) {
return lines.Join("\r\n");
} else {
+19 -5
View File
@@ -1,4 +1,6 @@
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
namespace MeloongCore.Extensions;
public static class StringExtensions {
@@ -331,8 +333,6 @@ public static class StringExtensions {
#endregion
#region 读取为字符串
/// <summary>
/// 读取 <see cref="Stream"/> 中的内容,并解码为字符串。
/// 会将流的位置主动重置到开头。
@@ -366,8 +366,6 @@ public static class StringExtensions {
}
}
#endregion
/// <summary>
/// 将第一个字符转换为大写,其余字符转换为小写。
/// </summary>
@@ -400,4 +398,20 @@ public static class StringExtensions {
return result ^ 0xA98F501BC684032FUL;
}
/// <summary>
/// 将字符串转化为 JSON 对象。
/// </summary>
public static JToken? DeserializeJson(this string data) {
try {
return JsonConvert.DeserializeObject<JToken>(data, new JsonSerializerSettings {DateTimeZoneHandling = DateTimeZoneHandling.Local});
} catch (Exception ex) {
int length = (data ?? "").Length;
throw new FormatException("JSON deserialize failed: " +
(length > 2000
? $"{data![..500]}...(total {length} characters)...{data[^500..]}"
: (data ?? "")),
ex);
}
}
}