forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorCommand.cpp
More file actions
87 lines (77 loc) · 2.89 KB
/
Copy pathErrorCommand.cpp
File metadata and controls
87 lines (77 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "ErrorCommand.h"
#include "AppInstallerStrings.h"
#include "AppInstallerErrors.h"
#include "VTSupport.h"
namespace AppInstaller::CLI
{
namespace
{
// 0x12345678 : SYMBOL_VALUE
// Descriptive text
void OutputHResultInformation(Execution::Context& context, const Errors::HResultInformation& error)
{
auto info = context.Reporter.Info();
info << VirtualTerminal::TextFormat::Foreground::Bright << "0x" << VirtualTerminal::TextFormat::Foreground::Bright << Logging::SetHRFormat << error.Value();
if (!error.Symbol().empty())
{
info << " : "_liv << VirtualTerminal::TextFormat::Foreground::BrightCyan << error.Symbol();
}
info << std::endl;
info << error.GetDescription() << std::endl;
}
}
std::vector<Argument> ErrorCommand::GetArguments() const
{
return {
Argument{ Execution::Args::Type::ErrorInput, Resource::String::ErrorInputArgumentDescription, ArgumentType::Positional, true },
};
}
Resource::LocString ErrorCommand::ShortDescription() const
{
return { Resource::String::ErrorCommandShortDescription };
}
Resource::LocString ErrorCommand::LongDescription() const
{
return { Resource::String::ErrorCommandLongDescription };
}
void ErrorCommand::ExecuteInternal(Execution::Context& context) const
{
std::string input{ context.Args.GetArg(Execution::Args::Type::ErrorInput) };
const char* begin = input.c_str();
char* end = nullptr;
errno = 0;
HRESULT errorNumber = strtol(begin, &end, 0);
if (errno == ERANGE)
{
errno = 0;
unsigned long unsignedError = strtoul(begin, &end, 0);
if (errno == ERANGE)
{
context.Reporter.Error() << Resource::String::ErrorNumberIsTooLarge << std::endl;
AICLI_TERMINATE_CONTEXT(E_INVALIDARG);
}
errorNumber = static_cast<HRESULT>(unsignedError);
}
// If the entire string was consumed as a number, treat it as an HRESULT
if (static_cast<size_t>(end - begin) == input.length())
{
auto error = Errors::HResultInformation::Find(errorNumber);
if (error)
{
OutputHResultInformation(context, *error);
}
}
// otherwise, treat it as a string and search our error list
else
{
auto errors = Errors::HResultInformation::Find(Utility::Trim(input));
for (const auto& error : errors)
{
OutputHResultInformation(context, *error);
}
}
}
}