Code RoomError wrapping loses original type
MediumPrep Room Coding #1953

Error wrapping loses original type

Code reviewCode quality & reviewAlgorithms & data structuresMid–Senior~16 min

Review this Go function that wraps a downstream error before returning it. The caller wants to treat 'not found' differently from a real outage.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 16 min
Mark a line and say what kind of problem it is.0 findings
1func loadUser(id string) (*User, error) {
2 u, err := repo.Find(id)
3 if err != nil {
4 return nil, fmt.Errorf("failed to load user %s", id)
5 }
6 return u, nil
7}
8 
9func handler(w http.ResponseWriter, id string) {
10 u, err := loadUser(id)
11 if errors.Is(err, sql.ErrNoRows) {
12 http.Error(w, "no such user", 404)
13 return
14 }
15 if err != nil {
16 http.Error(w, "internal error", 500)
17 return
18 }
19 writeUser(w, u)
20}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.