Skip to content

Commit 08d808f

Browse files
committed
Eradicate panics
1 parent 2ffc2fc commit 08d808f

7 files changed

+31
-17
lines changed

course_types_groups.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func populateUserCourseTypesAndGroups(
150150
}
151151
course, ok := _course.(*courseT)
152152
if !ok {
153-
panic("courses map has non-\"*courseT\" items")
153+
return errType
154154
}
155155
thisGroupName = course.Group
156156
thisTypeName = course.Type

endpoint_export_choices.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func handleExportChoices(
9292
}
9393
course, ok := _course.(*courseT)
9494
if !ok {
95-
panic("courses map has non-\"*courseT\" items")
95+
return "", -1, errType
9696
}
9797
if course == nil {
9898
return "", -1, wrapAny(errNoSuchCourse, currentCourseID)

endpoint_index.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ func handleIndex(w http.ResponseWriter, req *http.Request) (string, int, error)
5858
Courses: &_coursemap,
5959
}
6060
}
61+
err = nil
6162
courses.Range(func(key, value interface{}) bool {
6263
courseID, ok := key.(int)
6364
if !ok {
64-
panic("courses map has non-\"int\" keys")
65+
err = errType
66+
return false
6567
}
6668
course, ok := value.(*courseT)
6769
if !ok {
68-
panic("courses map has non-\"*courseT\" items")
70+
err = errType
71+
return false
6972
}
7073
if department != staffDepartment {
7174
if yearGroupsNumberBits[department]&course.YearGroups == 0 {
@@ -75,6 +78,9 @@ func handleIndex(w http.ResponseWriter, req *http.Request) (string, int, error)
7578
(*_groups[course.Group].Courses)[courseID] = course
7679
return true
7780
})
81+
if err != nil {
82+
return "", -1, err
83+
}
7884

7985
if department == staffDepartment {
8086
StatesDereferenced := map[string]uint32{}

ws_connection.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func handleConn(
3838
c *websocket.Conn,
3939
userID string,
4040
department string,
41-
) error {
41+
) (reterr error) {
4242
_state, ok := states[department]
4343
if !ok {
4444
return errNoSuchYearGroup
@@ -76,14 +76,17 @@ func handleConn(
7676
usems := make(map[int]*usemT)
7777

7878
atomic.AddInt64(&usemCount, int64(atomic.LoadUint32(&numCourses)))
79+
var err error
7980
courses.Range(func(key, value interface{}) bool {
8081
courseID, ok := key.(int)
8182
if !ok {
82-
panic("courses map has non-\"int\" keys")
83+
err = errType
84+
return false
8385
}
8486
course, ok := value.(*courseT)
8587
if !ok {
86-
panic("courses map has non-\"*courseT\" items")
88+
err = errType
89+
return false
8790
}
8891
usem := &usemT{} //exhaustruct:ignore
8992
usem.init()
@@ -97,7 +100,8 @@ func handleConn(
97100
_ = key
98101
course, ok := value.(*courseT)
99102
if !ok {
100-
panic("courses map has non-\"*courseT\" items")
103+
reterr = errType
104+
return false
101105
}
102106
course.Usems.Delete(userID)
103107
return true
@@ -137,7 +141,7 @@ func handleConn(
137141

138142
var userCourseGroups userCourseGroupsT = make(map[string]struct{})
139143
var userCourseTypes userCourseTypesT = make(map[string]int)
140-
err := populateUserCourseTypesAndGroups(
144+
err = populateUserCourseTypesAndGroups(
141145
newCtx,
142146
&userCourseTypes,
143147
&userCourseGroups,

ws_utils.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func propagateSelectedUpdate(course *courseT) {
7272
_ = key
7373
usem, ok := value.(*usemT)
7474
if !ok {
75-
panic("Usems contains non-\"*usemT\" value")
75+
slog.Error(errType.Error())
76+
return false
7677
}
7778
usem.set()
7879
return true
@@ -90,7 +91,7 @@ func sendSelectedUpdate(
9091
}
9192
course, ok := _course.(*courseT)
9293
if !ok {
93-
panic("courses map has non-\"*courseT\" items")
94+
return errType
9495
}
9596
if course == nil {
9697
return fmt.Errorf("%w: %d", errNoSuchCourse, courseID)
@@ -111,17 +112,20 @@ func propagate(yeargroup string, msg string) error {
111112
if !ok {
112113
return errNoSuchYearGroup
113114
}
115+
var err error
114116
chanSubPool.Range(func(_userID, _ch interface{}) bool {
115117
ch, ok := _ch.(*chan string)
116118
if !ok {
117-
panic("chanPool has non-\"*chan string\" key")
119+
err = errType
120+
return false
118121
}
119122
select {
120123
case *ch <- msg:
121124
default:
122125
userID, ok := _userID.(string)
123126
if !ok {
124-
panic("chanPool has non-string key")
127+
err = errType
128+
return false
125129
}
126130
slog.Warn(
127131
"sendq",
@@ -131,7 +135,7 @@ func propagate(yeargroup string, msg string) error {
131135
}
132136
return true
133137
})
134-
return nil
138+
return err
135139
}
136140

137141
func writeText(ctx context.Context, c *websocket.Conn, msg string) error {

wsmsg_choose.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func messageChooseCourse(
6868
}
6969
course, ok := _course.(*courseT)
7070
if !ok {
71-
panic("courses map has non-\"*courseT\" items")
71+
return errType
7272
}
7373
if course == nil {
7474
return errNoSuchCourse

wsmsg_unchoose.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func messageUnchooseCourse(
6363
}
6464
course, ok := _course.(*courseT)
6565
if !ok {
66-
panic("courses map has non-\"*courseT\" items")
66+
return errType
6767
}
6868
if course == nil {
6969
return errNoSuchCourse
@@ -94,7 +94,7 @@ func messageUnchooseCourse(
9494
}
9595
course, ok := _course.(*courseT)
9696
if !ok {
97-
panic("courses map has non-\"*courseT\" items")
97+
return errType
9898
}
9999
if course == nil {
100100
return errNoSuchCourse

0 commit comments

Comments
 (0)