-
Notifications
You must be signed in to change notification settings - Fork 8
Improve error handling & return meaningful error messages #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
897e1a1
9dfb831
b2a6227
d8a3639
10ac5e6
a84bc62
74f9e2f
5e9ef85
a89e649
cbfce2f
854d013
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,7 +132,11 @@ pub async fn get_sql_query_param( | |
| } else if single_table_name.is_some() { | ||
| table_name = single_table_name.map(|x| x.to_string()); | ||
| } else { | ||
| panic!("failed to find an appropriate table name while processing WHERE statement") | ||
| error!( | ||
| "Failed to infer table name while processing WHERE clause. Expression: {}", | ||
| left | ||
| ); | ||
| return None; | ||
|
JasonShin marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| let column_name = translate_column_name_expr(left); | ||
|
|
@@ -144,17 +148,30 @@ pub async fn get_sql_query_param( | |
| match (column_name, expr_placeholder, table_name) { | ||
| (Some(column_name), Some(expr_placeholder), Some(table_name)) => { | ||
| let table_names = vec![table_name.as_str()]; | ||
| let columns = DB_SCHEMA | ||
| .lock() | ||
| .await | ||
| .fetch_table(&table_names, db_conn) | ||
| .await | ||
| .unwrap_or_else(|| panic!("Failed to fetch columns for table {table_name}")); | ||
| let columns = DB_SCHEMA.lock().await.fetch_table(&table_names, db_conn).await; | ||
|
|
||
| if columns.is_none() { | ||
| error!( | ||
| "Table '{}' not found in database schema. Check that the table exists and is accessible.", | ||
| table_name | ||
| ); | ||
| return None; | ||
|
||
| } | ||
|
|
||
| let columns = columns.unwrap(); | ||
|
|
||
| // get column and return TsFieldType | ||
| let column = columns | ||
| .get(column_name.as_str()) | ||
| .unwrap_or_else(|| panic!("Failed to find the column from the table schema of {table_name}")); | ||
| let column = columns.get(column_name.as_str()); | ||
| if column.is_none() { | ||
| let available_columns = columns.keys().map(|k| k.as_str()).collect::<Vec<_>>().join(", "); | ||
| error!( | ||
| "Column '{}' not found in table '{}'. Available columns: {}", | ||
| column_name, table_name, available_columns | ||
| ); | ||
| return None; | ||
|
JasonShin marked this conversation as resolved.
Outdated
JasonShin marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| let column = column.unwrap(); | ||
| Some((column.field_type.to_owned(), column.is_nullable, Some(expr_placeholder))) | ||
| } | ||
| _ => None, | ||
|
|
@@ -731,16 +748,35 @@ pub async fn translate_assignment( | |
| let value = get_expr_placeholder(&assignment.value); | ||
|
|
||
| if value.is_some() { | ||
| let table_details = &DB_SCHEMA | ||
| let table_details = DB_SCHEMA | ||
|
JasonShin marked this conversation as resolved.
|
||
| .lock() | ||
| .await | ||
| .fetch_table(&vec![table_name], db_conn) | ||
| .await | ||
| .unwrap(); | ||
| let column_name = translate_column_name_assignment(assignment).unwrap(); | ||
| let field = table_details | ||
| .get(&column_name) | ||
| .unwrap_or_else(|| panic!("Failed to find the column detail for {column_name}")); | ||
| .ok_or_else(|| TsGeneratorError::TableNotFoundInSchema { | ||
| table: table_name.to_string(), | ||
| })?; | ||
|
|
||
| let column_name = translate_column_name_assignment(assignment).ok_or_else(|| { | ||
| TsGeneratorError::InsertStatementProcessingFailed { | ||
| reason: "Failed to extract column name from assignment".to_string(), | ||
| query: format!("UPDATE {} SET {} = ...", table_name, assignment), | ||
| } | ||
| })?; | ||
|
Comment on lines
+757
to
+762
|
||
|
|
||
| let field = table_details.get(&column_name).ok_or_else(|| { | ||
| let available_columns = table_details | ||
| .keys() | ||
| .map(|k| k.as_str()) | ||
| .collect::<Vec<_>>() | ||
| .join(", "); | ||
| TsGeneratorError::ColumnNotFoundInTable { | ||
| column: column_name.clone(), | ||
| table: table_name.to_string(), | ||
| available_columns, | ||
| } | ||
| })?; | ||
|
|
||
| let _ = ts_query.insert_param(&field.field_type, &field.is_nullable, &value); | ||
| } | ||
| Ok(()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two different error variants are using the same error code [E018]. The
TableNotFoundInSchemaerror on line 47 andUpdateStatementProcessingFailederror on line 45 both use [E018]. One should be changed to [E020] or another unique code to avoid confusion.