babalae/better-genshin-impact · warning · ArgumentException

文件路径不能为空

Error message

文件路径不能为空

What it means

Thrown by ScriptUtils.NormalizePath when the input path is null, empty, or whitespace. This is the first validation gate before any path normalization/escape check; it rejects obviously invalid input early.

Source

Thrown at BetterGenshinImpact/Core/Script/Utils/ScriptUtils.cs:16

using System;
using System.IO;
using System.Linq;

namespace BetterGenshinImpact.Core.Script.Utils;

public class ScriptUtils
{
    /// <summary>
    /// Normalize and validate a path.
    /// </summary>
    public static string NormalizePath(string root, string path)
    {
        // 校验空字符串
        if (string.IsNullOrWhiteSpace(path))
            throw new ArgumentException("文件路径不能为空");

        // 检查是否含有非法文件名字符
        var invalidChars = Path.GetInvalidFileNameChars();
        string fileName = Path.GetFileName(path);
        if (fileName.Any(c => invalidChars.Contains(c)))
        {
            throw new ArgumentException($"文件路径 '{path}' 包含非法字符");
        }

        // 替换分隔符
        path = path.Replace('\\', '/');

        // 组合并获取绝对路径
        var fullPath = Path.GetFullPath(Path.Combine(root, path));

        // 防止越界访问
        if (!fullPath.StartsWith(root, StringComparison.OrdinalIgnoreCase))
        {

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Validate at the source: if path comes from JSON/config, use a required-field check or [JsonRequired] so the error surfaces at deserialization, not deep in NormalizePath.
  2. Coalesce empty to a sensible default if an empty path is semantically valid for the caller.
  3. Keep this throw but ensure all callers catch ArgumentException and present a user-facing message naming the missing field.

Example fix

// before
if (string.IsNullOrWhiteSpace(path))
    throw new ArgumentException("文件路径不能为空");

// after (include parameter name)
if (string.IsNullOrWhiteSpace(path))
    throw new ArgumentException("文件路径不能为空", nameof(path));
Defensive patterns

Strategy: validation

Validate before calling

// Validate at the source (e.g., JSON config) before calling NormalizePath
if (string.IsNullOrWhiteSpace(manifest.Path))
    throw new ArgumentException("manifest 缺少 path 字段");

Type guard

static bool IsValidPathInput(string? path) =>
    !string.IsNullOrWhiteSpace(path);

Try / catch

catch (ArgumentException ex) when (ex.Message.Contains("不能为空"))
{
    Toast.Error($"脚本路径为空,请检查 manifest: {scriptName}");
}

Prevention

When it happens

Trigger: NormalizePath(root, path) called with path = null, "", or a string of only whitespace. Any caller (manifest parsing, file checkout, webview bridge) passing an unvalidated path triggers it.

Common situations: A manifest field for a script path is empty; a JSON config omits the path key and deserialization yields null/empty; a webview request supplies no path parameter.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/c5053df6813a1c39. Report an issue: GitHub.