diff options
| author | Dan Engelbrecht <[email protected]> | 2025-09-05 13:02:27 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2025-09-05 13:02:27 +0200 |
| commit | 45b0307d42b22e04cee63467a8fdb898a2d8d552 (patch) | |
| tree | 905b3eb62af89269be5b15f4c407d900ce86f7f3 /src/zenserver/config.cpp | |
| parent | Avoid mutating executable paths when copying files during full service instal... (diff) | |
| download | zen-45b0307d42b22e04cee63467a8fdb898a2d8d552.tar.xz zen-45b0307d42b22e04cee63467a8fdb898a2d8d552.zip | |
refactor zen command return value handling (#487)
- Improvement: Use consistent language for command line argument parsing errors
- Improvement: Changed zen command parsing errors to output help first and error last to make it easier to spot the error
- Improvement: Refactor zen command return codes to conform to valid Linux range (0-255)
kSuccess = 0,
kOtherError = 1,
kBadInput = 2,
kOutOfMemory = 16,
kOutOfDisk = 17,
kAssertError = 70,
kHttpOtherClientError = 80,
kHttpCantConnectError = 81,
kHttpNotFound = 66, // NotFound(404)
kHttpUnauthorized = 77, // Unauthorized(401),
kHttpSLLError = 82,
kHttpForbidden = 83, // Forbidden(403)
kHttpTimeout = 84, // RequestTimeout(408)
kHttpConflict = 85, // Conflict(409)
kHttpNoHost = 86,
kHttpOtherServerError = 90,
kHttpInternalServerError = 91, // InternalServerError(500)
kHttpServiceUnavailable = 69, // ServiceUnavailable(503)
kHttpBadGateway = 92, // BadGateway(502)
kHttpGatewayTimeout = 93, // GatewayTimeout(504)
Diffstat (limited to 'src/zenserver/config.cpp')
| -rw-r--r-- | src/zenserver/config.cpp | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/src/zenserver/config.cpp b/src/zenserver/config.cpp index 0397677b9..0cf5a9ca3 100644 --- a/src/zenserver/config.cpp +++ b/src/zenserver/config.cpp @@ -99,35 +99,37 @@ ValidateOptions(ZenServerOptions& ServerOptions) { if (ServerOptions.EncryptionKey.empty() == false) { - const auto Key = zen::AesKey256Bit::FromString(ServerOptions.EncryptionKey); + const auto Key = AesKey256Bit::FromString(ServerOptions.EncryptionKey); if (Key.IsValid() == false) { - throw zen::OptionParseException("Invalid AES encryption key"); + throw OptionParseException(fmt::format("'--encryption-aes-key' ('{}') is malformed", ServerOptions.EncryptionKey), {}); } } if (ServerOptions.EncryptionIV.empty() == false) { - const auto IV = zen::AesIV128Bit::FromString(ServerOptions.EncryptionIV); + const auto IV = AesIV128Bit::FromString(ServerOptions.EncryptionIV); if (IV.IsValid() == false) { - throw zen::OptionParseException("Invalid AES initialization vector"); + throw OptionParseException(fmt::format("'--encryption-aes-iv' ('{}') is malformed", ServerOptions.EncryptionIV), {}); } } if (ServerOptions.HttpServerConfig.ForceLoopback && ServerOptions.IsDedicated) { - throw zen::OptionParseException("Dedicated server can not be used with forced local server address"); + throw OptionParseException("'--dedicated' conflicts with '--http-forceloopback'", {}); } if (ServerOptions.GcConfig.AttachmentPassCount > ZenGcConfig::GcMaxAttachmentPassCount) { - throw zen::OptionParseException( - fmt::format("GC attachment pass count can not be larger than {}", ZenGcConfig::GcMaxAttachmentPassCount)); + throw OptionParseException(fmt::format("'--gc-attachment-passes' ('{}') is invalid, maximum is {}.", + ServerOptions.GcConfig.AttachmentPassCount, + ZenGcConfig::GcMaxAttachmentPassCount), + {}); } if (ServerOptions.GcConfig.UseGCV2 == false) { - ZEN_WARN("--gc-v2=false is deprecated, reverting to --gc-v2=true"); + ZEN_WARN("'--gc-v2=false' is deprecated, reverting to '--gc-v2=true'"); ServerOptions.GcConfig.UseGCV2 = true; } } @@ -185,7 +187,7 @@ class CachePolicyOption : public LuaConfig::OptionValue { public: CachePolicyOption(UpstreamCachePolicy& Value) : Value(Value) {} - virtual void Print(std::string_view, zen::StringBuilderBase& StringBuilder) override + virtual void Print(std::string_view, StringBuilderBase& StringBuilder) override { switch (Value) { @@ -232,7 +234,7 @@ class ZenAuthConfigOption : public LuaConfig::OptionValue { public: ZenAuthConfigOption(ZenAuthConfig& Value) : Value(Value) {} - virtual void Print(std::string_view Indent, zen::StringBuilderBase& StringBuilder) override + virtual void Print(std::string_view Indent, StringBuilderBase& StringBuilder) override { if (Value.OpenIdProviders.empty()) { @@ -275,7 +277,7 @@ class ZenObjectStoreConfigOption : public LuaConfig::OptionValue { public: ZenObjectStoreConfigOption(ZenObjectStoreConfig& Value) : Value(Value) {} - virtual void Print(std::string_view Indent, zen::StringBuilderBase& StringBuilder) override + virtual void Print(std::string_view Indent, StringBuilderBase& StringBuilder) override { if (Value.Buckets.empty()) { @@ -318,7 +320,7 @@ class ZenStructuredCacheBucketsConfigOption : public LuaConfig::OptionValue { public: ZenStructuredCacheBucketsConfigOption(std::vector<std::pair<std::string, ZenStructuredCacheBucketConfig>>& Value) : Value(Value) {} - virtual void Print(std::string_view Indent, zen::StringBuilderBase& StringBuilder) override + virtual void Print(std::string_view Indent, StringBuilderBase& StringBuilder) override { if (Value.empty()) { @@ -359,14 +361,15 @@ public: std::string Name = Kv.first.as<std::string>(); if (Name.empty()) { - throw zen::OptionParseException(fmt::format("cache bucket option must have a name.")); + throw OptionParseException("Cache bucket option must have a name.", {}); } const uint64_t MaxBlockSize = Bucket.value().get_or("maxblocksize", BucketConfig.MaxBlockSize); if (MaxBlockSize == 0) { - throw zen::OptionParseException( - fmt::format("maxblocksize option for cache bucket '{}' is invalid. It must be non-zero.", Name)); + throw OptionParseException( + fmt::format("'maxblocksize' option for cache bucket '{}' is invalid. It must be non-zero.", Name), + {}); } BucketConfig.MaxBlockSize = MaxBlockSize; @@ -375,8 +378,9 @@ public: const uint64_t MemCacheSizeThreshold = Bucket.value().get_or("sizethreshold", BucketConfig.MemCacheSizeThreshold); if (MemCacheSizeThreshold == 0) { - throw zen::OptionParseException( - fmt::format("memlayer.sizethreshold option for cache bucket '{}' is invalid. It must be non-zero.", Name)); + throw OptionParseException( + fmt::format("'memlayer.sizethreshold' option for cache bucket '{}' is invalid. It must be non-zero.", Name), + {}); } BucketConfig.MemCacheSizeThreshold = Bucket.value().get_or("sizethreshold", BucketConfig.MemCacheSizeThreshold); } @@ -384,17 +388,20 @@ public: const uint32_t PayloadAlignment = Bucket.value().get_or("payloadalignment", BucketConfig.PayloadAlignment); if (PayloadAlignment == 0 || !IsPow2(PayloadAlignment)) { - throw zen::OptionParseException(fmt::format( - "payloadalignment option for cache bucket '{}' is invalid. It needs to be non-zero and a power of two.", - Name)); + throw OptionParseException( + fmt::format( + "'payloadalignment' option for cache bucket '{}' is invalid. It needs to be non-zero and a power of two.", + Name), + {}); } BucketConfig.PayloadAlignment = PayloadAlignment; const uint64_t LargeObjectThreshold = Bucket.value().get_or("largeobjectthreshold", BucketConfig.LargeObjectThreshold); if (LargeObjectThreshold == 0) { - throw zen::OptionParseException( - fmt::format("largeobjectthreshold option for cache bucket '{}' is invalid. It must be non-zero.", Name)); + throw OptionParseException( + fmt::format("'largeobjectthreshold' option for cache bucket '{}' is invalid. It must be non-zero.", Name), + {}); } BucketConfig.LargeObjectThreshold = LargeObjectThreshold; @@ -409,19 +416,19 @@ public: }; std::shared_ptr<LuaConfig::OptionValue> -MakeOption(zen::UpstreamCachePolicy& Value) +MakeOption(UpstreamCachePolicy& Value) { return std::make_shared<CachePolicyOption>(Value); }; std::shared_ptr<LuaConfig::OptionValue> -MakeOption(zen::ZenAuthConfig& Value) +MakeOption(ZenAuthConfig& Value) { return std::make_shared<ZenAuthConfigOption>(Value); }; std::shared_ptr<LuaConfig::OptionValue> -MakeOption(zen::ZenObjectStoreConfig& Value) +MakeOption(ZenObjectStoreConfig& Value) { return std::make_shared<ZenObjectStoreConfigOption>(Value); }; @@ -660,11 +667,11 @@ ParseConfigFile(const std::filesystem::path& Path, if (!OutputConfigFile.empty()) { - std::filesystem::path WritePath(MakeSafeAbsolutePath(OutputConfigFile)); - zen::ExtendableStringBuilder<512> ConfigStringBuilder; + std::filesystem::path WritePath(MakeSafeAbsolutePath(OutputConfigFile)); + ExtendableStringBuilder<512> ConfigStringBuilder; LuaOptions.Print(ConfigStringBuilder, CmdLineResult); - zen::BasicFile Output; - Output.Open(WritePath, zen::BasicFile::Mode::kTruncate); + BasicFile Output; + Output.Open(WritePath, BasicFile::Mode::kTruncate); Output.Write(ConfigStringBuilder.Data(), ConfigStringBuilder.Size(), 0); } } @@ -737,7 +744,7 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) const char* DefaultHttp = "asio"; #if ZEN_WITH_HTTPSYS - if (!zen::windows::IsRunningOnWine()) + if (!windows::IsRunningOnWine()) { DefaultHttp = "httpsys"; } @@ -1334,7 +1341,7 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) } catch (const std::exception& Ex) { - throw zen::OptionParseException(Ex.what()); + throw OptionParseException(Ex.what(), options.help()); } if (Result.count("help")) @@ -1399,17 +1406,17 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) if (!BaseSnapshotDir.empty()) { if (DataDir.empty()) - throw zen::OptionParseException("You must explicitly specify a data directory when specifying a base snapshot"); + throw OptionParseException("'--snapshot-dir' requires '--data-dir'", options.help()); if (!IsDir(ServerOptions.BaseSnapshotDir)) - throw OptionParseException(fmt::format("Snapshot directory must be a directory: '{}", BaseSnapshotDir)); + throw std::runtime_error(fmt::format("'--snapshot-dir' ('{}') must be a directory", BaseSnapshotDir)); } if (OpenIdProviderUrl.empty() == false) { if (OpenIdClientId.empty()) { - throw zen::OptionParseException("Invalid OpenID client ID"); + throw OptionParseException("'--openid-provider-url' requires '--openid-client-id'", options.help()); } ServerOptions.AuthConfig.OpenIdProviders.push_back( @@ -1436,10 +1443,10 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) ValidateOptions(ServerOptions); } - catch (const zen::OptionParseException& e) + catch (const OptionParseException& e) { - ZEN_CONSOLE_ERROR("Invalid zenserver arguments: {}\n\n{}", e.what(), options.help()); - + ZEN_CONSOLE("{}\n", options.help()); + ZEN_CONSOLE_ERROR("Invalid zenserver arguments: {}", e.what()); throw; } |