Skip to content

Commit e0fc39d

Browse files
committed
add "Transaction rolled back" for invalid batons inside transactions tursodatabase#1856
1 parent 15849e4 commit e0fc39d

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

libsql/src/hrana/stream.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ where
8181
tracing::trace!("baton not found - skipping finalize for {:?}", req);
8282
return Ok(false);
8383
}
84-
let (resp, is_autocommit) = client.finalize(req).await?;
84+
let (resp, is_autocommit) = client.finalize(req).await.map_err(|e| match e {
85+
HranaError::Api(e) if e == "Received an invalid baton" && !self.is_autocommit() => {
86+
HranaError::Api("Received an invalid baton: Transaction rolled back".into())
87+
}
88+
e => e,
89+
})?;
8590
self.inner
8691
.is_autocommit
8792
.store(is_autocommit, Ordering::SeqCst);
@@ -94,7 +99,9 @@ where
9499
(0, 0)
95100
};
96101

97-
self.inner.total_changes.fetch_add(affected_row_count, Ordering::SeqCst);
102+
self.inner
103+
.total_changes
104+
.fetch_add(affected_row_count, Ordering::SeqCst);
98105
self.inner
99106
.affected_row_count
100107
.store(affected_row_count, Ordering::SeqCst);
@@ -132,7 +139,15 @@ where
132139
StreamRequest::GetAutocommit(GetAutocommitStreamReq {}),
133140
StreamRequest::Close(CloseStreamReq {}),
134141
])
135-
.await?;
142+
.await
143+
.map_err(|e| match e {
144+
HranaError::Api(e)
145+
if e == "Received an invalid baton" && !self.is_autocommit() =>
146+
{
147+
HranaError::Api("Received an invalid baton: Transaction rolled back".into())
148+
}
149+
e => e,
150+
})?;
136151
client.reset();
137152
(resp, get_autocommit)
138153
} else {
@@ -142,7 +157,15 @@ where
142157
StreamRequest::Batch(BatchStreamReq { batch }),
143158
StreamRequest::GetAutocommit(GetAutocommitStreamReq {}),
144159
])
145-
.await?;
160+
.await
161+
.map_err(|e| match e {
162+
HranaError::Api(e)
163+
if e == "Received an invalid baton" && !self.is_autocommit() =>
164+
{
165+
HranaError::Api("Received an invalid baton: Transaction rolled back".into())
166+
}
167+
e => e,
168+
})?;
146169
(resp, get_autocommit)
147170
};
148171
drop(client);

0 commit comments

Comments
 (0)