Error.h 3.8 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
14 15 16

#pragma once

17
#include <glog/logging.h>
Y
Yu Yang 已提交
18
#include <stdio.h>
Y
Yu Yang 已提交
19 20
#include <memory>
#include <string>
21
#include "Compiler.h"
Y
Yu Yang 已提交
22 23 24

namespace paddle {

25 26 27 28
/**
 * Status is Paddle error code. It only contain a std::string as error message.
 * Although Status inherits the std::exception, but do not throw it except you
 * know what you are doing.
Y
Stash  
Yu Yang 已提交
29 30 31 32 33
 *
 *
 * There are two styles to return status in Paddle.
 *
 * 1. Return Status
34 35 36
 *    When method return a status, the return must use `__must_check` attribute.
 *    Example as below.
 * @code{cpp}
Y
Yu Yang 已提交
37
 * Error __must_check foo();
Y
Stash  
Yu Yang 已提交
38
 *
Y
Yu Yang 已提交
39
 * Error __must_check bar() {
40 41 42 43 44 45 46
 *   // do something.
 *   Status s = foo();  // invoke other method return status.
 *   if (!s.isOK()) return s;
 *   // do something else.
 *   return Status();
 * }
 * @endcode{cpp}
Y
Stash  
Yu Yang 已提交
47
 *
48 49 50 51 52
 * 2. Return by parameter.
 *    It is another way to return a status, by using a pointer parameter.
 *    Example as below.
 *
 * @code{cpp}
Y
Yu Yang 已提交
53
 * Error bar();
54
 *
Y
Yu Yang 已提交
55
 * int foo(Error* status) {
56 57 58 59 60 61 62 63
 *   // Do something.
 *   Status s = bar();
 *   if (!s.isOK()) {
 *     *status = s;
 *     return 0;
 *   }
 *   // Do something else.
 *   if (someInternalErrorHappend) {
Y
Yu Yang 已提交
64
 *     *status = ErrorF("Some dimension is too large, %d", dimension);
65 66 67 68 69 70
 *     return 0;
 *   }
 *   // End of method.
 *   return someValue;
 * }
 *
Y
Yu Yang 已提交
71 72
 * Error foobar() {
 *   Error s;
73 74 75 76 77 78 79 80 81 82
 *   // do something.
 *   foo(&s);
 *   if (!s.isOK()) return s;
 * }
 * @endcode{cpp}
 *
 *
 * Currently there is a helper method 'check' in status, because Paddle always
 * use log(FATAL) or CHECK to make program exit before. When we clean all
 * log(FATAL) and CHECK in Paddle, 'check' method will be removed.
83
 */
Y
Yu Yang 已提交
84
class Error final : public std::exception {
Y
Yu Yang 已提交
85
public:
86 87 88
  /**
   * Default Status. OK
   */
Y
Yu Yang 已提交
89
  Error() noexcept {}
Y
Yu Yang 已提交
90

91 92 93 94
  /**
   * @brief what will return the error message. If status is OK, return nullptr.
   */
  const char* what() const noexcept override {
Y
Yu Yang 已提交
95 96 97 98 99 100 101
    if (errMsg_) {
      return errMsg_->data();
    } else {
      return nullptr;
    }
  }

102 103 104 105
  /**
   * @brief isOK
   * @return true if OK.
   */
Y
Yu Yang 已提交
106 107
  inline bool isOK() const noexcept { return errMsg_ == nullptr; }

108 109 110 111 112 113 114
  /**
   * @brief check this status by glog.
   * @note It is a temp method used during cleaning Paddle code. It will be
   *       removed later.
   */
  inline void check() const { CHECK(isOK()) << what(); }

Y
Yu Yang 已提交
115 116 117 118 119 120
  /**
   * friend method to create Error.
   */
  template <typename... ARGS>
  friend Error __must_check ErrorF(const char* fmt, ARGS... args);

Y
Yu Yang 已提交
121
private:
122
  std::shared_ptr<std::string> errMsg_;
Y
Yu Yang 已提交
123 124
};

Y
Yu Yang 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/**
 * ErrorF will create an Error by printf syntax.
 *
 * Specialize this method because clang will give a warning when use printf(fmt)
 * without arguments.
 */
template <>
inline Error __must_check ErrorF(const char* msg) {
  Error e;
  e.errMsg_.reset(new std::string(msg));
  return e;
}

/**
 * ErrorF will create an Error by printf syntax.
 *
 * Examples:
 * @code{cpp}
 * auto err = ErrorF("SomeError");
 * auto err2 = ErrorF("SomeErrorWithParameter %f %d", real_val, int_val);
 * @endcode{cpp}
 */
template <typename... ARGS>
inline Error __must_check ErrorF(const char* fmt, ARGS... args) {
  constexpr size_t kBufferSize = 1024;
  char buffer[kBufferSize];
  snprintf(buffer, kBufferSize, fmt, args...);
  Error e;
  e.errMsg_.reset(new std::string(buffer));
  return e;
}

Y
Yu Yang 已提交
157
}  // namespace paddle