Powershell: Get-Date -UFormat '' 为空字符串参数抛出一个无用的异常

创建于 2017-10-07  ·  3评论  ·  资料来源: PowerShell/PowerShell

Get-Date -UFormat ''中的空字符串参数会抛出IndexOutOfRangeException ,如果错误说明出了什么问题并将注意力转移到 UFormat 参数上会更有帮助。 “缺少或无效的 UFormat 字符串”或类似的词。

重现步骤

PS /test> get-date -uformat "$accidentallyEmptyFormatStringVariable"
get-date : Index was outside the bounds of the array.
At line:1 char:1
+ get-date -uformat ''
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Date], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.Commands.GetDateCommand

预期行为

A message directing attention to the -UFormat string parameter being empty.

实际行为

A message about some internal problem.

环境数据

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta.8
PSEdition                      Core
GitCommitId                    v6.0.0-beta.8
OS                             Linux 2.6.32-042stab120.6 #1 SMP Thu Oct 27 16:59:03 MSK 2016
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Area-Cmdlets-Utility Hacktoberfest Resolution-Fixed Up-for-Grabs

最有用的评论

我会处理这个。

所有3条评论

同意,所有错误消息都应该由用户采取行动(如果不可能,则提供信息)

我会处理这个。

@DdWr感谢您抓住这个。 通常,初学者会收到大多数风格评论。 请阅读编码指南。 主要是你应该遵循周围代码的风格。 另外请添加一个测试(使用我们的 ShouldBeErrorId 函数)。 将新的错误消息放入适当的 Resx 文件中。

此页面是否有帮助?
0 / 5 - 0 等级