purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 Judica, Inc.
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit/.
4
10#pragma once
11
12#include <compare>
13#include <cstdint>
14#include <string_view>
15#include <utility>
16
17#include "purify/expected.hpp"
18
19namespace purify {
20
29enum class ErrorCategory : std::uint8_t {
30 Natural,
31 Usage,
33};
34
73
75struct Error {
77
78 [[nodiscard]] constexpr ErrorCategory category() const noexcept;
79 [[nodiscard]] constexpr std::string_view name() const noexcept;
80 [[nodiscard]] constexpr std::string_view message() const noexcept;
81
82 [[nodiscard]] constexpr bool is_natural() const noexcept {
84 }
85
86 [[nodiscard]] constexpr bool is_usage_error() const noexcept {
88 }
89
90 [[nodiscard]] constexpr bool is_internal() const noexcept {
92 }
93
94 constexpr auto operator<=>(const Error&) const = default;
95};
96
98template <typename T>
100
103
144
146[[nodiscard]] constexpr std::string_view to_string(ErrorCategory category) noexcept {
147 switch (category) {
149 return "natural";
151 return "usage";
153 return "internal";
154 }
155 return "unknown_category";
156}
157
159[[nodiscard]] constexpr std::string_view to_string(ErrorCode code) noexcept {
160 switch (code) {
162 return "invalid_hex";
164 return "invalid_hex_length";
166 return "invalid_fixed_size";
168 return "overflow";
170 return "underflow";
172 return "narrowing_overflow";
174 return "division_by_zero";
176 return "bit_index_out_of_range";
178 return "range_violation";
180 return "empty_input";
182 return "size_mismatch";
184 return "missing_value";
186 return "invalid_symbol";
188 return "unsupported_symbol";
190 return "uninitialized_state";
192 return "index_out_of_range";
194 return "invalid_dimensions";
196 return "non_boolean_value";
198 return "equation_mismatch";
200 return "binding_mismatch";
202 return "io_open_failed";
204 return "io_write_failed";
206 return "entropy_unavailable";
208 return "backend_rejected_input";
210 return "hash_to_curve_exhausted";
212 return "unexpected_size";
214 return "generator_order_check_failed";
216 return "internal_mismatch";
218 return "transcript_check_failed";
219 }
220 return "unknown_error";
221}
222
224[[nodiscard]] constexpr std::string_view error_message(ErrorCode code) noexcept {
225 switch (code) {
227 return "hex input contains a non-hexadecimal character";
229 return "hex input has an invalid length";
231 return "input does not have the required fixed size";
233 return "operation overflowed the target representation";
235 return "operation underflowed the target representation";
237 return "narrowing conversion would discard non-zero bits";
239 return "division by zero is not permitted";
241 return "bit index is outside the valid range";
243 return "input is outside the documented valid range";
245 return "input must not be empty";
247 return "related inputs do not have matching sizes";
249 return "required value is missing";
251 return "symbol encoding is malformed";
253 return "symbol is well-formed but not supported";
255 return "object must be initialized before this operation";
257 return "index is outside the valid range";
259 return "inputs imply an invalid shape or dimension";
261 return "value violates a required boolean constraint";
263 return "value violates a required equality constraint";
265 return "prepared state is bound to a different secret, message, or topic";
267 return "unable to open the requested file or stream";
269 return "unable to write the requested file or stream";
271 return "unable to obtain secure operating-system randomness";
273 return "the cryptographic backend rejected the supplied input";
275 return "hash-to-curve sampling exhausted all retry attempts";
277 return "backend returned an unexpected serialized size";
279 return "fixed generator failed its subgroup order check";
281 return "internal consistency check failed";
283 return "internally generated transcript failed validation";
284 }
285 return "unknown error";
286}
287
293[[nodiscard]] constexpr Unexpected<Error> unexpected_error(ErrorCode code, [[maybe_unused]] const char* context = nullptr) {
294 return Unexpected<Error>(Error{code});
295}
296
302[[nodiscard]] constexpr Unexpected<Error> unexpected_error(Error error, [[maybe_unused]] const char* context = nullptr) {
303 return Unexpected<Error>(error);
304}
305
306inline constexpr ErrorCategory Error::category() const noexcept {
307 return error_category(code);
308}
309
310inline constexpr std::string_view Error::name() const noexcept {
311 return to_string(code);
312}
313
314inline constexpr std::string_view Error::message() const noexcept {
315 return error_message(code);
316}
317
318} // namespace purify
319
320#define PURIFY_DETAIL_CONCAT_IMPL(x, y) x##y
321#define PURIFY_DETAIL_CONCAT(x, y) PURIFY_DETAIL_CONCAT_IMPL(x, y)
322
329#define PURIFY_RETURN_IF_ERROR(expr, context) \
330 PURIFY_DETAIL_RETURN_IF_ERROR_IMPL(PURIFY_DETAIL_CONCAT(_purify_status_, __COUNTER__), expr, context)
331
338#define PURIFY_ASSIGN_OR_RETURN(lhs, expr, context) \
339 PURIFY_DETAIL_ASSIGN_OR_RETURN_IMPL(PURIFY_DETAIL_CONCAT(_purify_result_, __COUNTER__), lhs, expr, context)
340
341#define PURIFY_DETAIL_RETURN_IF_ERROR_IMPL(status_name, expr, context) \
342 auto status_name = (expr); \
343 if (!status_name.has_value()) \
344 return ::purify::unexpected_error(status_name.error(), context)
345
346#define PURIFY_DETAIL_ASSIGN_OR_RETURN_IMPL(result_name, lhs, expr, context) \
347 auto result_name = (expr); \
348 if (!result_name.has_value()) \
349 return ::purify::unexpected_error(result_name.error(), context); \
350 lhs = std::move(*result_name)
Purify result carrier that either holds a value or an error.
Definition expected.hpp:64
Stable public expected-style types used by Purify.
Definition api.hpp:21
ErrorCategory
High-level classification for all recoverable Purify errors.
Definition error.hpp:29
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
Definition error.hpp:293
ErrorCode
Machine-readable error codes shared across the library.
Definition error.hpp:42
constexpr std::string_view to_string(ErrorCategory category) noexcept
Returns a stable programmatic name for an error category.
Definition error.hpp:146
constexpr std::string_view error_message(ErrorCode code) noexcept
Returns the human-facing description for an error code.
Definition error.hpp:224
constexpr ErrorCategory error_category(ErrorCode code) noexcept
Returns the high-level category for a concrete error code.
Definition error.hpp:105
Compact error object returned by checked APIs.
Definition error.hpp:75
ErrorCode code
Definition error.hpp:76
constexpr bool is_usage_error() const noexcept
Definition error.hpp:86
constexpr bool is_natural() const noexcept
Definition error.hpp:82
constexpr std::string_view name() const noexcept
Definition error.hpp:310
constexpr std::string_view message() const noexcept
Definition error.hpp:314
constexpr auto operator<=>(const Error &) const =default
constexpr ErrorCategory category() const noexcept
Definition error.hpp:306
constexpr bool is_internal() const noexcept
Definition error.hpp:90