Skip to main content

reifydb_core/error/diagnostic/
transaction.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::{error::Diagnostic, fragment::Fragment};
5
6pub fn transaction_conflict() -> Diagnostic {
7	Diagnostic {
8		code: "TXN_001".to_string(),
9		rql: None,
10		message: "Transaction conflict detected - another transaction modified the same data".to_string(),
11		column: None,
12		fragment: Fragment::None,
13		label: None,
14		help: Some("Retry the transaction".to_string()),
15		notes: vec![],
16		cause: None,
17		operator_chain: None,
18	}
19}
20
21pub fn transaction_rolled_back() -> Diagnostic {
22	Diagnostic {
23		code: "TXN_002".to_string(),
24		rql: None,
25		message: "Transaction rolled back and cannot be committed".to_string(),
26		column: None,
27		fragment: Fragment::None,
28		label: None,
29		help: Some("Start a new transaction".to_string()),
30		notes: vec![],
31		cause: None,
32		operator_chain: None,
33	}
34}
35
36pub fn transaction_too_large() -> Diagnostic {
37	Diagnostic {
38		code: "TXN_003".to_string(),
39		rql: None,
40		message: "Transaction contains too many writes and exceeds size limits".to_string(),
41		column: None,
42		fragment: Fragment::None,
43		label: None,
44		help: Some("Split the transaction into smaller batches".to_string()),
45		notes: vec![],
46		cause: None,
47		operator_chain: None,
48	}
49}
50
51pub fn commit_failed(reason: String) -> Diagnostic {
52	Diagnostic {
53		code: "TXN_004".to_string(),
54		rql: None,
55		message: format!("Transaction commit failed: {}", reason),
56		column: None,
57		fragment: Fragment::None,
58		label: None,
59		help: Some("Check transaction state and retry if appropriate".to_string()),
60		notes: vec![],
61		cause: None,
62		operator_chain: None,
63	}
64}
65
66pub fn transaction_already_committed() -> Diagnostic {
67	Diagnostic {
68		code: "TXN_008".to_string(),
69		rql: None,
70		message: "Transaction was already committed".to_string(),
71		column: None,
72		fragment: Fragment::None,
73		label: None,
74		help: Some("Cannot use a transaction after it has been committed".to_string()),
75		notes: vec![],
76		cause: None,
77		operator_chain: None,
78	}
79}
80
81pub fn transaction_already_rolled_back() -> Diagnostic {
82	Diagnostic {
83		code: "TXN_009".to_string(),
84		rql: None,
85		message: "Transaction was already rolled back".to_string(),
86		column: None,
87		fragment: Fragment::None,
88		label: None,
89		help: Some("Cannot use a transaction after it has been rolled back".to_string()),
90		notes: vec![],
91		cause: None,
92		operator_chain: None,
93	}
94}
95
96pub fn key_out_of_scope(key: String) -> Diagnostic {
97	Diagnostic {
98		code: "TXN_010".to_string(),
99		rql: None,
100		message: format!("Key '{}' is not in the transaction's declared key scope", key),
101		column: None,
102		fragment: Fragment::None,
103		label: None,
104		help: Some("Declare the key when beginning the transaction or use a different transaction scope"
105			.to_string()),
106		notes: vec![],
107		cause: None,
108		operator_chain: None,
109	}
110}