Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonld_error.h
1#ifndef SOURCEMETA_CORE_JSONLD_ERROR_H_
2#define SOURCEMETA_CORE_JSONLD_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_JSONLD_EXPORT
5#include <sourcemeta/core/jsonld_export.h>
6#endif
7
8#include <sourcemeta/core/json.h>
9#include <sourcemeta/core/jsonpointer.h>
10
11#include <exception> // std::exception
12#include <initializer_list> // std::initializer_list
13#include <utility> // std::move
14
15namespace sourcemeta::core {
16
17// Exporting symbols that depends on the standard C++ library is considered
18// safe.
19// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
20#if defined(_MSC_VER)
21#pragma warning(disable : 4251 4275)
22#endif
23
28class SOURCEMETA_CORE_JSONLD_EXPORT JSONLDError : public std::exception {
29public:
31 JSONLDError(const char *code, Pointer pointer)
32 : code_{code}, pointer_{std::move(pointer)} {}
33
35 JSONLDError(const char *code, const WeakPointer &pointer)
36 : code_{code}, pointer_{to_pointer(pointer)} {}
37
40 JSONLDError(const char *code, const WeakPointer &pointer,
41 const std::initializer_list<JSON::StringView> children)
42 : code_{code}, pointer_{to_pointer(pointer)} {
43 for (const auto child : children) {
44 this->pointer_.push_back(JSON::String{child});
45 }
46 }
47
48 [[nodiscard]] auto what() const noexcept -> const char * override {
49 return this->code_.c_str();
50 }
51
54 [[nodiscard]] auto pointer() const noexcept -> const Pointer & {
55 return this->pointer_;
56 }
57
58private:
59 JSON::String code_;
60 Pointer pointer_;
61};
62
68class SOURCEMETA_CORE_JSONLD_EXPORT JSONLDFragmentError : public JSONLDError {
69public:
71 JSONLDFragmentError(const char *code, Pointer pointer, JSON::String key)
72 : JSONLDError{code, std::move(pointer)}, key_{std::move(key)} {}
73
76 [[nodiscard]] auto key() const noexcept -> const JSON::String & {
77 return this->key_;
78 }
79
80private:
81 JSON::String key_;
82};
83
84#if defined(_MSC_VER)
85#pragma warning(default : 4251 4275)
86#endif
87
88} // namespace sourcemeta::core
89
90#endif
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
Definition json_value.h:39
JSONLDError(const char *code, Pointer pointer)
Locate the error at an owned pointer.
Definition jsonld_error.h:31
JSONLDFragmentError(const char *code, Pointer pointer, JSON::String key)
Locate the error at a fragment entry.
Definition jsonld_error.h:71
JSONLDError(const char *code, const WeakPointer &pointer, const std::initializer_list< JSON::StringView > children)
Definition jsonld_error.h:40
JSONLDError(const char *code, const WeakPointer &pointer)
Locate the error at a weak pointer, materialising an owned pointer.
Definition jsonld_error.h:35
auto key() const noexcept -> const JSON::String &
Definition jsonld_error.h:76
GenericPointer< JSON::String, PropertyHashJSON< JSON::String > > Pointer
Definition jsonpointer.h:39
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto to_pointer(const JSON &document) -> Pointer
GenericPointer< std::reference_wrapper< const std::string >, PropertyHashJSON< JSON::String > > WeakPointer
Definition jsonpointer.h:44