1 /*
2 * Error.cpp
3 *
4 * This source file is part of the FoundationDB open source project
5 *
6 * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 #include "flow/Error.h"
22 #include "flow/Trace.h"
23 #include "flow/Knobs.h"
24 #include <iostream>
25 using std::cout;
26 using std::endl;
27 using std::make_pair;
28
29 bool g_crashOnError = false;
30
errorCounts()31 std::map<int, int>& Error::errorCounts() {
32 static std::map<int, int> counts;
33 return counts;
34 }
35
36 #include <iostream>
37
38 extern void flushTraceFileVoid();
39
fromUnvalidatedCode(int code)40 Error Error::fromUnvalidatedCode(int code) {
41 if (code < 0 || code > 30000) {
42 Error e = Error::fromCode(error_code_unknown_error);
43 TraceEvent(SevWarn, "ConvertedUnvalidatedErrorCode").error(e).detail("OriginalCode", code);
44 return e;
45 }
46 else
47 return Error::fromCode(code);
48 }
49
internal_error_impl(const char * file,int line)50 Error internal_error_impl( const char* file, int line ) {
51 fprintf(stderr, "Internal Error @ %s %d:\n %s\n", file, line, platform::get_backtrace().c_str());
52
53 TraceEvent(SevError, "InternalError")
54 .error(Error::fromCode(error_code_internal_error))
55 .detail("File", file)
56 .detail("Line", line)
57 .backtrace();
58 flushTraceFileVoid();
59 return Error(error_code_internal_error);
60 }
61
Error(int error_code)62 Error::Error(int error_code)
63 : error_code(error_code), flags(0)
64 {
65 if (TRACE_SAMPLE()) TraceEvent(SevSample, "ErrorCreated").detail("ErrorCode", error_code);
66 //std::cout << "Error: " << error_code << std::endl;
67 if (error_code >= 3000 && error_code < 6000) {
68 TraceEvent(SevError, "SystemError").error(*this).backtrace();
69 if (g_crashOnError) {
70 flushOutputStreams();
71 flushTraceFileVoid();
72 crashAndDie();
73 }
74 }
75 /*if (error_code)
76 errorCounts()[error_code]++;*/
77 }
78
errorCodeTable()79 ErrorCodeTable& Error::errorCodeTable() {
80 static ErrorCodeTable table;
81 return table;
82 }
83
name() const84 const char* Error::name() const {
85 auto table = errorCodeTable();
86 auto it = table.find(error_code);
87 if (it == table.end()) return "UNKNOWN_ERROR";
88 return it->second.first;
89 }
90
what() const91 const char* Error::what() const {
92 auto table = errorCodeTable();
93 auto it = table.find(error_code);
94 if (it == table.end()) return "UNKNOWN_ERROR";
95 return it->second.second;
96 }
97
init()98 void Error::init() {
99 errorCodeTable();
100 }
101
asInjectedFault() const102 Error Error::asInjectedFault() const {
103 Error e = *this;
104 e.flags |= FLAG_INJECTED_FAULT;
105 return e;
106 }
107
ErrorCodeTable()108 ErrorCodeTable::ErrorCodeTable() {
109 #define ERROR(name, number, description) addCode(number, #name, description); enum { Duplicate_Error_Code_##number = 0 };
110 #include "error_definitions.h"
111 }
112
addCode(int code,const char * name,const char * description)113 void ErrorCodeTable::addCode(int code, const char *name, const char *description) {
114 (*this)[code] = std::make_pair(name, description);
115 }
116
isAssertDisabled(int line)117 bool isAssertDisabled(int line) {
118 return FLOW_KNOBS && (FLOW_KNOBS->DISABLE_ASSERTS == -1 || FLOW_KNOBS->DISABLE_ASSERTS == line);
119 }
120
breakpoint_me()121 void breakpoint_me() {
122 return;
123 }
124