Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
json_error.h
1#ifndef SOURCEMETA_CORE_JSON_ERROR_H_
2#define SOURCEMETA_CORE_JSON_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_JSON_EXPORT
5#include <sourcemeta/core/json_export.h>
6#endif
7
8#include <cstdint> // std::uint64_t
9#include <exception> // std::exception
10#include <filesystem> // std::filesystem::path
11#include <string> // std::string
12#include <string_view> // std::string_view
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
26class SOURCEMETA_CORE_JSON_EXPORT JSONParseError : public std::exception {
27public:
29 JSONParseError(const std::uint64_t line, const std::uint64_t column)
30 : line_{line}, column_{column},
31 message_{"Failed to parse the JSON document"} {}
32
34 JSONParseError(const std::uint64_t line, const std::uint64_t column,
35 const char *message)
36 : line_{line}, column_{column}, message_{message} {}
37 JSONParseError(const std::uint64_t line, const std::uint64_t column,
38 std::string message) = delete;
39 JSONParseError(const std::uint64_t line, const std::uint64_t column,
40 std::string &&message) = delete;
41 JSONParseError(const std::uint64_t line, const std::uint64_t column,
42 std::string_view message) = delete;
43
44 [[nodiscard]] auto what() const noexcept -> const char * override {
45 return this->message_;
46 }
47
49 [[nodiscard]] auto line() const noexcept -> std::uint64_t {
50 return this->line_;
51 }
52
54 [[nodiscard]] auto column() const noexcept -> std::uint64_t {
55 return this->column_;
56 }
57
58private:
59 std::uint64_t line_;
60 std::uint64_t column_;
61 const char *message_;
62};
63
66class SOURCEMETA_CORE_JSON_EXPORT JSONFileParseError : public JSONParseError {
67public:
69 JSONFileParseError(std::filesystem::path path, const std::uint64_t line,
70 const std::uint64_t column, const char *message)
71 : JSONParseError{line, column, message}, path_{std::move(path)} {}
72 JSONFileParseError(std::filesystem::path path, const std::uint64_t line,
73 const std::uint64_t column, std::string message) = delete;
74 JSONFileParseError(std::filesystem::path path, const std::uint64_t line,
75 const std::uint64_t column,
76 std::string &&message) = delete;
77 JSONFileParseError(std::filesystem::path path, const std::uint64_t line,
78 const std::uint64_t column,
79 std::string_view message) = delete;
80
82 JSONFileParseError(std::filesystem::path path, const JSONParseError &parent)
83 : JSONParseError{parent.line(), parent.column(), parent.what()},
84 path_{std::move(path)} {}
85
87 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
88 return this->path_;
89 }
90
91private:
92 std::filesystem::path path_;
93};
94
95#if defined(_MSC_VER)
96#pragma warning(default : 4251 4275)
97#endif
98
99} // namespace sourcemeta::core
100
101#endif
auto column() const noexcept -> std::uint64_t
Get the column number of the error.
Definition json_error.h:54
auto line() const noexcept -> std::uint64_t
Get the line number of the error.
Definition json_error.h:49
auto path() const noexcept -> const std::filesystem::path &
Get the file path of the error.
Definition json_error.h:87
JSONParseError(const std::uint64_t line, const std::uint64_t column, const char *message)
Create a parsing error with a custom error.
Definition json_error.h:34
JSONFileParseError(std::filesystem::path path, const JSONParseError &parent)
Create a file parsing error from a parse error.
Definition json_error.h:82
JSONParseError(const std::uint64_t line, const std::uint64_t column)
Create a parsing error.
Definition json_error.h:29
JSONFileParseError(std::filesystem::path path, const std::uint64_t line, const std::uint64_t column, const char *message)
Create a file parsing error.
Definition json_error.h:69
Definition json_error.h:66
Definition json_error.h:26