1// Protocol Buffers - Google's data interchange format 2// Copyright 2008 Google Inc. All rights reserved. 3// https://developers.google.com/protocol-buffers/ 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are 7// met: 8// 9// * Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above 12// copyright notice, this list of conditions and the following disclaimer 13// in the documentation and/or other materials provided with the 14// distribution. 15// * Neither the name of Google Inc. nor the names of its 16// contributors may be used to endorse or promote products derived from 17// this software without specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30#ifndef GOOGLE_PROTOBUF_STUBS_STATUS_H_ 31#define GOOGLE_PROTOBUF_STUBS_STATUS_H_ 32 33#include <iosfwd> 34#include <string> 35 36#include <google/protobuf/stubs/common.h> 37#include <google/protobuf/stubs/stringpiece.h> 38 39namespace google { 40namespace protobuf { 41namespace util { 42namespace error { 43// These values must match error codes defined in google/rpc/code.proto. 44enum Code { 45 OK = 0, 46 CANCELLED = 1, 47 UNKNOWN = 2, 48 INVALID_ARGUMENT = 3, 49 DEADLINE_EXCEEDED = 4, 50 NOT_FOUND = 5, 51 ALREADY_EXISTS = 6, 52 PERMISSION_DENIED = 7, 53 UNAUTHENTICATED = 16, 54 RESOURCE_EXHAUSTED = 8, 55 FAILED_PRECONDITION = 9, 56 ABORTED = 10, 57 OUT_OF_RANGE = 11, 58 UNIMPLEMENTED = 12, 59 INTERNAL = 13, 60 UNAVAILABLE = 14, 61 DATA_LOSS = 15, 62}; 63} // namespace error 64 65class LIBPROTOBUF_EXPORT Status { 66 public: 67 // Creates a "successful" status. 68 Status(); 69 70 // Create a status in the canonical error space with the specified 71 // code, and error message. If "code == 0", error_message is 72 // ignored and a Status object identical to Status::OK is 73 // constructed. 74 Status(error::Code error_code, StringPiece error_message); 75 Status(const Status&); 76 Status& operator=(const Status& x); 77 ~Status() {} 78 79 // Some pre-defined Status objects 80 static const Status OK; // Identical to 0-arg constructor 81 static const Status CANCELLED; 82 static const Status UNKNOWN; 83 84 // Accessor 85 bool ok() const { 86 return error_code_ == error::OK; 87 } 88 int error_code() const { 89 return error_code_; 90 } 91 StringPiece error_message() const { 92 return error_message_; 93 } 94 95 bool operator==(const Status& x) const; 96 bool operator!=(const Status& x) const { 97 return !operator==(x); 98 } 99 100 // Return a combination of the error code name and message. 101 string ToString() const; 102 103 private: 104 error::Code error_code_; 105 string error_message_; 106}; 107 108// Prints a human-readable representation of 'x' to 'os'. 109LIBPROTOBUF_EXPORT ostream& operator<<(ostream& os, const Status& x); 110 111#define EXPECT_OK(value) EXPECT_TRUE((value).ok()) 112 113} // namespace util 114} // namespace protobuf 115} // namespace google 116#endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_ 117