Skip to content

Commit 40bff2b

Browse files
committed
fix(errors): enhance some error handling messages
Signed-off-by: Daniel Boll <danielboll.academico@gmail.com>
1 parent ef2c8d8 commit 40bff2b

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

index.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,72 @@ switch (platform) {
224224
}
225225
break
226226
case 'arm':
227+
if (isMusl()) {
228+
localFileExisted = existsSync(
229+
join(__dirname, 'scylladb.linux-arm-musleabihf.node')
230+
)
231+
try {
232+
if (localFileExisted) {
233+
nativeBinding = require('./scylladb.linux-arm-musleabihf.node')
234+
} else {
235+
nativeBinding = require('@lambda-group/scylladb-linux-arm-musleabihf')
236+
}
237+
} catch (e) {
238+
loadError = e
239+
}
240+
} else {
241+
localFileExisted = existsSync(
242+
join(__dirname, 'scylladb.linux-arm-gnueabihf.node')
243+
)
244+
try {
245+
if (localFileExisted) {
246+
nativeBinding = require('./scylladb.linux-arm-gnueabihf.node')
247+
} else {
248+
nativeBinding = require('@lambda-group/scylladb-linux-arm-gnueabihf')
249+
}
250+
} catch (e) {
251+
loadError = e
252+
}
253+
}
254+
break
255+
case 'riscv64':
256+
if (isMusl()) {
257+
localFileExisted = existsSync(
258+
join(__dirname, 'scylladb.linux-riscv64-musl.node')
259+
)
260+
try {
261+
if (localFileExisted) {
262+
nativeBinding = require('./scylladb.linux-riscv64-musl.node')
263+
} else {
264+
nativeBinding = require('@lambda-group/scylladb-linux-riscv64-musl')
265+
}
266+
} catch (e) {
267+
loadError = e
268+
}
269+
} else {
270+
localFileExisted = existsSync(
271+
join(__dirname, 'scylladb.linux-riscv64-gnu.node')
272+
)
273+
try {
274+
if (localFileExisted) {
275+
nativeBinding = require('./scylladb.linux-riscv64-gnu.node')
276+
} else {
277+
nativeBinding = require('@lambda-group/scylladb-linux-riscv64-gnu')
278+
}
279+
} catch (e) {
280+
loadError = e
281+
}
282+
}
283+
break
284+
case 's390x':
227285
localFileExisted = existsSync(
228-
join(__dirname, 'scylladb.linux-arm-gnueabihf.node')
286+
join(__dirname, 'scylladb.linux-s390x-gnu.node')
229287
)
230288
try {
231289
if (localFileExisted) {
232-
nativeBinding = require('./scylladb.linux-arm-gnueabihf.node')
290+
nativeBinding = require('./scylladb.linux-s390x-gnu.node')
233291
} else {
234-
nativeBinding = require('@lambda-group/scylladb-linux-arm-gnueabihf')
292+
nativeBinding = require('@lambda-group/scylladb-linux-s390x-gnu')
235293
}
236294
} catch (e) {
237295
loadError = e

src/cluster/scylla_cluster.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl ScyllaCluster {
111111
(Some(Either::A(_)), None) => Ok(None),
112112
};
113113

114-
if let Some(keyspace) = keyspace? {
114+
if let Some(keyspace) = keyspace.clone()? {
115115
builder = builder.use_keyspace(keyspace, false);
116116
}
117117

@@ -159,6 +159,18 @@ impl ScyllaCluster {
159159
builder = builder.compression(compression.into());
160160
}
161161

162-
Ok(ScyllaSession::new(builder.build().await.unwrap()))
162+
let session = builder.build().await;
163+
164+
match session {
165+
Ok(session) => Ok(ScyllaSession::new(session)),
166+
Err(err) => Err(napi::Error::from_reason(format!(
167+
"Failed to connect to the database: {} - [{uri}] - Keyspace: {keyspace}",
168+
err,
169+
uri = self.uri,
170+
keyspace = keyspace
171+
.unwrap_or(Some("No keyspace provided".to_string()))
172+
.unwrap_or("No keyspace provided".to_string())
173+
))),
174+
}
163175
}
164176
}

src/query/scylla_query.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::Display;
2+
13
use scylla::query::Query;
24
use scylla::statement::Consistency;
35

@@ -6,6 +8,12 @@ pub struct ScyllaQuery {
68
pub(crate) query: Query,
79
}
810

11+
impl Display for ScyllaQuery {
12+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13+
write!(f, "ScyllaQuery: {}", self.query.contents)
14+
}
15+
}
16+
917
#[napi]
1018
impl ScyllaQuery {
1119
#[napi(constructor)]

src/session/scylla_session.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ impl ScyllaSession {
2929
query: Either<String, &ScyllaPreparedStatement>,
3030
parameters: Option<Vec<Either3<u32, String, &Uuid>>>,
3131
) -> napi::Result<serde_json::Value> {
32-
let values = QueryParameter::parser(parameters).unwrap();
32+
let values = QueryParameter::parser(parameters.clone()).ok_or(napi::Error::new(
33+
napi::Status::InvalidArg,
34+
format!("Something went wrong with your query parameters. {parameters:?}"),
35+
))?;
3336

34-
let query_result = match query {
37+
let query_result = match query.clone() {
3538
Either::A(query) => self.session.query(query, values).await,
3639
Either::B(prepared) => self.session.execute(&prepared.prepared, values).await,
3740
}
3841
.map_err(|_| {
42+
let query = match query {
43+
Either::A(query) => query,
44+
Either::B(prepared) => prepared.prepared.get_statement().to_string(),
45+
};
46+
3947
napi::Error::new(
4048
napi::Status::InvalidArg,
41-
"Something went wrong with your query.",
49+
format!("Something went wrong with your query. - [{query}] - {parameters:?}"),
4250
)
4351
})?;
4452

@@ -51,7 +59,10 @@ impl ScyllaSession {
5159
scylla_query: &ScyllaQuery,
5260
parameters: Option<Vec<Either3<u32, String, &Uuid>>>,
5361
) -> napi::Result<serde_json::Value> {
54-
let values = QueryParameter::parser(parameters).unwrap();
62+
let values = QueryParameter::parser(parameters.clone()).ok_or(napi::Error::new(
63+
napi::Status::InvalidArg,
64+
format!("Something went wrong with your query parameters. {parameters:?}"),
65+
))?;
5566

5667
let query_result = self
5768
.session
@@ -60,7 +71,7 @@ impl ScyllaSession {
6071
.map_err(|_| {
6172
napi::Error::new(
6273
napi::Status::InvalidArg,
63-
"Something went wrong with your query.",
74+
format!("Something went wrong with your query. - [{scylla_query}] - {parameters:?}"),
6475
)
6576
})?;
6677

@@ -69,10 +80,10 @@ impl ScyllaSession {
6980

7081
#[napi]
7182
pub async fn prepare(&self, query: String) -> napi::Result<ScyllaPreparedStatement> {
72-
let prepared = self.session.prepare(query).await.map_err(|_| {
83+
let prepared = self.session.prepare(query.clone()).await.map_err(|_| {
7384
napi::Error::new(
7485
napi::Status::InvalidArg,
75-
"Something went wrong with your prepared statement.",
86+
format!("Something went wrong with your prepared statement. - [{query}]"),
7687
)
7788
})?;
7889

src/types/uuid.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use napi::Result;
22

33
#[napi()]
4+
#[derive(Debug)]
45
pub struct Uuid {
56
pub(crate) uuid: uuid::Uuid,
67
}

0 commit comments

Comments
 (0)