Skip to content

Commit

Permalink
make default_err_msg & prompt constant
Browse files Browse the repository at this point in the history
  • Loading branch information
xzhseh committed Feb 13, 2024
1 parent f07dd5b commit 876f810
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/frontend/src/handler/create_sql_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ use crate::catalog::CatalogError;
use crate::expr::{Expr, ExprImpl, Literal};
use crate::{bind_data_type, Binder};

const DEFAULT_ERR_MSG: &str = "Failed to conduct semantic check";

const PROMPT: &str = "In SQL UDF definition: ";

/// Create a mock `udf_context`, which is used for semantic check
fn create_mock_udf_context(
arg_types: Vec<DataType>,
Expand Down Expand Up @@ -223,16 +227,14 @@ pub async fn handle_create_sql_function(
}
}
Err(e) => {
let default_err_msg = "Failed to conduct semantic check".to_string();
let prompt = "In SQL UDF definition: ".to_string();

if let ErrorCode::BindErrorRoot { expr: _, error } = e.inner() {
let invalid_msg = error.to_string();
let pattern = "[sql udf]";

// Valid error message for hint display
let Some(_) = invalid_msg.as_str().find(pattern) else {
return Err(ErrorCode::InvalidInputSyntax(default_err_msg).into());
return Err(ErrorCode::InvalidInputSyntax(DEFAULT_ERR_MSG.into()).into());
};

// Get the name of the invalid item
Expand All @@ -241,28 +243,26 @@ pub async fn handle_create_sql_function(

// Find the invalid parameter / column
let Some(idx) = find_target(body.as_str(), invalid_item_name) else {
return Err(ErrorCode::InvalidInputSyntax(default_err_msg).into());
return Err(ErrorCode::InvalidInputSyntax(DEFAULT_ERR_MSG.into()).into());
};

println!("Current idx: {}", idx);

// The exact error position for `^` to point to
let position = format!("{}{}",
" ".repeat(idx + prompt.len() + 1),
" ".repeat(idx + PROMPT.len() + 1),
"^".repeat(invalid_item_name.len()));

return Err(ErrorCode::InvalidInputSyntax(
format!("\n{}: {}\n{}`{}`\n{}",
default_err_msg,
DEFAULT_ERR_MSG,
invalid_msg,
prompt,
PROMPT,
body,
position)
).into());
}

// Otherwise return the default error message
return Err(ErrorCode::InvalidInputSyntax(default_err_msg).into())
return Err(ErrorCode::InvalidInputSyntax(DEFAULT_ERR_MSG.into()).into())
}
}
} else {
Expand Down

0 comments on commit 876f810

Please sign in to comment.