Files
ziti/controller/handler_peer_ctrl/response.go
T
Paul Lorenz a827bfbe14 Carry structured API error causes across the peer channel. Fixes #4538
- maps UniqueIndexDuplicateError and ReferenceExistsError in ToApiError, so forwarded duplicate-name creates and referenced deletes return 400 and 409 from a follower instead of 500
- replaces the hand-written error switches in the create, delete, update and patch route helpers with ToApiError
- moves the peer channel API error encoding and decoding into one cause codec registry in the raft package, covering not-found, field, duplicate index, reference exists and schema validation errors
- parses both shapes ValidationErrors marshals to, one error bare or several under Reason and Errors
- sends an unregistered cause as its message on both sides instead of a raw JSON object
- tests each mapping, a round trip for every cause class, and a duplicate-name service create through the API
2026-10-28 17:22:07 -04:00

73 lines
2.5 KiB
Go

/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handler_peer_ctrl
import (
"github.com/hashicorp/raft"
"github.com/openziti/channel/v5"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/ziti/v2/common/pb/cmd_pb"
"github.com/openziti/ziti/v2/controller/models"
"github.com/openziti/ziti/v2/controller/peermsg"
ctrlraft "github.com/openziti/ziti/v2/controller/raft"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func sendErrorResponseCalculateType(m *channel.Message, ch channel.Channel, err error) {
if errors.Is(err, raft.ErrNotLeader) {
sendErrorResponse(m, ch, err, peermsg.ErrorCodeNotLeader)
} else {
sendApiErrorResponse(m, ch, models.ToApiError(err))
}
}
func sendErrorResponse(m *channel.Message, ch channel.Channel, err error, errorCode uint32) {
resp := channel.NewMessage(int32(cmd_pb.ContentType_ErrorResponseType), []byte(err.Error()))
resp.ReplyTo(m)
resp.PutUint32Header(peermsg.HeaderErrorCode, errorCode)
if sendErr := ch.Send(resp); sendErr != nil {
logrus.WithError(sendErr).Error("error while sending error response")
}
}
func sendApiErrorResponse(m *channel.Message, ch channel.Channel, err *errorz.ApiError) {
buf, encodeErr := ctrlraft.EncodeApiError(err)
if encodeErr != nil {
logrus.WithError(encodeErr).WithField("apiErr", err).Error("unable to encode api error")
sendErrorResponse(m, ch, err, peermsg.ErrorCodeGeneric)
return
}
resp := channel.NewMessage(int32(cmd_pb.ContentType_ErrorResponseType), buf)
resp.ReplyTo(m)
resp.PutUint32Header(peermsg.HeaderErrorCode, peermsg.ErrorCodeApiError)
if sendErr := ch.Send(resp); sendErr != nil {
logrus.WithError(sendErr).Error("error while sending error response")
}
}
func sendSuccessResponse(m *channel.Message, ch channel.Channel, index uint64) {
resp := channel.NewMessage(int32(cmd_pb.ContentType_SuccessResponseType), nil)
resp.ReplyTo(m)
resp.PutUint64Header(peermsg.HeaderIndex, index)
if sendErr := ch.Send(resp); sendErr != nil {
logrus.WithError(sendErr).Error("error while sending success response")
}
}