Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
exceptions.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/core/exceptions.hh
4 *
5 * @brief Definitions of various `std::exception` children used for throwing
6 * errors with nice messages and typed discrimination.
7 *
8 *
9 * -------------------------------------------------------------------------- */
10
11#pragma once
12
13#include <nix/nixexpr.hh>
14#include <nlohmann/json.hpp>
15#include <optional>
16#include <stdexcept>
17#include <string>
18
19
20/* -------------------------------------------------------------------------- */
21
22namespace flox {
23
24/* -------------------------------------------------------------------------- */
25
80
81}; /* End enum `error_category' */
82
83
84/* -------------------------------------------------------------------------- */
85
87class FloxException : public std::exception
88{
89
90private:
91
93 std::optional<std::string> contextMsg;
94
99 std::optional<std::string> caughtMsg;
100
102 std::string whatMsg;
103
104
105public:
106
112 explicit FloxException( std::string_view contextMsg )
114 , whatMsg( "general error: " + std::string( contextMsg ) )
115 {}
116
123 explicit FloxException( std::string_view contextMsg,
124 std::string_view caughtMsg )
127 , whatMsg( "general error: " + std::string( contextMsg ) + ": "
128 + std::string( caughtMsg ) )
129 {}
130
140 explicit FloxException( std::string_view categoryMsg,
141 std::optional<std::string> contextMsg,
142 std::optional<std::string> caughtMsg )
143 : contextMsg( contextMsg ), caughtMsg( caughtMsg ), whatMsg( categoryMsg )
144 {
145 /* Finish initializing `categoryMsg` if we received `contextMsg` or
146 * `caughtMsg` values. */
147 if ( contextMsg.has_value() ) { this->whatMsg += ": " + ( *contextMsg ); }
148 if ( caughtMsg.has_value() ) { this->whatMsg += ": " + ( *caughtMsg ); }
149 }
150
151
152 [[nodiscard]] virtual error_category
153 getErrorCode() const noexcept
154 {
155 return EC_FLOX_EXCEPTION;
156 }
157
158 [[nodiscard]] std::optional<std::string>
159 getContextMessage() const noexcept
160 {
161 return this->contextMsg;
162 }
163
164 [[nodiscard]] std::optional<std::string>
165 getCaughtMessage() const noexcept
166 {
167 return this->caughtMsg;
168 }
169
170 [[nodiscard]] virtual std::string_view
171 getCategoryMessage() const noexcept
172 {
173 return "general error";
174 }
175
177 [[nodiscard]] const char *
178 what() const noexcept override
179 {
180 return this->whatMsg.c_str();
181 }
182
183
184}; /* End class `FloxException' */
185
186
187/* -------------------------------------------------------------------------- */
188
190void
191to_json( nlohmann::json & jto, const FloxException & err );
192
193
194/* -------------------------------------------------------------------------- */
195
196// NOLINTBEGIN(bugprone-macro-parentheses)
197// Disable macro parentheses lint so we can use `NAME' symbol directly.
198
206#define FLOX_DEFINE_EXCEPTION( NAME, ERROR_CODE, CATEGORY_MSG ) \
207 class NAME : public FloxException \
208 { \
209 public: \
210 \
211 NAME() : FloxException( CATEGORY_MSG, std::nullopt, std::nullopt ) {} \
212 \
213 explicit NAME( std::string_view contextMsg ) \
214 : FloxException( ( CATEGORY_MSG ), \
215 std::string( contextMsg ), \
216 std::nullopt ) \
217 {} \
218 \
219 explicit NAME( std::string_view contextMsg, std::string_view caughtMsg ) \
220 : FloxException( ( CATEGORY_MSG ), \
221 std::string( contextMsg ), \
222 std::string( caughtMsg ) ) \
223 {} \
224 \
225 [[nodiscard]] error_category \
226 getErrorCode() const noexcept override \
227 { \
228 return ( ERROR_CODE ); \
229 } \
230 \
231 [[nodiscard]] std::string_view \
232 getCategoryMessage() const noexcept override \
233 { \
234 return ( CATEGORY_MSG ); \
235 } \
236 };
237// NOLINTEND(bugprone-macro-parentheses)
238
239
240/* -------------------------------------------------------------------------- */
241
244{
245
246public:
247
248 explicit NixEvalException( std::string_view contextMsg,
249 const nix::EvalError & err )
250 : FloxException( "invalid argument",
251 std::string( contextMsg ),
252 nix::filterANSIEscapes( err.what(), true ) )
253 {}
254
255 [[nodiscard]] error_category
256 getErrorCode() const noexcept override
257 {
258 return EC_NIX_EVAL;
259 }
260
261 [[nodiscard]] std::string_view
262 getCategoryMessage() const noexcept override
263 {
264 return "Nix evaluation error";
265 }
266
267
268}; /* End class `NixEvalException' */
269
270
271/* -------------------------------------------------------------------------- */
272
273// TODO: wrap usage of `nix::flake::Fingerprint' with these.
283/* -------------------------------------------------------------------------- */
284
285} // namespace flox
286
287
288/* -------------------------------------------------------------------------- *
289 *
290 *
291 *
292 * ========================================================================== */
Definition: exceptions.hh:88
FloxException(std::string_view contextMsg)
Create a generic exception with a custom message.
Definition: exceptions.hh:112
std::optional< std::string > caughtMsg
Definition: exceptions.hh:99
const char * what() const noexcept override
Produces an explanatory string about an exception.
Definition: exceptions.hh:178
std::string whatMsg
Definition: exceptions.hh:102
FloxException(std::string_view contextMsg, std::string_view caughtMsg)
Create a generic exception with a custom message and information from a child error.
Definition: exceptions.hh:123
FloxException(std::string_view categoryMsg, std::optional< std::string > contextMsg, std::optional< std::string > caughtMsg)
Directly initialize a FloxException with a custom category message, (optional) context,...
Definition: exceptions.hh:140
std::optional< std::string > contextMsg
Definition: exceptions.hh:93
An exception thrown when a hash string is invalid.
A nix::EvalError was encountered.
Definition: exceptions.hh:244
#define FLOX_DEFINE_EXCEPTION(NAME, ERROR_CODE, CATEGORY_MSG)
Generate a class definition with an error code and category message.
Definition: exceptions.hh:206
Interfaces for use by flox.
Definition: command.cc:29
error_category
Definition: exceptions.hh:26
@ EC_JSON
Definition: exceptions.hh:67
@ EC_INVALID_HASH
Definition: exceptions.hh:75
@ EC_INVALID_PKG_QUERY_ARG
Definition: exceptions.hh:41
@ EC_ENVIRONMENT_MIXIN
Definition: exceptions.hh:79
@ EC_PARSE_RESOLVED
Definition: exceptions.hh:59
@ EC_PARSE_SEARCH_QUERY
Definition: exceptions.hh:61
@ EC_NIX
Definition: exceptions.hh:49
@ EC_PARSE_MANIFEST_DESCRIPTOR_RAW
Definition: exceptions.hh:57
@ EC_INVALID_ARG
Definition: exceptions.hh:37
@ EC_INVALID_LOCKFILE
Definition: exceptions.hh:73
@ EC_INVALID_MANIFEST_FILE
Definition: exceptions.hh:45
@ EC_INVALID_MANIFEST_DESCRIPTOR
Definition: exceptions.hh:39
@ EC_SQLITE3
Definition: exceptions.hh:65
@ EC_FLOX_EXCEPTION
Definition: exceptions.hh:35
@ EC_YAML_TO_JSON
Definition: exceptions.hh:71
@ EC_FAILURE
Definition: exceptions.hh:33
@ EC_TOML_TO_JSON
Definition: exceptions.hh:69
@ EC_RESOLUTION_FAILURE
Definition: exceptions.hh:77
@ EC_PACKAGE_INIT
Definition: exceptions.hh:55
@ EC_OKAY
Definition: exceptions.hh:28
@ EC_NIX_EVAL
Definition: exceptions.hh:51
@ EC_NIX_LOCK_FLAKE
Definition: exceptions.hh:53
@ EC_PKG_DB
Definition: exceptions.hh:63
@ EC_INVALID_REGISTRY
Definition: exceptions.hh:43
void to_json(nlohmann::json &jto, const FloxException &err)
Convert a flox::FloxException to a JSON object.
Definition: exceptions.cc:27