errors.hpp 1.12 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_ERRORS_HPP
#define MIGRAPH_GUARD_ERRORS_HPP
Paul's avatar
Paul committed
3
4
5
6
7

#include <exception>
#include <stdexcept>
#include <string>

Paul's avatar
Paul committed
8
namespace migraph {
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
/// Represents exceptions that can be thrown by migraphlib
Paul's avatar
Paul committed
11
12
struct exception : std::runtime_error
{
Paul's avatar
Paul committed
13
    exception(std::string msg = "") : std::runtime_error(msg) {}
Paul's avatar
Paul committed
14
15
};

Paul's avatar
Paul committed
16
17
/**
 * @brief Create an exception object
Paul's avatar
Paul committed
18
 *
Paul's avatar
Paul committed
19
 * @param context A message that says where the exception occurred
Paul's avatar
Paul committed
20
 * @param message Custom message for the error
Paul's avatar
Paul committed
21
22
 * @return Exceptions
 */
Paul's avatar
Paul committed
23
24
25
26
27
inline exception make_exception(std::string context, std::string message = "")
{
    return {context + ": " + message};
}

Paul's avatar
Paul committed
28
29
/**
 * @brief Create a message of a file location
Paul's avatar
Paul committed
30
 *
Paul's avatar
Paul committed
31
32
 * @param file The filename
 * @param line The line number
Paul's avatar
Paul committed
33
 *
Paul's avatar
Paul committed
34
35
 * @return A string that represents the file location
 */
Paul's avatar
Paul committed
36
37
38
39
40
inline std::string make_source_context(const std::string& file, int line)
{
    return file + ":" + std::to_string(line);
}

Paul's avatar
Paul committed
41
42
43
/**
 * @brief Throw an exception with context information
 */
Paul's avatar
Paul committed
44
45
#define MIGRAPH_THROW(...) \
    throw migraph::make_exception(migraph::make_source_context(__FILE__, __LINE__), __VA_ARGS__)
Paul's avatar
Paul committed
46

Paul's avatar
Paul committed
47
} // namespace migraph
Paul's avatar
Paul committed
48
49

#endif