Checking an error type inaccurately

Mistake

This stops working with wrapped errors

if err != nil {
  switch err := err.(type) {
  case transientError:
    http.Error(w, err.Error(), http.StatusServiceUnavailable)
  default:
    http.Error(w, err.Error(), http.StatusBadRequest)
  }
  return
}

Fix

Use the errors.As() function

if err != nil {
  if errors.As(err, &transientError{}){
    http.Error(w, err.Error(), http.StatusServiceUnavailable)
  } else {
    http.Error(w, err.Error(), http.StatusBadRequest)
  }
  return
}

References