Result.h (2597B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 4 #pragma once 5 6 #include <cstdint> 7 #include <string> 8 #include <string_view> 9 10 namespace SFS 11 { 12 class Result 13 { 14 public: 15 enum Code : uint32_t 16 { 17 Success = 0x00000000, 18 19 // 20 // Failure codes 21 // 22 23 // Generic errors start at 0x8000'0000 24 InvalidArg = 0x8000'0001, 25 NotImpl = 0x8000'0002, 26 NotSet = 0x8000'0003, 27 OutOfMemory = 0x8000'0004, 28 Unexpected = 0x8000'0005, 29 30 // Connection errors start at 0x8000'1000 31 ConnectionSetupFailed = 0x8000'1000, 32 ConnectionUnexpectedError = 0x8000'1001, 33 ConnectionUrlSetupFailed = 0x8000'1002, 34 35 // Http Errors start at 0x8000'2000 36 // Generic Http errors 37 HttpTimeout = 0x8000'2000, 38 HttpUnexpected = 0x8000'2001, 39 40 // Last 3 digits mapped to Http status codes 41 HttpBadRequest = 0x8000'2400, 42 HttpNotFound = 0x8000'2404, 43 HttpMethodNotAllowed = 0x8000'2405, 44 HttpTooManyRequests = 0x8000'2429, 45 HttpServiceNotAvailable = 0x8000'2503, 46 47 // Service errors start at 0x8000'3000 48 ServiceInvalidResponse = 0x8000'3000, 49 ServiceUnexpectedContentType = 0x8000'3001, 50 }; 51 52 Result(Code code) noexcept; 53 Result(Code code, std::string message) noexcept; 54 55 Code GetCode() const noexcept; 56 57 /// @brief Returns the message associated with the result code. 58 /// @note "GetMsg" is used instead of "GetMessage" to avoid conflicts with Windows API. 59 const std::string& GetMsg() const noexcept; 60 61 bool IsSuccess() const noexcept; 62 bool IsFailure() const noexcept; 63 64 /** 65 * @brief Returns true if the result code represents a successful operation. 66 */ 67 operator bool() const noexcept; 68 69 bool operator==(Code resultCode) const noexcept; 70 bool operator!=(Code resultCode) const noexcept; 71 72 // Results should not be compared directly 73 bool operator==(Result) const = delete; 74 bool operator!=(Result) const = delete; 75 bool operator>(Result) const = delete; 76 bool operator>=(Result) const = delete; 77 bool operator<(Result) const = delete; 78 bool operator<=(Result) const = delete; 79 80 private: 81 Code m_code; 82 std::string m_message; 83 }; 84 85 std::string_view ToString(Result::Code code) noexcept; 86 } // namespace SFS 87 88 std::ostream& operator<<(std::ostream& os, const SFS::Result& value); 89 std::ostream& operator<<(std::ostream& os, const SFS::Result::Code& value);