代码之家  ›  专栏  ›  技术社区  ›  Petr

如何使PowerShell CMake模块显示完整的错误消息而不修剪它?

  •  0
  • Petr  · 技术社区  · 6 年前

    当使用失败的powershell脚本运行CMake时,它总是用 ...

    例如:

    Checking paths...     OK
    Looking for MSBuild...    OK
    Looking for Qt5...    OK
    Looking for OpenSSL...    OK
    Looking for nsis...    OK
    Looking for git...    OK
    Looking for cmake...    OK
    Configuring the project...
    Running cmake
    -- The C compiler identification is MSVC 19.0.24215.1
    -- The CXX compiler identification is MSVC 19.0.24215.1
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    cmake : CMake Error at extensions/extension-mass-delete/CMakeLists.txt:26 (find_package):
    At C:\Users\Petr Bena\Documents\huggle3-qt-lx\windows\release.ps1:172 char:5
    +     cmake ..\..\src\ -G "$cmake_generator" -DWEB_ENGINE=true -DPYTHON ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (CMake Error at ...(find_package)::String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
    
    

    这种错误根本无助于识别错误,因为它太泛化了。如何展开它以便看到完整的错误消息?

    0 回复  |  直到 6 年前
        1
  •  1
  •   trebleCode    6 年前

    你可以用 try{ } catch { } 块捕获异常,然后访问错误属性。下面是我编写的一个自定义错误写入函数:

    Function Write-CustomError()
    {
    <#
    .Synopsis
       Displays error information to the console
    .DESCRIPTION
        Writes property information from the current [ErrorRecord] object
        in the pipeline to the console
    .EXAMPLE
       Write-CustomError -UserMessage "Exception occurred at memory location $x" -ErrorObject $_
    .EXAMPLE
       Write-CustomError -UserMessage "Exception occurred at memory location $x" -ErrorObject $_ -FullDetail
    .INPUTS
       $Error[0]
    .OUTPUTS
       [String]
    .COMPONENT
       adminkitMiscTools   
    .FUNCTIONALITY
       General Utility
    #>
        [cmdletBinding()]
        param(
            [Parameter(Mandatory=$False)]
            [String]$UserMessage,
    
            [Parameter(Mandatory=$True)]
            [Object]$ErrorObject,
    
            [Parameter(Mandatory=$false)]
            [Switch]$FullDetail
        )
    
        BEGIN
        {}
        PROCESS
        {
            if($UserMessage) {
                 Write-Host "`nERROR: $UserMessage" -ForegroundColor Red
            }
    
            if($FullDetail)
            {
                $ErrorData = $ErrorData + [PSCustomObject]@{AccountUsed=$ENV:USERNAME;
                                                ExceptionMessage=$ErrorObject.ToString();
                                                CategoryInfo=$ErrorObject.CategoryInfo;
                                                ExceptionType=$ErrorObject.Exception.GetType();
                                                ErrorDetails=$ErrorObject.ErrorDetails;
                                                FullyQualifiedErrorId=$ErrorObject.FullyQualifiedErrorId;
                                                InvocationInfo=$ErrorObject.InvocationInfo;
                                                PipelineIterationInfo=$ErrorObject.PipelineIterationInfo;
                                                ScriptStackTrace=$ErrorObject.ScriptStackTrace
                                                TargetObject=$ErrorObject.TargetObject;
                                                }
            }
            return $ErrorData
        }
        END
        {}
    }
    

    在你的剧本里:

    try { 
          # your cmake command here 
     }
    catch {
         Write-CustomError -UserMessage 'There was an error' -ErrorObject $_ -FullDetail
    }
    

    这将为您提供更多有关错误的详细信息,而不应剪辑消息