Skip to content

Commit 228428e

Browse files
refactor
1 parent a843a5f commit 228428e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ mod ui {
151151
.expect("has segemnts")
152152
.find(|v| v.starts_with('v'))
153153
.expect("version segement");
154-
println!("cargo:rustc-env=UI_VERSION={}", ui_version);
154+
println!("cargo:rustc-env=UI_VERSION={ui_version}");
155155
}
156156

157157
Ok(())

src/handlers/http/logstream.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,15 @@ pub async fn detect_schema(Json(json): Json<Value>) -> Result<impl Responder, St
109109
if !has_more_than_max_allowed_levels(&json, 1) {
110110
//perform generic flattening, return error if failed to flatten
111111
let mut flattened_json = match generic_flattening(&json) {
112-
Ok(flattened) => convert_to_array(flattened).unwrap(),
112+
Ok(flattened) => match convert_to_array(flattened) {
113+
Ok(array) => array,
114+
Err(e) => {
115+
return Err(StreamError::Custom {
116+
msg: format!("Failed to convert to array: {}", e),
117+
status: StatusCode::BAD_REQUEST,
118+
})
119+
}
120+
},
113121
Err(e) => {
114122
return Err(StreamError::Custom {
115123
msg: e.to_string(),
@@ -128,16 +136,26 @@ pub async fn detect_schema(Json(json): Json<Value>) -> Result<impl Responder, St
128136
value @ Value::Object(_) => vec![value],
129137
_ => unreachable!("flatten would have failed beforehand"),
130138
};
131-
let mut schema =
132-
Arc::new(infer_json_schema_from_iterator(flattened_json_arr.iter().map(Ok)).unwrap());
139+
let mut schema = match infer_json_schema_from_iterator(flattened_json_arr.iter().map(Ok)) {
140+
Ok(schema) => Arc::new(schema),
141+
Err(e) => {
142+
return Err(StreamError::Custom {
143+
msg: format!("Failed to infer schema: {}", e),
144+
status: StatusCode::BAD_REQUEST,
145+
})
146+
}
147+
};
133148
for flattened_json in flattened_json_arr {
134149
schema = override_data_type(schema, flattened_json, SchemaVersion::V1);
135150
}
136151
Ok((web::Json(schema), StatusCode::OK))
137152
} else {
138153
// error out if the JSON is heavily nested
139154
Err(StreamError::Custom {
140-
msg: "heavily nested, cannot flatten this JSON".to_string(),
155+
msg: format!(
156+
"JSON is too deeply nested (exceeds level {}), cannot flatten",
157+
PARSEABLE.options.event_flatten_level
158+
),
141159
status: StatusCode::BAD_REQUEST,
142160
})
143161
}

0 commit comments

Comments
 (0)