forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherr_common.cpp
More file actions
226 lines (192 loc) · 5.65 KB
/
err_common.cpp
File metadata and controls
226 lines (192 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <af/exception.h>
#include <af/device.h>
#include <err_common.hpp>
#include <type_util.hpp>
#include <util.hpp>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#if defined(WITH_GRAPHICS) && !defined(AF_UNIFIED)
#include <graphics_common.hpp>
#endif
using std::string;
using std::stringstream;
AfError::AfError(const char * const func,
const char * const file,
const int line,
const char * const message, af_err err)
: logic_error (message),
functionName (func),
fileName (file),
lineNumber(line),
error(err)
{}
AfError::AfError(string func,
string file,
const int line,
string message, af_err err)
: logic_error (message),
functionName (func),
fileName (file),
lineNumber(line),
error(err)
{}
const string&
AfError::getFunctionName() const
{
return functionName;
}
const string&
AfError::getFileName() const
{
return fileName;
}
int
AfError::getLine() const
{
return lineNumber;
}
af_err
AfError::getError() const
{
return error;
}
AfError::~AfError() throw() {}
TypeError::TypeError(const char * const func,
const char * const file,
const int line,
const int index, const af_dtype type)
: AfError (func, file, line, "Invalid data type", AF_ERR_TYPE),
argIndex(index),
errTypeName(getName(type))
{}
const string& TypeError::getTypeName() const
{
return errTypeName;
}
int TypeError::getArgIndex() const
{
return argIndex;
}
ArgumentError::ArgumentError(const char * const func,
const char * const file,
const int line,
const int index,
const char * const expectString)
: AfError(func, file, line, "Invalid argument", AF_ERR_ARG),
argIndex(index),
expected(expectString)
{
}
const string& ArgumentError::getExpectedCondition() const
{
return expected;
}
int ArgumentError::getArgIndex() const
{
return argIndex;
}
SupportError::SupportError(const char * const func,
const char * const file,
const int line,
const char * const back)
: AfError(func, file, line, "Unsupported Error", AF_ERR_NOT_SUPPORTED),
backend(back)
{}
const string& SupportError::getBackendName() const
{
return backend;
}
DimensionError::DimensionError(const char * const func,
const char * const file,
const int line,
const int index,
const char * const expectString)
: AfError(func, file, line, "Invalid size", AF_ERR_SIZE),
argIndex(index),
expected(expectString)
{
}
const string& DimensionError::getExpectedCondition() const
{
return expected;
}
int DimensionError::getArgIndex() const
{
return argIndex;
}
void
print_error(const string &msg)
{
std::string perr = getEnvVar("AF_PRINT_ERRORS");
if(!perr.empty()) {
if(perr != "0")
fprintf(stderr, "%s\n", msg.c_str());
}
get_global_error_string() = msg;
}
af_err processException()
{
stringstream ss;
af_err err= AF_ERR_INTERNAL;
try {
throw;
} catch (const DimensionError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< "Invalid dimension for argument " << ex.getArgIndex() << "\n"
<< "Expected: " << ex.getExpectedCondition() << "\n";
print_error(ss.str());
err = AF_ERR_SIZE;
} catch (const ArgumentError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< "Invalid argument at index " << ex.getArgIndex() << "\n"
<< "Expected: " << ex.getExpectedCondition() << "\n";
print_error(ss.str());
err = AF_ERR_ARG;
} catch (const SupportError &ex) {
ss << ex.getFunctionName()
<< " not supported for " << ex.getBackendName()
<< " backend\n";
print_error(ss.str());
err = AF_ERR_NOT_SUPPORTED;
} catch (const TypeError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< "Invalid type for argument " << ex.getArgIndex() << "\n";
print_error(ss.str());
err = AF_ERR_TYPE;
} catch (const AfError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< ex.what() << "\n";
print_error(ss.str());
err = ex.getError();
#if defined(WITH_GRAPHICS) && !defined(AF_UNIFIED)
} catch (const fg::Error &ex) {
ss << ex << "\n";
print_error(ss.str());
err = AF_ERR_INTERNAL;
#endif
} catch (...) {
print_error(ss.str());
err = AF_ERR_UNKNOWN;
}
return err;
}
std::string& get_global_error_string()
{
static std::string global_error_string = std::string("");
return global_error_string;
}