errors.hpp 2.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#ifndef MIGRAPHX_GUARD_ERRORS_HPP
#define MIGRAPHX_GUARD_ERRORS_HPP
Paul's avatar
Paul committed
26
27
28
29

#include <exception>
#include <stdexcept>
#include <string>
Paul's avatar
Paul committed
30
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
31

Paul's avatar
Paul committed
32
namespace migraphx {
Paul's avatar
Paul committed
33
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
34

Paul's avatar
Paul committed
35
/// Represents exceptions that can be thrown by migraphxlib
Paul's avatar
Paul committed
36
37
struct exception : std::runtime_error
{
Paul Fultz II's avatar
Paul Fultz II committed
38
39
40
41
    unsigned int error;
    exception(unsigned int e = 0, const std::string& msg = "") : std::runtime_error(msg), error(e)
    {
    }
Paul's avatar
Paul committed
42
43
};

Paul's avatar
Paul committed
44
45
/**
 * @brief Create an exception object
Paul's avatar
Paul committed
46
 *
Paul's avatar
Paul committed
47
 * @param context A message that says where the exception occurred
Paul's avatar
Paul committed
48
 * @param message Custom message for the error
Paul's avatar
Paul committed
49
50
 * @return Exceptions
 */
Paul's avatar
Paul committed
51
inline exception make_exception(const std::string& context, const std::string& message = "")
Paul's avatar
Paul committed
52
{
Paul Fultz II's avatar
Paul Fultz II committed
53
54
55
56
57
58
59
    return {0, context + ": " + message};
}

inline exception
make_exception(const std::string& context, unsigned int e, const std::string& message = "")
{
    return {e, context + ": " + message};
Paul's avatar
Paul committed
60
61
}

Paul's avatar
Paul committed
62
63
/**
 * @brief Create a message of a file location
Paul's avatar
Paul committed
64
 *
Paul's avatar
Paul committed
65
66
 * @param file The filename
 * @param line The line number
Paul's avatar
Paul committed
67
 *
Paul's avatar
Paul committed
68
69
 * @return A string that represents the file location
 */
70
inline std::string make_source_context(const std::string& file, int line, const std::string& fname)
Paul's avatar
Paul committed
71
{
72
    return file + ":" + std::to_string(line) + ": " + fname;
Paul's avatar
Paul committed
73
74
}

75
76
77
// NOLINTNEXTLINE
#define MIGRAPHX_MAKE_SOURCE_CTX() migraphx::make_source_context(__FILE__, __LINE__, __func__)

Paul's avatar
Paul committed
78
79
80
/**
 * @brief Throw an exception with context information
 */
81
#define MIGRAPHX_THROW(...) throw migraphx::make_exception(MIGRAPHX_MAKE_SOURCE_CTX(), __VA_ARGS__)
Paul's avatar
Paul committed
82

Paul's avatar
Paul committed
83
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
84
} // namespace migraphx
Paul's avatar
Paul committed
85
86

#endif