errors.hpp 1.23 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

#include <exception>
#include <stdexcept>
#include <string>
7
#include <migraph/config.hpp>
Paul's avatar
Paul committed
8

9
10
namespace migraph {
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
11

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

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

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

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

49
} // namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
50
} // namespace migraph
Paul's avatar
Paul committed
51
52

#endif