From 3d35cc20ec1eac61e2ebab3a8c04625c83ba4996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Fri, 12 Dec 2025 17:26:27 +0100 Subject: [PATCH] Use `WriteStateFetchError` in API handlers whenever possible (#16140) **What type of PR is this?** Other **What does this PR do? Why is it needed?** Calls to `Stater.StateBySlot` and `Stater.State` should be followed by `shared.WriteStateFetchError` to provide the most robust error handling. **Acknowledgements** - [x] I have read [CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md). - [x] I have included a uniquely named [changelog fragment file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd). - [x] I have added a description with sufficient context for reviewers to understand this PR. - [x] I have tested that my changes work as expected and I added a testing plan to the PR description (if applicable). --- beacon-chain/rpc/eth/rewards/handlers.go | 2 +- beacon-chain/rpc/eth/validator/handlers.go | 8 ++++---- changelog/radek_use-statefetch-error.md | 3 +++ 3 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 changelog/radek_use-statefetch-error.md diff --git a/beacon-chain/rpc/eth/rewards/handlers.go b/beacon-chain/rpc/eth/rewards/handlers.go index 87be7a5a60..9174feb1b6 100644 --- a/beacon-chain/rpc/eth/rewards/handlers.go +++ b/beacon-chain/rpc/eth/rewards/handlers.go @@ -228,7 +228,7 @@ func (s *Server) attRewardsState(w http.ResponseWriter, r *http.Request) (state. } st, err := s.Stater.StateBySlot(r.Context(), nextEpochEnd) if err != nil { - httputil.HandleError(w, "Could not get state for epoch's starting slot: "+err.Error(), http.StatusInternalServerError) + shared.WriteStateFetchError(w, err) return nil, false } return st, true diff --git a/beacon-chain/rpc/eth/validator/handlers.go b/beacon-chain/rpc/eth/validator/handlers.go index a382c7d386..11f0488a02 100644 --- a/beacon-chain/rpc/eth/validator/handlers.go +++ b/beacon-chain/rpc/eth/validator/handlers.go @@ -911,7 +911,7 @@ func (s *Server) GetAttesterDuties(w http.ResponseWriter, r *http.Request) { st, err := s.Stater.StateBySlot(ctx, startSlot) if err != nil { - httputil.HandleError(w, "Could not get state: "+err.Error(), http.StatusInternalServerError) + shared.WriteStateFetchError(w, err) return } @@ -1030,7 +1030,7 @@ func (s *Server) GetProposerDuties(w http.ResponseWriter, r *http.Request) { if requestedEpoch < currentEpoch { st, err = s.Stater.StateBySlot(ctx, epochStartSlot) if err != nil { - httputil.HandleError(w, fmt.Sprintf("Could not get state for slot %d: %v ", epochStartSlot, err), http.StatusInternalServerError) + shared.WriteStateFetchError(w, err) return } } else { @@ -1181,7 +1181,7 @@ func (s *Server) GetSyncCommitteeDuties(w http.ResponseWriter, r *http.Request) } st, err := s.Stater.State(ctx, []byte(strconv.FormatUint(uint64(slot), 10))) if err != nil { - httputil.HandleError(w, "Could not get sync committee state: "+err.Error(), http.StatusInternalServerError) + shared.WriteStateFetchError(w, err) return } @@ -1327,7 +1327,7 @@ func (s *Server) GetLiveness(w http.ResponseWriter, r *http.Request) { } st, err = s.Stater.StateBySlot(ctx, epochEnd) if err != nil { - httputil.HandleError(w, "Could not get slot for requested epoch: "+err.Error(), http.StatusInternalServerError) + shared.WriteStateFetchError(w, err) return } participation, err = st.CurrentEpochParticipation() diff --git a/changelog/radek_use-statefetch-error.md b/changelog/radek_use-statefetch-error.md new file mode 100644 index 0000000000..dcf01c910d --- /dev/null +++ b/changelog/radek_use-statefetch-error.md @@ -0,0 +1,3 @@ +### Ignored + +- Use `WriteStateFetchError` in API handlers whenever possible. \ No newline at end of file