sync the repo
[hiphop-php.git] / hphp / runtime / base / exceptions.h
blob293e91d8954a2651484ce4bab39c2c8c6b445dc8
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 #pragma once
19 #include <string>
20 #include <atomic>
21 #include <utility>
23 #include <folly/String.h>
25 #include "hphp/util/portability.h"
26 #include "hphp/util/exception.h"
27 #include "hphp/runtime/base/location.h"
28 #include "hphp/runtime/base/type-array.h"
29 #include "hphp/runtime/base/req-root.h"
30 #include "hphp/util/rds-local.h"
32 namespace HPHP {
34 //////////////////////////////////////////////////////////////////////
36 struct c_WaitableWaitHandle;
39 * ExtendedException is the exception type for C++ exceptions that carry PHP
40 * stack traces, but do not represent user-visible PHP exception objects.
42 * This class should probably eventually be merged with FatalErrorException;
43 * for now it's still here for historical reasons, though.
45 * You generally should not have to add new subclasses of these Exception types
46 * in extension code---normally you want to go through the raise_error
47 * machinery.
49 struct ExtendedException : Exception {
50 enum class SkipFrame {};
52 explicit ExtendedException();
53 explicit ExtendedException(const std::string& msg);
54 explicit ExtendedException(SkipFrame frame, const std::string& msg);
55 explicit ExtendedException(ATTRIBUTE_PRINTF_STRING const char* fmt, ...)
56 ATTRIBUTE_PRINTF(2,3);
57 ExtendedException(const ExtendedException& other);
58 ExtendedException(ExtendedException&& other) noexcept;
60 ExtendedException& operator=(const ExtendedException& other);
61 ExtendedException& operator=(ExtendedException&& other) noexcept;
63 EXCEPTION_COMMON_IMPL(ExtendedException);
65 Array getBacktrace() const;
66 void leakBacktrace() { m_btp.detach(); }
67 std::pair<String,int> getFileAndLine() const;
69 // a silent exception does not have its exception message logged
70 bool isSilent() const { return m_silent; }
71 void setSilent(bool s = true) { m_silent = s; }
72 void recomputeBacktraceFromWH(c_WaitableWaitHandle* wh);
74 protected:
75 ExtendedException(const std::string& msg, ArrayData* backTrace);
76 void computeBacktrace(bool skipFrame = false);
78 private:
79 req::root<Array> m_btp;
80 bool m_silent{false};
83 struct FatalErrorException : ExtendedException {
84 explicit FatalErrorException(const char *msg)
85 : ExtendedException("%s", msg)
87 FatalErrorException(int, ATTRIBUTE_PRINTF_STRING const char *msg, ...)
88 ATTRIBUTE_PRINTF(3,4);
89 FatalErrorException(const std::string&, const Array& backtrace,
90 bool isRecoverable = false);
92 EXCEPTION_COMMON_IMPL(FatalErrorException);
94 bool isRecoverable() const { return m_recoverable; }
96 private:
97 bool m_recoverable{false};
100 [[noreturn]]
101 void raise_fatal_error(const char* msg, const Array& bt = null_array,
102 bool recoverable = false, bool silent = false,
103 bool throws = true);
105 [[noreturn]] void raise_parse_error(const StringData*,
106 const char*,
107 const Location::Range& loc);
109 //////////////////////////////////////////////////////////////////////
111 struct ResourceExceededException : FatalErrorException {
112 ResourceExceededException(const std::string& msg, const Array& backtrace)
113 : FatalErrorException(msg, backtrace)
115 EXCEPTION_COMMON_IMPL(ResourceExceededException);
118 struct RequestTimeoutException : ResourceExceededException {
119 RequestTimeoutException(const std::string& msg, const Array& backtrace)
120 : ResourceExceededException(msg, backtrace)
122 EXCEPTION_COMMON_IMPL(RequestTimeoutException);
125 struct RequestCPUTimeoutException : ResourceExceededException {
126 RequestCPUTimeoutException(const std::string& msg, const Array& backtrace)
127 : ResourceExceededException(msg, backtrace)
129 EXCEPTION_COMMON_IMPL(RequestCPUTimeoutException);
132 struct RequestMemoryExceededException : ResourceExceededException {
133 RequestMemoryExceededException(const std::string& msg,
134 const Array& backtrace)
135 : ResourceExceededException(msg, backtrace)
137 EXCEPTION_COMMON_IMPL(RequestMemoryExceededException);
140 struct RequestOOMKilledException : ResourceExceededException {
141 explicit RequestOOMKilledException(size_t usedBytes)
142 : ResourceExceededException(
143 folly::sformat("request aborted due to memory pressure, "
144 "used {} bytes", usedBytes),
145 empty_vec_array())
146 , m_usedBytes(usedBytes)
148 const size_t m_usedBytes;
149 EXCEPTION_COMMON_IMPL(RequestOOMKilledException);
152 //////////////////////////////////////////////////////////////////////
154 extern RDS_LOCAL(int, rl_exit_code);
156 struct ExitException : ExtendedException {
157 explicit ExitException(int exitCode) {
158 *rl_exit_code = exitCode;
160 EXCEPTION_COMMON_IMPL(ExitException);
163 struct PhpFileDoesNotExistException : ExtendedException {
164 explicit PhpFileDoesNotExistException(const char* file)
165 : ExtendedException("File could not be loaded: %s", file) {}
166 explicit PhpFileDoesNotExistException(const char* msg,
167 DEBUG_ONLY bool empty_file)
168 : ExtendedException("%s", msg) {
169 assertx(empty_file);
171 EXCEPTION_COMMON_IMPL(PhpFileDoesNotExistException);
174 //////////////////////////////////////////////////////////////////////
177 * These are deprecated functions for raising exceptions with certain error
178 * messages.
180 * In newer code you'll generally want to use raise_error.
182 [[noreturn]] void throw_null_pointer_exception();
183 [[noreturn]] void throw_invalid_object_type(const char* clsName);
184 [[noreturn]] void throw_not_implemented(const char* feature);
185 [[noreturn]]
186 void throw_not_supported(const char* feature, const char* reason);
187 [[noreturn]] void throw_stack_overflow();
190 * Initialize Throwable's file name and line number assuming the stack trace
191 * was already initialized and the current vmfp() is a built-in.
193 void throwable_init_file_and_line_from_builtin(ObjectData* throwable);
196 * Initialize Throwable's stack trace, file name and line number.
198 void throwable_init(ObjectData* throwable);
201 * Reinitialize Throwable's stack trace, file name and line number based on wait
202 * handle.
204 void throwable_recompute_backtrace_from_wh(ObjectData* throwable,
205 c_WaitableWaitHandle* wh);
208 * Mark the result of an array cast on a throwable object so that it's safe
209 * to perform Hack-Array-Migration-sensitive operations on it.
211 void throwable_mark_array(const ObjectData* throwable, Array& props);
213 String throwable_to_string(ObjectData* throwable);
215 //////////////////////////////////////////////////////////////////////