Codemod asserts to assertxs in the runtime
[hiphop-php.git] / hphp / runtime / base / exceptions.h
blob720646669747da9cb88c45d2a9763183663e9736
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_CPP_BASE_EXCEPTIONS_H_
18 #define incl_HPHP_CPP_BASE_EXCEPTIONS_H_
20 #include <string>
21 #include <atomic>
22 #include <utility>
24 #include <folly/String.h>
26 #include "hphp/util/portability.h"
27 #include "hphp/util/exception.h"
28 #include "hphp/runtime/base/type-array.h"
29 #include "hphp/runtime/base/req-root.h"
31 namespace HPHP {
33 //////////////////////////////////////////////////////////////////////
35 struct String;
36 struct IMarker;
38 //////////////////////////////////////////////////////////////////////
41 * ExtendedException is the exception type for C++ exceptions that carry PHP
42 * stack traces, but do not represent user-visible PHP exception objects.
44 * This class should probably eventually be merged with FatalErrorException;
45 * for now it's still here for historical reasons, though.
47 * You generally should not have to add new subclasses of these Exception types
48 * in extension code---normally you want to go through the raise_error
49 * machinery.
51 struct ExtendedException : Exception {
52 enum class SkipFrame {};
54 explicit ExtendedException();
55 explicit ExtendedException(const std::string& msg);
56 explicit ExtendedException(SkipFrame frame, const std::string& msg);
57 explicit ExtendedException(ATTRIBUTE_PRINTF_STRING const char* fmt, ...)
58 ATTRIBUTE_PRINTF(2,3);
59 ExtendedException(const ExtendedException& other);
60 ExtendedException(ExtendedException&& other) noexcept;
62 ExtendedException& operator=(const ExtendedException& other);
63 ExtendedException& operator=(ExtendedException&& other) noexcept;
65 EXCEPTION_COMMON_IMPL(ExtendedException);
67 Array getBacktrace() const;
68 void leakBacktrace() { m_btp.detach(); }
69 std::pair<String,int> getFileAndLine() const;
71 // a silent exception does not have its exception message logged
72 bool isSilent() const { return m_silent; }
73 void setSilent(bool s = true) { m_silent = s; }
75 protected:
76 ExtendedException(const std::string& msg, ArrayData* backTrace);
77 void computeBacktrace(bool skipFrame = false);
79 private:
80 req::root<Array> m_btp;
81 bool m_silent{false};
84 struct FatalErrorException : ExtendedException {
85 explicit FatalErrorException(const char *msg)
86 : ExtendedException("%s", msg)
88 FatalErrorException(int, ATTRIBUTE_PRINTF_STRING const char *msg, ...)
89 ATTRIBUTE_PRINTF(3,4);
90 FatalErrorException(const std::string&, const Array& backtrace,
91 bool isRecoverable = false);
93 EXCEPTION_COMMON_IMPL(FatalErrorException);
95 bool isRecoverable() const { return m_recoverable; }
97 private:
98 bool m_recoverable{false};
101 [[noreturn]]
102 void raise_fatal_error(const char* msg, const Array& bt = null_array,
103 bool recoverable = false, bool silent = false,
104 bool throws = true);
106 //////////////////////////////////////////////////////////////////////
108 struct ResourceExceededException : FatalErrorException {
109 ResourceExceededException(const std::string& msg, const Array& backtrace)
110 : FatalErrorException(msg, backtrace)
112 EXCEPTION_COMMON_IMPL(ResourceExceededException);
115 struct RequestTimeoutException : ResourceExceededException {
116 RequestTimeoutException(const std::string& msg, const Array& backtrace)
117 : ResourceExceededException(msg, backtrace)
119 EXCEPTION_COMMON_IMPL(RequestTimeoutException);
122 struct RequestCPUTimeoutException : ResourceExceededException {
123 RequestCPUTimeoutException(const std::string& msg, const Array& backtrace)
124 : ResourceExceededException(msg, backtrace)
126 EXCEPTION_COMMON_IMPL(RequestCPUTimeoutException);
129 struct RequestMemoryExceededException : ResourceExceededException {
130 RequestMemoryExceededException(const std::string& msg,
131 const Array& backtrace)
132 : ResourceExceededException(msg, backtrace)
134 EXCEPTION_COMMON_IMPL(RequestMemoryExceededException);
137 //////////////////////////////////////////////////////////////////////
139 extern __thread int tl_exit_code;
141 struct ExitException : ExtendedException {
142 explicit ExitException(int exitCode) {
143 tl_exit_code = exitCode;
145 EXCEPTION_COMMON_IMPL(ExitException);
148 struct PhpFileDoesNotExistException : ExtendedException {
149 explicit PhpFileDoesNotExistException(const char* file)
150 : ExtendedException("File could not be loaded: %s", file) {}
151 explicit PhpFileDoesNotExistException(const char* msg,
152 DEBUG_ONLY bool empty_file)
153 : ExtendedException("%s", msg) {
154 assertx(empty_file);
156 EXCEPTION_COMMON_IMPL(PhpFileDoesNotExistException);
159 //////////////////////////////////////////////////////////////////////
162 * These are deprecated functions for raising exceptions with certain error
163 * messages.
165 * In newer code you'll generally want to use raise_error.
167 [[noreturn]] void throw_null_pointer_exception();
168 [[noreturn]] void throw_invalid_object_type(const char* clsName);
169 [[noreturn]] void throw_not_implemented(const char* feature);
170 [[noreturn]]
171 void throw_not_supported(const char* feature, const char* reason);
174 * Initialize Throwable's file name and line number assuming the stack trace
175 * was already initialized and the current vmfp() is a built-in.
177 void throwable_init_file_and_line_from_builtin(ObjectData* throwable);
180 * Initialize Throwable's stack trace, file name and line number.
182 void throwable_init(ObjectData* throwable);
184 //////////////////////////////////////////////////////////////////////
188 #endif