Browse Source

修复 mahonia ConvertString 引发的 panic 没有被处理的问题

Signed-off-by: 716 <[email protected]>
716 4 years ago
parent
commit
7d943b2ef4
2 changed files with 7 additions and 1 deletions
  1. 2 1
      downloader_test.go
  2. 5 0
      model/language.go

+ 2 - 1
downloader_test.go

@@ -30,7 +30,8 @@ func TestDownloader_DownloadSub(t *testing.T) {
 	var err error
 	//dirRoot := "X:\\电影\\Spiral From the Book of Saw (2021)"
 	//dirRoot := "X:\\电影\\Oslo (2021)"
-	dirRoot := "X:\\电影\\The Devil All the Time (2020)"
+	//dirRoot := "X:\\电影\\The Devil All the Time (2020)"
+	dirRoot := "X:\\电影\\冰海陷落 (2018)"
 
 	dl := NewDownloader(common.ReqParam{
 		SaveMultiSub: true,

+ 5 - 0
model/language.go

@@ -278,6 +278,11 @@ func IsChineseSimpleOrTraditional(inputFileName string, orgLang common.Language)
 
 // ConvertToString 将字符串从原始编码转换到目标编码,需要配合字符串检测编码库使用 chardet.NewTextDetector()
 func ConvertToString(src string, srcCode string, tagCode string) string {
+	defer func() {
+		if err := recover(); err != nil {
+			GetLogger().Errorln("ConvertToString panic:", err)
+		}
+	}()
 	srcCoder := mahonia.NewDecoder(srcCode)
 	srcResult := srcCoder.ConvertString(src)
 	tagCoder := mahonia.NewDecoder(tagCode)