azure-sdk-for-cpp
azure-sdk-for-cpp copied to clipboard
Update our samples to use `std::getenv()` with a throw
Reading our samples should require as little distracting inner knowledge as possible. Everyone knows getenv(). We should use it. We should not use things like GetEnvHelper.
Pattern should be like this:
#include <cstdlib>
#include <iostream>
#include <azure/...>
int main() {
try {
const char* tenantIdEnvVar = std::getenv("AZURE_TENANT_ID");
std::string tenantId = (tenantIdEnvVar != nullptr) ? tenantIdEnvVar : throw std::exception("AZURE_TENANT_ID not set");
// ...
// ...
// ... other sample code. Our SDK throws exceptions that are derived from std::exception,
// so the catch block below is going to handle them as well.
} catch (const std::exception& ex) {
std::cerr << '\n' << ex.what();
return 1;
// Also, for troubleshooting, try setting AZURE_LOG_LEVEL=verbose environment variable and re-running.
// Read more at aka.ms to https://github.com/Azure/azure-sdk-for-cpp/tree/main/sdk/core/azure-core#sdk-log-messages
}
return 0;
}
See also #5133