Skip to content

Commit 756be55

Browse files
committed
refactor: change anyhow error to custom error variants
1 parent 98676f7 commit 756be55

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

src/static_schema.rs

+37-18
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

@@ -27,6 +26,7 @@ use std::{
2726
collections::{HashMap, HashSet},
2827
sync::Arc,
2928
};
29+
3030
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
3131
pub struct StaticSchema {
3232
fields: Vec<SchemaFields>,
@@ -57,13 +57,12 @@ pub struct Fields {
5757
}
5858

5959
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
60-
6160
pub struct Metadata {}
6261
pub fn convert_static_schema_to_arrow_schema(
6362
static_schema: StaticSchema,
6463
time_partition: &str,
6564
custom_partition: Option<&String>,
66-
) -> Result<Arc<Schema>, AnyError> {
65+
) -> Result<Arc<Schema>, StaticSchemaError> {
6766
let mut parsed_schema = ParsedSchema {
6867
fields: Vec::new(),
6968
metadata: HashMap::new(),
@@ -86,7 +85,9 @@ pub fn convert_static_schema_to_arrow_schema(
8685

8786
for partition in &custom_partition_list {
8887
if !custom_partition_exists.contains_key(*partition) {
89-
return Err(anyhow!("custom partition field {partition} does not exist in the schema for the static schema logstream"));
88+
return Err(StaticSchemaError::MissingCustomPartition(
89+
partition.to_string(),
90+
));
9091
}
9192
}
9293
}
@@ -134,29 +135,26 @@ pub fn convert_static_schema_to_arrow_schema(
134135
parsed_schema.fields.push(parsed_field);
135136
}
136137
if !time_partition.is_empty() && !time_partition_exists {
137-
return Err(anyhow! {
138-
format!(
139-
"time partition field {time_partition} does not exist in the schema for the static schema logstream"
140-
),
141-
});
138+
return Err(StaticSchemaError::MissingTimePartition(
139+
time_partition.to_string(),
140+
));
142141
}
143142
add_parseable_fields_to_static_schema(parsed_schema)
144143
}
145144

146145
fn add_parseable_fields_to_static_schema(
147146
parsed_schema: ParsedSchema,
148-
) -> Result<Arc<Schema>, AnyError> {
147+
) -> Result<Arc<Schema>, StaticSchemaError> {
149148
let mut schema: Vec<Arc<Field>> = Vec::new();
150149
for field in parsed_schema.fields.iter() {
151150
let field = Field::new(field.name.clone(), field.data_type.clone(), field.nullable);
152151
schema.push(Arc::new(field));
153152
}
154153

155154
if get_field(&schema, DEFAULT_TIMESTAMP_KEY).is_some() {
156-
return Err(anyhow!(
157-
"field {} is a reserved field",
158-
DEFAULT_TIMESTAMP_KEY
159-
));
155+
return Err(StaticSchemaError::ReservedKey(
156+
DEFAULT_TIMESTAMP_KEY
157+
));
160158
};
161159

162160
// add the p_timestamp field to the event schema to the 0th index
@@ -187,22 +185,43 @@ fn default_dict_is_ordered() -> bool {
187185
fn validate_field_names(
188186
field_name: &str,
189187
existing_fields: &mut HashSet<String>,
190-
) -> Result<(), AnyError> {
188+
) -> Result<(), StaticSchemaError> {
191189
if field_name.is_empty() {
192-
return Err(anyhow!("field names should not be empty"));
190+
return Err(StaticSchemaError::EmptyFieldName);
193191
}
194192

195193
if !existing_fields.insert(field_name.to_string()) {
196-
return Err(anyhow!("duplicate field name: {}", field_name));
194+
return Err(StaticSchemaError::DuplicateField(field_name.to_string()));
197195
}
198196

199197
Ok(())
200198
}
201199

200+
#[derive(Debug, thiserror::Error)]
201+
pub enum StaticSchemaError {
202+
#[error(
203+
"custom partition field {0} does not exist in the schema for the static schema logstream"
204+
)]
205+
MissingCustomPartition(String),
206+
207+
#[error(
208+
"time partition field {0} does not exist in the schema for the static schema logstream"
209+
)]
210+
MissingTimePartition(String),
211+
212+
#[error("field {DEFAULT_TIMESTAMP_KEY:?} is a reserved field")]
213+
ReservedKey(&'static str),
214+
215+
#[error("field name cannot be empty")]
216+
EmptyFieldName,
217+
218+
#[error("duplicate field name: {0}")]
219+
DuplicateField(String),
220+
}
221+
202222
#[cfg(test)]
203223
mod tests {
204224
use super::*;
205-
use std::collections::HashSet;
206225
#[test]
207226
fn empty_field_names() {
208227
let mut existing_field_names: HashSet<String> = HashSet::new();

0 commit comments

Comments
 (0)