Skip to content

Commit

Permalink
apply TRY401
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenliang123 committed Nov 11, 2024
1 parent 2e0da36 commit e92d323
Show file tree
Hide file tree
Showing 45 changed files with 80 additions and 70 deletions.
4 changes: 2 additions & 2 deletions api/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def upgrade_db():
click.echo(click.style("Database migration successful!", fg="green"))

except Exception as e:
logging.exception(f"Database migration failed: {e}")
logging.exception("Failed to execute database migration")
finally:
lock.release()
else:
Expand Down Expand Up @@ -633,7 +633,7 @@ def fix_app_site_missing():
except Exception as e:
failed_app_ids.append(app_id)
click.echo(click.style("Failed to fix missing site for app {}".format(app_id), fg="red"))
logging.exception(f"Fix app related site missing issue failed, error: {e}")
logging.exception(f"Failed to fix app related site missing issue, app_id: {app_id}")
continue

if not processed_count:
Expand Down
6 changes: 3 additions & 3 deletions api/controllers/console/app/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def post(self, app_model):
except ValueError as e:
raise e
except Exception as e:
logging.exception(f"internal server error, {str(e)}.")
logging.exception("Failed to handle post request to ChatMessageAudioApi")
raise InternalServerError()


Expand Down Expand Up @@ -128,7 +128,7 @@ def post(self, app_model):
except ValueError as e:
raise e
except Exception as e:
logging.exception(f"internal server error, {str(e)}.")
logging.exception("Failed to handle post request to ChatMessageTextApi")
raise InternalServerError()


Expand Down Expand Up @@ -170,7 +170,7 @@ def get(self, app_model):
except ValueError as e:
raise e
except Exception as e:
logging.exception(f"internal server error, {str(e)}.")
logging.exception("Failed to handle get request to TextModesApi")
raise InternalServerError()


Expand Down
2 changes: 1 addition & 1 deletion api/controllers/console/datasets/datasets_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def post(self, dataset_id):
raise DocumentAlreadyFinishedError()
retry_documents.append(document)
except Exception as e:
logging.exception(f"Document {document_id} retry failed: {str(e)}")
logging.exception(f"Failed to retry document, document id: {document_id}")
continue
# retry document
DocumentService.retry_document(dataset_id, retry_documents)
Expand Down
10 changes: 8 additions & 2 deletions api/controllers/console/workspace/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def post(self):
model=model_setting["model"],
)
except Exception as ex:
logging.exception(f"{model_setting['model_type']} save error: {ex}")
logging.exception(
f"Failed to update default model, model type: {model_setting['model_type']},"
f" model:{model_setting.get('model')}"
)
raise ex

return {"result": "success"}
Expand Down Expand Up @@ -156,7 +159,10 @@ def post(self, provider: str):
credentials=args["credentials"],
)
except CredentialsValidateFailedError as ex:
logging.exception(f"save model credentials error: {ex}")
logging.exception(
f"Failed to save model credentials, tenant_id: {tenant_id},"
f" model: {args.get('model')}, model_type: {args.get('model_type')}"
)
raise ValueError(str(ex))

return {"result": "success"}, 200
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/web/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def post(self, app_model: App, end_user):
except ValueError as e:
raise e
except Exception as e:
logging.exception(f"internal server error: {str(e)}")
logging.exception("Failed to handle post request to AudioApi")
raise InternalServerError()


Expand Down Expand Up @@ -117,7 +117,7 @@ def post(self, app_model: App, end_user):
except ValueError as e:
raise e
except Exception as e:
logging.exception(f"internal server error: {str(e)}")
logging.exception("Failed to handle post request to TextApi")
raise InternalServerError()


