Skip to content

Commit 00f7ddb

Browse files
committed
[types] User anyhow as error
1 parent a7d77c9 commit 00f7ddb

File tree

13 files changed

+82
-97
lines changed

13 files changed

+82
-97
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

arangodb-types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test = []
99
db_mutex = ["log", "rand", "tokio"]
1010

1111
[dependencies]
12+
anyhow = "1.0.51"
1213
arangors = { version = "0.5.0", features = ["arango3_7"] }
1314
arcstr = { version = "1.1.1", features = ["serde", "substr-usize-indices"] }
1415
async-trait = "0.1.51"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub static ARANGODB_WAP_FN_TEST: &str = "test";
2-
pub static ARANGODB_WAP_FN_TEST_CODE: &str = include_str!("test.js");
1+
pub static ARANGODB_FUNCTION_TEST: &str = "test";
2+
pub static ARANGODB_FUNCTION_TEST_CODE: &str = include_str!("test.js");

arangodb-types/src/traits/collection.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::HashSet;
2-
use std::error::Error;
32
use std::sync::Arc;
43

54
use arangors::{AqlOptions, AqlQuery};
@@ -36,7 +35,7 @@ pub trait DBCollection: Send + Sync {
3635
fn db_info(&self) -> &Arc<DBInfo>;
3736

3837
/// Gets the arangodb instance of this collection.
39-
async fn db_collection(&self) -> Result<Collection, Box<dyn Error>> {
38+
async fn db_collection(&self) -> Result<Collection, anyhow::Error> {
4039
let db_info = self.db_info();
4140
Ok(db_info.database.collection(Self::name()).await?)
4241
}
@@ -51,7 +50,7 @@ pub trait DBCollection: Send + Sync {
5150
async fn exists_by_key(
5251
&self,
5352
key: &<Self::Document as DBDocument>::Key,
54-
) -> Result<bool, Box<dyn Error>> {
53+
) -> Result<bool, anyhow::Error> {
5554
Ok(self.get_one_by_key(key, None).await?.is_some())
5655
}
5756

@@ -60,15 +59,15 @@ pub trait DBCollection: Send + Sync {
6059
&self,
6160
property_path: &str,
6261
value: &V,
63-
) -> Result<bool, Box<dyn Error>> {
62+
) -> Result<bool, anyhow::Error> {
6463
Ok(self.get_one_by(property_path, value, None).await?.is_some())
6564
}
6665

6766
/// Gets all documents in the collection. Useful for cache.
6867
async fn get_all(
6968
&self,
7069
return_fields: Option<&Self::Document>,
71-
) -> Result<Vec<Self::Document>, Box<dyn Error>> {
70+
) -> Result<Vec<Self::Document>, anyhow::Error> {
7271
// Prepare AQL.
7372
// FOR i IN <collection>
7473
// RETURN i
@@ -90,7 +89,7 @@ pub trait DBCollection: Send + Sync {
9089
&self,
9190
key: &<<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::Key,
9291
return_fields: Option<&Self::Document>,
93-
) -> Result<Option<Self::Document>, Box<dyn Error>> {
92+
) -> Result<Option<Self::Document>, anyhow::Error> {
9493
let result = self
9594
.get_one_by(&DBDocumentField::Key.path(), &key, return_fields)
9695
.await?;
@@ -102,7 +101,7 @@ pub trait DBCollection: Send + Sync {
102101
&self,
103102
keys: &[<<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::Key],
104103
return_fields: Option<&Self::Document>,
105-
) -> Result<Vec<Option<Self::Document>>, Box<dyn Error>> {
104+
) -> Result<Vec<Option<Self::Document>>, anyhow::Error> {
106105
// Prepare AQL.
107106
// FOR i IN <keys>
108107
// LET o = Document(<collection>, i)
@@ -134,7 +133,7 @@ pub trait DBCollection: Send + Sync {
134133
&self,
135134
keys: &[<<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::Key],
136135
return_fields: Option<&Self::Document>,
137-
) -> Result<Vec<Self::Document>, Box<dyn Error>> {
136+
) -> Result<Vec<Self::Document>, anyhow::Error> {
138137
// Prepare AQL.
139138
// FOR i IN <keys>
140139
// LET o = Document(<collection>, i)
@@ -169,7 +168,7 @@ pub trait DBCollection: Send + Sync {
169168
property_path: &str,
170169
value: &V,
171170
return_fields: Option<&Self::Document>,
172-
) -> Result<Option<Self::Document>, Box<dyn Error>> {
171+
) -> Result<Option<Self::Document>, anyhow::Error> {
173172
let mut result = self
174173
.get_many_by(property_path, value, Some(1), return_fields)
175174
.await?;
@@ -185,7 +184,7 @@ pub trait DBCollection: Send + Sync {
185184
value: &V,
186185
limit: Option<u64>,
187186
return_fields: Option<&Self::Document>,
188-
) -> Result<Vec<Self::Document>, Box<dyn Error>> {
187+
) -> Result<Vec<Self::Document>, anyhow::Error> {
189188
// Prepare AQL.
190189
// FOR i IN <collection>
191190
// FILTER i.<property> == <value>
@@ -225,7 +224,7 @@ pub trait DBCollection: Send + Sync {
225224
async fn update_list_with_retries(
226225
&self,
227226
documents: &[Self::Document],
228-
) -> Result<(), Box<dyn Error>> {
227+
) -> Result<(), anyhow::Error> {
229228
// FOR i IN <documents>
230229
// UPDATE i WITH i IN <collection> OPTIONS { ignoreErrors: true }
231230
// RETURN NEW._key
@@ -271,15 +270,15 @@ pub trait DBCollection: Send + Sync {
271270
async fn send_aql<'a>(
272271
&self,
273272
aql: &AqlBuilder<'a>,
274-
) -> Result<AqlResult<Self::Document>, Box<dyn Error>> {
273+
) -> Result<AqlResult<Self::Document>, anyhow::Error> {
275274
self.send_generic_aql(aql).await
276275
}
277276

278277
/// Sends an AQL command.
279278
async fn send_generic_aql<'a, R: Send + Sync + for<'de> Deserialize<'de>>(
280279
&self,
281280
aql: &AqlBuilder<'a>,
282-
) -> Result<AqlResult<R>, Box<dyn Error>> {
281+
) -> Result<AqlResult<R>, anyhow::Error> {
283282
let db_info = self.db_info();
284283

285284
let batch_size = aql.batch_size();
@@ -348,7 +347,7 @@ pub trait DBCollection: Send + Sync {
348347
&self,
349348
aql: &mut AqlBuilder<'a>,
350349
checker: F,
351-
) -> Result<(), Box<dyn Error>>
350+
) -> Result<(), anyhow::Error>
352351
where
353352
F: FnMut(AqlResult<Self::Document>, &mut AqlBuilder<'a>) -> bool + Send,
354353
{
@@ -365,7 +364,7 @@ pub trait DBCollection: Send + Sync {
365364
&self,
366365
aql: &mut AqlBuilder<'a>,
367366
mut checker: F,
368-
) -> Result<(), Box<dyn Error>>
367+
) -> Result<(), anyhow::Error>
369368
where
370369
F: FnMut(AqlResult<R>, &mut AqlBuilder<'a>) -> bool + Send,
371370
{
@@ -380,18 +379,21 @@ pub trait DBCollection: Send + Sync {
380379
aql_retry += 1;
381380
}
382381

383-
return Err(format!("Maximum AQL retries reached for '{:?}'", aql).into());
382+
return Err(anyhow::anyhow!(
383+
"Maximum AQL retries reached for '{:?}'",
384+
aql
385+
));
384386
}
385387

386388
/// Removes all documents from the collection.
387-
async fn truncate(&self) -> Result<(), Box<dyn Error>> {
389+
async fn truncate(&self) -> Result<(), anyhow::Error> {
388390
let db_info = self.db_collection().await?;
389391
db_info.truncate().await?;
390392
Ok(())
391393
}
392394

393395
/// Drops the colle ction.
394-
async fn drop_collection(&self) -> Result<(), Box<dyn Error>> {
396+
async fn drop_collection(&self) -> Result<(), anyhow::Error> {
395397
let db_info = self.db_collection().await?;
396398
db_info.drop().await?;
397399
Ok(())

arangodb-types/src/traits/collection_edge.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::error::Error;
2-
31
use async_trait::async_trait;
42

53
use crate::aql::AqlBuilder;
@@ -21,7 +19,7 @@ pub trait DBEdgeCollection: DBCollection {
2119
&self,
2220
key: &DBId<<<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::Key, <<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::CollectionType>,
2321
return_fields: Option<&Self::Document>,
24-
) -> Result<Option<Self::Document>, Box<dyn Error>> {
22+
) -> Result<Option<Self::Document>, anyhow::Error> {
2523
let result = self
2624
.get_one_by(&DBDocumentField::From.path(), &key, return_fields)
2725
.await?;
@@ -33,7 +31,7 @@ pub trait DBEdgeCollection: DBCollection {
3331
&self,
3432
key: &DBId<<<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::Key, <<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::CollectionType>,
3533
return_fields: Option<&Self::Document>,
36-
) -> Result<Option<Self::Document>, Box<dyn Error>> {
34+
) -> Result<Option<Self::Document>, anyhow::Error> {
3735
let result = self
3836
.get_one_by(&DBDocumentField::To.path(), &key, return_fields)
3937
.await?;
@@ -46,7 +44,7 @@ pub trait DBEdgeCollection: DBCollection {
4644
from: &DBId<<<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::Key, <<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::CollectionType>,
4745
to: &DBId<<<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::Key, <<Self as crate::traits::collection::DBCollection>::Document as DBDocument>::CollectionType>,
4846
return_fields: Option<&Self::Document>,
49-
) -> Result<Option<Self::Document>, Box<dyn Error>> {
47+
) -> Result<Option<Self::Document>, anyhow::Error> {
5048
// Prepare AQL.
5149
// FOR i IN <collection>
5250
// FILTER i._from == <from> && i._to == <to>

arangodb-types/src/traits/document.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::error::Error;
1+
use std::fmt::Debug;
22
use std::hash::Hash;
33

44
use arangors::document::options::{InsertOptions, OverwriteMode, RemoveOptions, UpdateOptions};
@@ -16,7 +16,8 @@ use crate::types::DBId;
1616
pub trait DBDocument:
1717
Send + Sync + Clone + Serialize + for<'de> Deserialize<'de> + AQLMapping
1818
{
19-
type Key: ToString
19+
type Key: Debug
20+
+ ToString
2021
+ Eq
2122
+ PartialEq
2223
+ Clone
@@ -61,7 +62,7 @@ pub trait DBDocument:
6162
mut self,
6263
overwrite: bool,
6364
collection: &Self::Collection,
64-
) -> Result<Self, Box<dyn Error>> {
65+
) -> Result<Self, anyhow::Error> {
6566
let db_collection = collection.db_collection().await?;
6667

6768
loop {
@@ -95,7 +96,7 @@ pub trait DBDocument:
9596
mut self,
9697
overwrite: bool,
9798
collection: &Self::Collection,
98-
) -> Result<Self::Key, Box<dyn Error>> {
99+
) -> Result<Self::Key, anyhow::Error> {
99100
let db_collection = collection.db_collection().await?;
100101

101102
loop {
@@ -128,7 +129,7 @@ pub trait DBDocument:
128129
&self,
129130
merge_objects: bool,
130131
collection: &Self::Collection,
131-
) -> Result<Self, Box<dyn Error>> {
132+
) -> Result<Self, anyhow::Error> {
132133
let db_collection = collection.db_collection().await?;
133134

134135
let ignore_rev = self.db_rev().is_none();
@@ -175,7 +176,7 @@ pub trait DBDocument:
175176
&self,
176177
merge_objects: bool,
177178
collection: &Self::Collection,
178-
) -> Result<(), Box<dyn Error>> {
179+
) -> Result<(), anyhow::Error> {
179180
let db_collection = collection.db_collection().await?;
180181

181182
let ignore_rev = self.db_rev().is_none();
@@ -221,7 +222,7 @@ pub trait DBDocument:
221222
mut self,
222223
merge_objects: bool,
223224
collection: &Self::Collection,
224-
) -> Result<Self, Box<dyn Error>> {
225+
) -> Result<Self, anyhow::Error> {
225226
let db_collection = collection.db_collection().await?;
226227

227228
loop {
@@ -256,7 +257,7 @@ pub trait DBDocument:
256257
mut self,
257258
merge_objects: bool,
258259
collection: &Self::Collection,
259-
) -> Result<Self::Key, Box<dyn Error>> {
260+
) -> Result<Self::Key, anyhow::Error> {
260261
let db_collection = collection.db_collection().await?;
261262

262263
loop {
@@ -289,7 +290,7 @@ pub trait DBDocument:
289290
&self,
290291
rev: Option<ArcStr>,
291292
collection: &Self::Collection,
292-
) -> Result<Self, Box<dyn Error>> {
293+
) -> Result<Self, anyhow::Error> {
293294
let db_collection = collection.db_collection().await?;
294295

295296
let key = self
@@ -333,7 +334,7 @@ pub trait DBDocument:
333334
&self,
334335
rev: Option<ArcStr>,
335336
collection: &Self::Collection,
336-
) -> Result<(), Box<dyn Error>> {
337+
) -> Result<(), anyhow::Error> {
337338
let db_collection = collection.db_collection().await?;
338339

339340
let key = self

arangodb-types/src/types/database_information.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::borrow::Cow;
22
use std::collections::HashMap;
3-
use std::error::Error;
43

54
use arangors::{ClientError, Connection, GenericConnection};
65
use serde::Deserialize;
@@ -29,7 +28,7 @@ impl DBInfo {
2928
database: Cow<'static, str>,
3029
username: Cow<'static, str>,
3130
password: Cow<'static, str>,
32-
) -> Result<DBInfo, Box<dyn Error>> {
31+
) -> Result<DBInfo, anyhow::Error> {
3332
let connection = Connection::establish_jwt(&url, &username, &password).await?;
3433

3534
let database = match connection.create_database(&database).await {
@@ -79,7 +78,7 @@ impl DBInfo {
7978
name: &str,
8079
code: &str,
8180
is_deterministic: bool,
82-
) -> Result<(), Box<dyn Error>> {
81+
) -> Result<(), anyhow::Error> {
8382
let client = self.connection.session();
8483
let response = client
8584
.client
@@ -100,12 +99,12 @@ impl DBInfo {
10099
.text()
101100
.await
102101
.unwrap_or_else(|_| "<undefined>".to_string());
103-
Err(text.into())
102+
Err(anyhow::anyhow!(text))
104103
}
105104
}
106105
}
107106

108-
pub async fn remove_all_aql_function(&self, namespace: &str) -> Result<(), Box<dyn Error>> {
107+
pub async fn remove_all_aql_function(&self, namespace: &str) -> Result<(), anyhow::Error> {
109108
let client = self.connection.session();
110109
let response = client
111110
.client
@@ -125,7 +124,7 @@ impl DBInfo {
125124
.text()
126125
.await
127126
.unwrap_or_else(|_| "<undefined>".to_string());
128-
Err(text.into())
127+
Err(anyhow::anyhow!(text))
129128
}
130129
}
131130
}

arangodb-types/src/types/filters/api/expressions.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::error::Error;
2-
31
use serde::{Deserialize, Serialize};
42

53
use crate::aql::AqlBuilder;
@@ -39,11 +37,7 @@ impl<T: PaginatedDocumentField> APIFilterExpression<T> {
3937
self.right.calculate_stats(stats);
4038
}
4139

42-
pub fn build_aql(
43-
&self,
44-
query: &mut String,
45-
aql: &mut AqlBuilder,
46-
) -> Result<(), Box<dyn Error>> {
40+
pub fn build_aql(&self, query: &mut String, aql: &mut AqlBuilder) -> Result<(), anyhow::Error> {
4741
self.left.build_aql(query, aql)?;
4842
query.push(' ');
4943
self.operator.build_aql(query);

0 commit comments

Comments
 (0)