MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Exception.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
7#include <exception>
8#include <string>
9#include <fmt/format.h>
10#include <rdma/fi_errno.h>
11#include <mxl/mxl.h>
13
15{
20
27 class Exception : public std::exception
28 {
29 public:
30 Exception(std::string msg, mxlStatus status);
31
34 template<typename... T>
35 static Exception make(mxlStatus status, fmt::format_string<T...> fmt, T&&... args)
36 {
37 return Exception(fmt::format(fmt, std::forward<T>(args)...), status);
38 }
39
42 template<typename... T>
43 static Exception invalidArgument(fmt::format_string<T...> fmt, T&&... args)
44 {
45 return make(MXL_ERR_INVALID_ARG, fmt, std::forward<T>(args)...);
46 }
47
50 template<typename... T>
51 static Exception internal(fmt::format_string<T...> fmt, T&&... args)
52 {
53 return make(MXL_ERR_INTERNAL, fmt, std::forward<T>(args)...);
54 }
55
58 template<typename... T>
59 static Exception invalidState(fmt::format_string<T...> fmt, T&&... args)
60 {
61 return make(MXL_ERR_INVALID_STATE, fmt, std::forward<T>(args)...);
62 }
63
66 template<typename... T>
67 static Exception exists(fmt::format_string<T...> fmt, T&&... args)
68 {
69 return make(MXL_ERR_EXISTS, fmt, std::forward<T>(args)...);
70 }
71
74 template<typename... T>
75 static Exception notFound(fmt::format_string<T...> fmt, T&&... args)
76 {
77 return make(MXL_ERR_NOT_FOUND, fmt, std::forward<T>(args)...);
78 }
79
82 template<typename... T>
83 static Exception interrupted(fmt::format_string<T...> fmt, T&&... args)
84 {
85 return make(MXL_ERR_INTERRUPTED, fmt, std::forward<T>(args)...);
86 }
87
88 template<typename... T>
89 static Exception unsupportedOperation(fmt::format_string<T...> fmt, T&&... args)
90 {
91 return make(MXL_ERR_UNSUPPORTED_OPERATION, fmt, std::forward<T>(args)...);
92 }
93
94 template<typename... T>
95 static Exception noFabric(fmt::format_string<T...> fmt, T&&... args)
96 {
97 return make(MXL_ERR_NO_FABRIC, fmt, std::forward<T>(args)...);
98 }
99
103 [[nodiscard]]
104 mxlStatus status() const noexcept;
105
108 [[nodiscard]]
109 char const* what() const noexcept override;
110
111 private:
112 std::string _msg;
114 };
115
121 {
122 public:
123 FabricException(std::string msg, mxlStatus status, int fiErrno);
124
132 template<typename... T>
133 static FabricException make(int fiErrno, fmt::format_string<T...> fmt, T&&... args)
134 {
135 return FabricException(fmt::format(fmt, std::forward<T>(args)...), mxlStatusFromFiErrno(fiErrno), fiErrno);
136 }
137
138 [[nodiscard]]
139 int fiErrno() const noexcept;
140
141 [[nodiscard]]
142 bool isInterrupted() const noexcept;
143
144 private:
146 };
147
154 template<typename F, typename... T>
155 int fiCall(F&& fun, std::string_view msg, T&&... args)
156 {
157 int result = fun(std::forward<T>(args)...);
158 if (result < 0)
159 {
160 auto str = ::fi_strerror(result);
161 throw FabricException::make(result, "{}: {}, code {}", msg, str, result);
162 }
163
164 return result;
165 }
166
167 template<typename F>
168 void catchAndLogFabricError(F&& fun, std::string_view msg)
169 {
170 try
171 {
172 fun();
173 }
174 catch (FabricException const& e)
175 {
176 MXL_ERROR("FabricException caught: {}: {}", msg, e.what());
177 }
178 }
179}
#define MXL_ERROR(...)
Definition Logging.hpp:67
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
Definition Exception.hpp:43
static Exception interrupted(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INTERRUPTED exception.
Definition Exception.hpp:83
char const * what() const noexcept override
Implements std::exception, returns a descriptive string about the error.
Definition Exception.cpp:21
static Exception internal(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INTERNAL exception.
Definition Exception.hpp:51
static Exception make(mxlStatus status, fmt::format_string< T... > fmt, T &&... args)
Make any type of exception.
Definition Exception.hpp:35
static Exception noFabric(fmt::format_string< T... > fmt, T &&... args)
Definition Exception.hpp:95
Exception(std::string msg, mxlStatus status)
Definition Exception.cpp:11
static Exception notFound(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_NOT_FOUND exception.
Definition Exception.hpp:75
static Exception unsupportedOperation(fmt::format_string< T... > fmt, T &&... args)
Definition Exception.hpp:89
static Exception exists(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_EXISTS exception.
Definition Exception.hpp:67
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
Definition Exception.hpp:59
mxlStatus status() const noexcept
Return the mxlStatus status code that describes the condition that led to the exception being thrown.
Definition Exception.cpp:16
An internal exception type that extends.
FabricException(std::string msg, mxlStatus status, int fiErrno)
Definition Exception.cpp:26
static FabricException make(int fiErrno, fmt::format_string< T... > fmt, T &&... args)
Create a new exception object.
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_INVALID_STATE
Definition mxl.h:47
@ MXL_ERR_INTERRUPTED
Definition mxl.h:45
@ MXL_ERR_INTERNAL
Definition mxl.h:48
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
@ MXL_ERR_UNSUPPORTED_OPERATION
Definition mxl.h:52
@ MXL_ERR_EXISTS
Definition mxl.h:51
@ MXL_ERR_NO_FABRIC
Definition mxl.h:46
@ MXL_ERR_NOT_FOUND
Definition mxl.h:50
void catchAndLogFabricError(F &&fun, std::string_view msg)
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.
mxlStatus mxlStatusFromFiErrno(int fiErrno)
Convert an error code returned by libfabric to an appropriate mxlStatus code.
Definition Exception.cpp:41