Skip to content

Commit dc09121

Browse files
author
Devdutt Shenoi
committed
refactor: custom error type
1 parent 1561742 commit dc09121

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/parseable/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ impl Parseable {
498498
let static_schema: StaticSchema = serde_json::from_slice(body)?;
499499
static_schema
500500
.convert_to_arrow_schema(&time_partition, custom_partition.as_ref())
501-
.map_err(|_| CreateStreamError::Custom {
501+
.map_err(|err| CreateStreamError::Custom {
502502
msg: format!(
503-
"Unable to commit static schema, logstream {stream_name} not created"
503+
"Unable to commit static schema, logstream {stream_name} not created; Error: {err}"
504504
),
505505
status: StatusCode::BAD_REQUEST,
506506
})?

src/static_schema.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,47 @@
1616
*
1717
*/
1818

19-
use crate::event::DEFAULT_TIMESTAMP_KEY;
20-
use crate::utils::arrow::get_field;
21-
use anyhow::{anyhow, Error as AnyError};
22-
use serde::{Deserialize, Serialize};
23-
use std::str;
19+
use std::{collections::HashMap, sync::Arc};
2420

2521
use arrow_schema::{DataType, Field, Schema, TimeUnit};
26-
use std::{collections::HashMap, sync::Arc};
22+
use serde::{Deserialize, Serialize};
23+
24+
use crate::{event::DEFAULT_TIMESTAMP_KEY, utils::arrow::get_field};
25+
2726
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2827
pub struct StaticSchema {
2928
fields: Vec<SchemaFields>,
3029
}
3130

31+
#[derive(Debug, thiserror::Error)]
32+
pub enum StaticSchemaError {
33+
#[error(
34+
"custom partition field {0} does not exist in the schema for the static schema logstream"
35+
)]
36+
MissingCustomPartition(String),
37+
#[error(
38+
"time partition field {0} does not exist in the schema for the static schema logstream"
39+
)]
40+
MissingTimePartition(String),
41+
#[error("field {DEFAULT_TIMESTAMP_KEY} is a reserved field")]
42+
MissingDefaultTimePartition,
43+
}
44+
3245
impl StaticSchema {
3346
pub fn convert_to_arrow_schema(
3447
self,
3548
time_partition: &str,
3649
custom_partition: Option<&String>,
37-
) -> Result<Arc<Schema>, AnyError> {
50+
) -> Result<Arc<Schema>, StaticSchemaError> {
3851
let mut fields = Vec::new();
3952
let mut time_partition_exists = false;
4053

4154
if let Some(custom_partition) = custom_partition {
4255
for partition in custom_partition.split(',') {
4356
if !self.fields.iter().any(|field| field.name == partition) {
44-
return Err(anyhow!("custom partition field {partition} does not exist in the schema for the static schema logstream"));
57+
return Err(StaticSchemaError::MissingCustomPartition(
58+
partition.to_owned(),
59+
));
4560
}
4661
}
4762
}
@@ -86,7 +101,9 @@ impl StaticSchema {
86101
}
87102

88103
if !time_partition.is_empty() && !time_partition_exists {
89-
return Err(anyhow!("time partition field {time_partition} does not exist in the schema for the static schema logstream"));
104+
return Err(StaticSchemaError::MissingTimePartition(
105+
time_partition.to_owned(),
106+
));
90107
}
91108

92109
let mut schema: Vec<Arc<Field>> = Vec::new();
@@ -96,10 +113,7 @@ impl StaticSchema {
96113
}
97114

98115
if get_field(&schema, DEFAULT_TIMESTAMP_KEY).is_some() {
99-
return Err(anyhow!(
100-
"field {} is a reserved field",
101-
DEFAULT_TIMESTAMP_KEY
102-
));
116+
return Err(StaticSchemaError::MissingDefaultTimePartition);
103117
};
104118

105119
// add the p_timestamp field to the event schema to the 0th index

0 commit comments

Comments
 (0)