Skip to content

Commit a3fb21d

Browse files
committed
refactor: change anyhow error to custom error variants
1 parent 6d47322 commit a3fb21d

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/static_schema.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
use crate::event::DEFAULT_TIMESTAMP_KEY;
2020
use crate::utils::arrow::get_field;
21-
use anyhow::{anyhow, Error as AnyError};
2221
use serde::{Deserialize, Serialize};
2322
use std::str;
2423

@@ -60,7 +59,7 @@ pub fn convert_static_schema_to_arrow_schema(
6059
static_schema: StaticSchema,
6160
time_partition: &str,
6261
custom_partition: Option<&String>,
63-
) -> Result<Arc<Schema>, AnyError> {
62+
) -> Result<Arc<Schema>, StaticSchemaError> {
6463
let mut parsed_schema = ParsedSchema {
6564
fields: Vec::new(),
6665
metadata: HashMap::new(),
@@ -83,7 +82,7 @@ pub fn convert_static_schema_to_arrow_schema(
8382

8483
for partition in &custom_partition_list {
8584
if !custom_partition_exists.contains_key(*partition) {
86-
return Err(anyhow!("custom partition field {partition} does not exist in the schema for the static schema logstream"));
85+
return Err(StaticSchemaError::MissingCustomPartition(partition.to_string()));
8786
}
8887
}
8988
}
@@ -132,18 +131,14 @@ pub fn convert_static_schema_to_arrow_schema(
132131
parsed_schema.fields.push(parsed_field);
133132
}
134133
if !time_partition.is_empty() && !time_partition_exists {
135-
return Err(anyhow! {
136-
format!(
137-
"time partition field {time_partition} does not exist in the schema for the static schema logstream"
138-
),
139-
});
134+
return Err(StaticSchemaError::MissingTimePartition(time_partition.to_string()));
140135
}
141136
add_parseable_fields_to_static_schema(parsed_schema)
142137
}
143138

144139
fn add_parseable_fields_to_static_schema(
145140
parsed_schema: ParsedSchema,
146-
) -> Result<Arc<Schema>, AnyError> {
141+
) -> Result<Arc<Schema>, StaticSchemaError> {
147142

148143
let mut schema: Vec<Arc<Field>> = Vec::new();
149144
for field in parsed_schema.fields.iter() {
@@ -152,10 +147,7 @@ fn add_parseable_fields_to_static_schema(
152147
}
153148

154149
if get_field(&schema, DEFAULT_TIMESTAMP_KEY).is_some() {
155-
return Err(anyhow!(
156-
"field {} is a reserved field",
157-
DEFAULT_TIMESTAMP_KEY
158-
));
150+
return Err(StaticSchemaError::DefaultTime);
159151
};
160152

161153
// add the p_timestamp field to the event schema to the 0th index
@@ -183,19 +175,43 @@ fn default_dict_is_ordered() -> bool {
183175
false
184176
}
185177

186-
fn validate_field_names(field_name: &str, existing_fields: &mut HashSet<String>) -> Result<(), AnyError> {
178+
fn validate_field_names(field_name: &str, existing_fields: &mut HashSet<String>) -> Result<(), StaticSchemaError> {
187179

188180
if field_name.is_empty() {
189-
return Err(anyhow!("field names should not be empty"));
181+
return Err(StaticSchemaError::EmptyFieldName);
190182
}
191183

192184
if !existing_fields.insert(field_name.to_string()) {
193-
return Err(anyhow!("duplicate field name: {}", field_name));
185+
return Err(StaticSchemaError::DuplicateField(field_name.to_string()));
194186
}
195187

196188
Ok(())
197189
}
198190

191+
192+
#[derive(Debug, thiserror::Error)]
193+
pub enum StaticSchemaError{
194+
195+
#[error(
196+
"custom partition field {0} does not exist in the schema for the static schema logstream"
197+
)]
198+
MissingCustomPartition(String),
199+
200+
#[error(
201+
"time partition field {0} does not exist in the schema for the static schema logstream"
202+
)]
203+
MissingTimePartition(String),
204+
205+
#[error("field {DEFAULT_TIMESTAMP_KEY:?} is a reserved field")]
206+
DefaultTime,
207+
208+
#[error("field name cannot be empty")]
209+
EmptyFieldName,
210+
211+
#[error("duplicate field name: {0}")]
212+
DuplicateField(String),
213+
}
214+
199215
#[cfg(test)]
200216
mod tests {
201217
use super::*;

0 commit comments

Comments
 (0)