主页

方法论-查找报错原因

2024-08-31 02:31PM

1. 查看报错日志

2. 查看源代码

3. 打印报错日志

4. 拆分打印某个报错的参数。

可以看下面这个列子:

在动态排放项目中,新建计算方案的时候同时计算:消防-HFC、房间空调-HFC410A的时候,就报错说:

2024/08/31 14:15:13 [Recovery] 2024/08/31 - 14:15:13 panic recovered:
POST /api/v1/calculation_plans?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6ImFkbWluIiwicGFzc3dvcmQiOiJhZG1pbiIsImV4cCI6MTcyNTA5NTY5MywiaXNzIjoiZ2luLWRvbmd0YWlwYWlmYW5nIn0.fPFD5V-xxNbzpjdBDcU9yP7VdrqXIy3XKCwZJMMUqtA HTTP/1.1
Host: localhost:8000
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Connection: keep-alive
Content-Length: 7492
Content-Type: application/json
Origin: http://localhost:3000
Postman-Token: a236a059-e658-472a-89e7-2e3abecd469c
Priority: u=0
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0


runtime error: index out of range [0] with length 0
/home/meiyi/Downloads/go/src/runtime/panic.go:113 (0x4357be)
    goPanicIndex: panic(boundsError{x: int64(x), signed: true, y: y, code: boundsIndex})
/home/meiyi/workspace/dongtaipaifang_2_backend/routers/api/v1/calculation_plan.go:561 (0xab8a24)
    getParameterForFireControlHFC: ef, _ := strconv.ParseFloat(row[otherTableMapping[0]], 64)
/home/meiyi/workspace/dongtaipaifang_2_backend/routers/api/v1/calculation_plan.go:217 (0xab3cc4)
    AddCalculationPlan: returnResult = calculator.CalculateForHfc(getParameterForFireControlHFC(req, myData, materialName, algorithm))

源代码代码内容如下:

 // 消防-HFC

 539 func getParameterForFireControlHFC(req AddCalculationPlanRequest, myData [][]string, materialName string, algorithm models.CalculationTemplates) [](map[string]interface{}) {
 540   parameterForFireControlHFC := make([](map[string]interface{}), 0)
 541   otherTableMapping := req.AlgorithmAndParameterColumnMappingForTable2["消防-HFC"][materialName]
 542 
 543   for year := req.StartYear; year <= req.EndYear; year++ {
 544     currentYear := year - req.StartYear + 1 // 将年份改为连续数字
 545     oneYearParameterForFireControlHFC := make(map[string]interface{}, 0)
 546     oneYearParameterForFireControlHFC["year"] = year
 547 
 548     for _, row := range myData {
 549       tempYear, _ := strconv.Atoi(row[0]) 
 550       if tempYear == year {
 551         consumption, _ := strconv.Atoi(row[1])
 552         oneYearParameterForFireControlHFC["consumption"] = consumption
 553       } 
 554     } 
 555     oneYearParameterForFireControlHFC["life"] = algorithm.Life
 556     
 557     otherTable2 := req.OtherTables["消防-HFC"]["table2"]
 558     for _, row := range otherTable2 {
 559       tempYear, _ := strconv.Atoi(row[0])
 560       if tempYear == currentYear {
 561         ef, _ := strconv.ParseFloat(row[otherTableMapping[0]], 64)
 562         oneYearParameterForFireControlHFC["ef"] = ef
 563       } 
 564     } 
 565     parameterForFireControlHFC = append(parameterForFireControlHFC, oneYearParameterForFireControlHFC)
 566   } 
 567   fmt.Println(" === parameterForFireControlHFC:", parameterForFireControlHFC)
 568   return parameterForFireControlHFC
 569 }

在编译日志中可以看到报错是因为561行 index out of range[0] with length 0

打印561行的代码:

 569       if tempYear == currentYear {
 570         fmt.Println("=========tempYear22222222:", tempYear)
 571         fmt.Println("====otherTableMapping222222:", otherTableMapping)
 572         ef, _ := strconv.ParseFloat(row[otherTableMapping[0]], 64)

 发现otherTableMapping为[]

=========tempYear22222222: 1
====otherTableMapping222222: []

然后打印otherTableMapping,还有查看otherTableMapping是如何定义的

 541   otherTableMapping := req.AlgorithmAndParameterColumnMappingForTable2["消防-HFC"][materialName]
 542   fmt.Println("========req.AlgorithmAndParameterColumnMappingForTable2", req.AlgorithmAndParameterColumnMappingForTable2["消防-HFC"][materialName])

 打印的结果发现

========req.AlgorithmAndParameterColumnMappingForTable2 []

然后拆分打印 req.AlgorithmAndParameterColumnMappingForTable2["消防-HFC"][materialName],看看是哪个参数有问题。

 542   fmt.Println("========req.AlgorithmAndParameterColumnMappingForTable2", req.AlgorithmAndParameterColumnMappingForTable2["消防-HFC"][materialName])
 543   fmt.Println("========2222222222req.AlgorithmAndParameterColumnMappingForTable2", req.AlgorithmAndParameterColumnMappingForTable2)
 544   fmt.Println("========333333req.AlgorithmAndParameterColumnMappingForTable2", req.AlgorithmAndParameterColumnMappingForTable2["消防-HFC"])
 545   fmt.Println("========materialName", materialName)

打印结果就发现是 materialName 有问题,消防的没有包含HFC-125,所以导致req.AlogrithmAndParameterCoulmnMappingForTable2为[]

========req.AlgorithmAndParameterColumnMappingForTable2 []
========2222222222req.AlgorithmAndParameterColumnMappingForTable2 map[房间空调-HFC410A:map[HFC-125:[1 2 3 4 5]] 消防-HFC:map[HFC-227ea:[1] HFC-236fa:[2]]]
========333333req.AlgorithmAndParameterColumnMappingForTable2 map[HFC-227ea:[1] HFC-236fa:[2]]
========materialName HFC-125 

所以就找到了报错的原因。 

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论