ORE Studio 0.0.4
Loading...
Searching...
No Matches
version_conflict_exception.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_UTILITY_REPOSITORY_VERSION_CONFLICT_EXCEPTION_HPP
21#define ORES_UTILITY_REPOSITORY_VERSION_CONFLICT_EXCEPTION_HPP
22
23#include <string>
24#include <format>
25#include <optional>
26#include <boost/exception/info.hpp>
27
28namespace ores::utility::repository {
29
38class version_conflict_exception : public virtual std::exception,
39 public virtual boost::exception {
40public:
49 version_conflict_exception(std::string_view entity_type,
50 std::string_view entity_id,
51 int expected_version,
52 int actual_version)
53 : entity_type_(entity_type),
54 entity_id_(entity_id),
55 expected_version_(expected_version),
56 actual_version_(actual_version),
57 message_(std::format(
58 "Version conflict for {} '{}': expected version {}, but current version is {}",
59 entity_type, entity_id, expected_version, actual_version)) {}
60
68 explicit version_conflict_exception(std::string_view message = "")
69 : message_(message) {}
70
71 [[nodiscard]] const char* what() const noexcept override {
72 return message_.c_str();
73 }
74
75 [[nodiscard]] const std::optional<std::string>& entity_type() const noexcept {
76 return entity_type_;
77 }
78
79 [[nodiscard]] const std::optional<std::string>& entity_id() const noexcept {
80 return entity_id_;
81 }
82
83 [[nodiscard]] std::optional<int> expected_version() const noexcept {
84 return expected_version_;
85 }
86
87 [[nodiscard]] std::optional<int> actual_version() const noexcept {
88 return actual_version_;
89 }
90
91private:
92 std::optional<std::string> entity_type_;
93 std::optional<std::string> entity_id_;
94 std::optional<int> expected_version_;
95 std::optional<int> actual_version_;
96 std::string message_;
97};
98
99}
100
101#endif
STL namespace.
Exception thrown when an optimistic locking version conflict occurs.
Definition version_conflict_exception.hpp:39
version_conflict_exception(std::string_view message="")
Constructs a version conflict exception with a custom message.
Definition version_conflict_exception.hpp:68
version_conflict_exception(std::string_view entity_type, std::string_view entity_id, int expected_version, int actual_version)
Constructs a version conflict exception with detailed information.
Definition version_conflict_exception.hpp:49