Expand Down
2 changes: 1 addition & 1 deletion api/core/app/apps/advanced_chat/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,5 @@ def _handle_advanced_chat_response(
if e.args[0] == "I/O operation on closed file.": # ignore this error
raise GenerateTaskStoppedError()
else:
logger.exception(e)
logger.exception(f"Failed to process generate task pipeline, conversation_id: {conversation.id}")
raise e
2 changes: 1 addition & 1 deletion api/core/app/apps/advanced_chat/generate_task_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def _wrapper_process_stream_response(
start_listener_time = time.time()
yield MessageAudioStreamResponse(audio=audio_trunk.audio, task_id=task_id)
except Exception as e:
logger.exception(e)
logger.exception(f"Failed to listen audio message, task_id: {task_id}")
break
if tts_publisher:
yield MessageAudioEndStreamResponse(audio="", task_id=task_id)
Expand Down
2 changes: 1 addition & 1 deletion api/core/app/apps/message_based_app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _handle_response(
if e.args[0] == "I/O operation on closed file.": # ignore this error
raise GenerateTaskStoppedError()
else:
logger.exception(e)
logger.exception(f"Failed to handle response, conversation_id: {conversation.id}")
raise e

def _get_conversation_by_user(
Expand Down
4 changes: 3 additions & 1 deletion api/core/app/apps/workflow/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,7 @@ def _handle_response(
if e.args[0] == "I/O operation on closed file.": # ignore this error
raise GenerateTaskStoppedError()
else:
logger.exception(e)
logger.exception(
f"Fails to process generate task pipeline, task_id: {application_generate_entity.task_id}"
)
raise e
2 changes: 1 addition & 1 deletion api/core/app/apps/workflow/generate_task_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def _wrapper_process_stream_response(
else:
yield MessageAudioStreamResponse(audio=audio_trunk.audio, task_id=task_id)
except Exception as e:
logger.exception(e)
logger.exception(f"Fails to get audio trunk, task_id: {task_id}")
break
if tts_publisher:
yield MessageAudioEndStreamResponse(audio="", task_id=task_id)
Expand Down
2 changes: 1 addition & 1 deletion api/core/app/task_pipeline/message_cycle_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _generate_conversation_name_worker(self, flask_app: Flask, conversation_id:
conversation.name = name
except Exception as e:
if dify_config.DEBUG:
logging.exception(f"generate conversation name failed: {e}")
logging.exception(f"generate conversation name failed, conversation_id: {conversation_id}")
pass

db.session.merge(conversation)
Expand Down
2 changes: 1 addition & 1 deletion api/core/helper/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def check_moderation(model_config: ModelConfigWithCredentialsEntity, text: str)
if moderation_result is True:
return True
except Exception as ex:
logger.exception(ex)
logger.exception(f"Fails to check moderation, provider_name: {provider_name}")
raise InvokeBadRequestError("Rate limit exceeded, please try again later.")

return False
2 changes: 1 addition & 1 deletion api/core/helper/module_import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def import_module_from_source(*, module_name: str, py_file_path: AnyStr, use_laz
spec.loader.exec_module(module)
return module
except Exception as e:
logging.exception(f"Failed to load module {module_name} from {py_file_path}: {str(e)}")
logging.exception(f"Failed to load module {module_name} from script file '{py_file_path}'")
raise e


Expand Down
2 changes: 1 addition & 1 deletion api/core/indexing_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def format_qa_document(self, flask_app: Flask, tenant_id: str, document_node, al
qa_documents.append(qa_document)
format_documents.extend(qa_documents)
except Exception as e:
logging.exception(e)
logging.exception("Failed to format qa document")

all_qa_documents.extend(format_documents)

Expand Down
10 changes: 6 additions & 4 deletions api/core/llm_generator/llm_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def generate_suggested_questions_after_answer(cls, tenant_id: str, histories: st
except InvokeError:
questions = []
except Exception as e:
logging.exception(e)
logging.exception("Failed to generate suggested questions after answer")
questions = []

return questions
Expand Down Expand Up @@ -148,7 +148,7 @@ def generate_rule_config(
error = str(e)
error_step = "generate rule config"
except Exception as e:
logging.exception(e)
logging.exception(f"Failed to generate rule config, model: {model_config.get('name')}")
rule_config["error"] = str(e)

rule_config["error"] = f"Failed to {error_step}. Error: {error}" if error else ""
Expand Down Expand Up @@ -234,7 +234,7 @@ def generate_rule_config(
error_step = "generate conversation opener"

except Exception as e:
logging.exception(e)
logging.exception(f"Failed to generate rule config, model: {model_config.get('name')}")
rule_config["error"] = str(e)

rule_config["error"] = f"Failed to {error_step}. Error: {error}" if error else ""
Expand Down Expand Up @@ -286,7 +286,9 @@ def generate_code(
error = str(e)
return {"code": "", "language": code_language, "error": f"Failed to generate code. Error: {error}"}
except Exception as e:
logging.exception(e)
logging.exception(
f"Failed to invoke LLM model, model: {model_config.get('name')}, language: {code_language}"
)
return {"code": "", "language": code_language, "error": f"An unexpected error occurred: {str(e)}"}

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _invoke(
return RerankResult(model=model, docs=rerank_documents)

except Exception as e:
logger.exception(f"Exception in Azure rerank: {e}")
logger.exception(f"Failed to invoke rerank model, model: {model}")
raise

def validate_credentials(self, model: str, credentials: dict) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _invoke(
return RerankResult(model=model, docs=rerank_documents)

except Exception as e:
logger.exception(f"Exception {e}, line : {line}")
logger.exception(f"Failed to invoke rerank model, model: {model}")

def validate_credentials(self, model: str, credentials: dict) -> None:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _invoke(self, model: str, credentials: dict, file: IO[bytes], user: Optional
json_obj = json.loads(json_str)
asr_text = json_obj["text"]
except Exception as e:
logger.exception(f"failed to invoke speech2text model, {e}")
logger.exception(f"failed to invoke speech2text model, model: {model}")
raise CredentialsValidateFailedError(str(e))

return asr_text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _invoke(
return TextEmbeddingResult(embeddings=all_embeddings, usage=usage, model=model)

except Exception as e:
logger.exception(f"Exception {e}, line : {line}")
logger.exception(f"Failed to invoke text embedding model, model: {model}, line: {line}")

def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int:
"""
Expand Down
2 changes: 1 addition & 1 deletion api/core/moderation/output_moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ def moderation(self, tenant_id: str, app_id: str, moderation_buffer: str) -> Opt
result: ModerationOutputsResult = moderation_factory.moderation_for_outputs(moderation_buffer)
return result
except Exception as e:
logger.exception("Moderation Output error: %s", e)
logger.exception(f"Moderation Output error, app_id: {app_id}")

return None
4 changes: 2 additions & 2 deletions api/core/ops/ops_trace_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ def add_trace_task(self, trace_task: TraceTask):
trace_task.app_id = self.app_id
trace_manager_queue.put(trace_task)
except Exception as e:
logging.exception(f"Error adding trace task: {e}")
logging.exception(f"Error adding trace task, trace_type {trace_task.trace_type}")
finally:
self.start_timer()

Expand All @@ -730,7 +730,7 @@ def run(self):
if tasks:
self.send_to_celery(tasks)
except Exception as e:
logging.exception(f"Error processing trace tasks: {e}")
logging.exception("Error processing trace tasks")

def start_timer(self):
global trace_manager_timer
Expand Down
2 changes: 1 addition & 1 deletion api/core/rag/datasource/vdb/couchbase/couchbase_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def delete_by_ids(self, ids: list[str]) -> None:
try:
self._cluster.query(query, named_parameters={"doc_ids": ids}).execute()
except Exception as e:
logger.exception(e)
logger.exception(f"Failed to delete documents, ids: {ids}")

def delete_by_document_id(self, document_id: str):
query = f"""
Expand Down
8 changes: 4 additions & 4 deletions api/core/rag/datasource/vdb/lindorm/lindorm_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __fetch_existing_ids(batch_ids: list[str]) -> set[str]:
existing_docs = self._client.mget(index=self._collection_name, body={"ids": batch_ids}, _source=False)
return {doc["_id"] for doc in existing_docs["docs"] if doc["found"]}
except Exception as e:
logger.exception(f"Error fetching batch {batch_ids}: {e}")
logger.exception(f"Error fetching batch {batch_ids}")
return set()

@retry(stop=stop_after_attempt(3), wait=wait_fixed(60))
Expand All @@ -96,7 +96,7 @@ def __fetch_existing_routing_ids(batch_ids: list[str], route_ids: list[str]) ->
)
return {doc["_id"] for doc in existing_docs["docs"] if doc["found"]}
except Exception as e:
logger.exception(f"Error fetching batch {batch_ids}: {e}")
logger.exception(f"Error fetching batch ids: {batch_ids}")
return set()

if ids is None:
Expand Down Expand Up @@ -177,7 +177,7 @@ def delete(self) -> None:
else:
logger.warning(f"Index '{self._collection_name}' does not exist. No deletion performed.")
except Exception as e:
logger.exception(f"Error occurred while deleting the index: {e}")
logger.exception(f"Error occurred while deleting the index: {self._collection_name}")
raise e

def text_exists(self, id: str) -> bool:
Expand All @@ -201,7 +201,7 @@ def search_by_vector(self, query_vector: list[float], **kwargs: Any) -> list[Doc
try:
response = self._client.search(index=self._collection_name, body=query)
except Exception as e:
logger.exception(f"Error executing search: {e}")
logger.exception(f"Error executing vector search, query: {query}")
raise

docs_and_scores = []
Expand Down
2 changes: 1 addition & 1 deletion api/core/rag/datasource/vdb/myscale/myscale_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _search(self, dist: str, order: SortOrder, **kwargs: Any) -> list[Document]:
for r in self._client.query(sql).named_results()
]
except Exception as e:
logging.exception(f"\033[91m\033[1m{type(e)}\033[0m \033[95m{str(e)}\033[0m")
logging.exception(f"\033[91m\033[1m{type(e)}\033[0m \033[95m{str(e)}\033[0m") # noqa:TRY401
return []

def delete(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def search_by_vector(self, query_vector: list[float], **kwargs: Any) -> list[Doc
try:
response = self._client.search(index=self._collection_name.lower(), body=query)
except Exception as e:
logger.exception(f"Error executing search: {e}")
logger.exception(f"Error executing vector search, query: {query}")
raise

docs = []
Expand Down
8 changes: 4 additions & 4 deletions api/core/rag/embedding/cached_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def embed_documents(self, texts: list[str]) -> list[list[float]]:
except IntegrityError:
db.session.rollback()
except Exception as e:
logging.exception("Failed transform embedding: %s", e)
logging.exception("Failed transform embedding")
cache_embeddings = []
try:
for i, embedding in zip(embedding_queue_indices, embedding_queue_embeddings):
Expand All @@ -89,7 +89,7 @@ def embed_documents(self, texts: list[str]) -> list[list[float]]:
db.session.rollback()
except Exception as ex:
db.session.rollback()
logger.exception("Failed to embed documents: %s", ex)
logger.exception("Failed to embed documents: %s")
raise ex

return text_embeddings
Expand All @@ -112,7 +112,7 @@ def embed_query(self, text: str) -> list[float]:
embedding_results = (embedding_results / np.linalg.norm(embedding_results)).tolist()
except Exception as ex:
if dify_config.DEBUG:
logging.exception(f"Failed to embed query text: {ex}")
logging.exception(f"Failed to embed query text '{text[:10]}...({len(text)} chars)'")
raise ex

try:
Expand All @@ -126,7 +126,7 @@ def embed_query(self, text: str) -> list[float]:
redis_client.setex(embedding_cache_key, 600, encoded_str)
except Exception as ex:
if dify_config.DEBUG:
logging.exception("Failed to add embedding to redis %s", ex)
logging.exception(f"Failed to add embedding to redis for the text '{text[:10]}...({len(text)} chars)'")
raise ex

return embedding_results
2 changes: 1 addition & 1 deletion api/core/rag/extractor/word_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def parse_docx(self, docx_path, image_folder):
for i in url_pattern.findall(x.text):
hyperlinks_url = str(i)
except Exception as e:
logger.exception(e)
logger.exception("Failed to parse HYPERLINK xml")

def parse_paragraph(paragraph):
paragraph_content = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _format_qa_document(self, flask_app: Flask, tenant_id: str, document_node, a
qa_documents.append(qa_document)
format_documents.extend(qa_documents)
except Exception as e:
logging.exception(e)
logging.exception("Failed to format qa document")

all_qa_documents.extend(format_documents)

Expand Down
Loading

0 comments on commit e92d323

Please sign in to comment.