From 2e4c0080098c06931834c54bc6e767ed087e0a30 Mon Sep 17 00:00:00 2001 From: qiujiacai Date: Fri, 22 Sep 2023 17:37:40 +0800 Subject: [PATCH] gitlint:add exception handling --- .../openeuler_embedded_commit_rules.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/scripts/gitlint/openeuler_embedded_commit_rules.py b/scripts/gitlint/openeuler_embedded_commit_rules.py index 4b92ba4cd6c..671392383e1 100644 --- a/scripts/gitlint/openeuler_embedded_commit_rules.py +++ b/scripts/gitlint/openeuler_embedded_commit_rules.py @@ -106,11 +106,21 @@ class TitleLength(LineRule): def validate(self, line, _commit): result = [] - if line.startswith("revert"): - max_length = self.options['line-max-length-revert'].value - else: - max_length = self.options['line-max-length-no-revert'].value - self.message = "Title exceeds max length ({0}>{1}) when revert" + try: + if line.startswith("revert"): + max_length = self.options['line-max-length-revert'].value + else: + max_length = self.options['line-max-length-no-revert'].value + self.message = "Title exceeds max length ({0}>{1}) when revert" + except KeyError as e: + # 处理选项不存在的异常,可以记录日志或者返回错误信息 + result.append(RuleViolation(self.id, f"Error: {e}", line)) + return result + except Exception as ex: + # 处理其他可能的异常 + result.append(RuleViolation(self.id, f"An unexpected error occurred: {ex}", line)) + return result + if len(line) > max_length: result.append(RuleViolation(self.id, self.message.format(len(line), max_length), line)) -- Gitee