mirror of
https://github.com/openziti/ziti.git
synced 2026-10-26 13:39:58 +00:00
c0109319a6
- the peer controller error encoder read err.Code into a JSON map, which after the AppCode rename is a bound method that json.Marshal rejects, so every forwarded API error fell to the generic path; it now encodes AppCode and, as on 2.0.x, sends a cause with nothing to marshal as text - the REST responder logged e.Code as a field for the same reason - the perf-tagged rate limiter test compared the method value to a code string - extracts encodeApiError and tests that the encoding succeeds and carries the code as a string
95 lines
3.2 KiB
Go
95 lines
3.2 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 (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/raft"
|
|
"github.com/openziti/channel/v4"
|
|
"github.com/openziti/foundation/v2/errorz"
|
|
"github.com/openziti/ziti/common/pb/cmd_pb"
|
|
"github.com/openziti/ziti/controller/models"
|
|
"github.com/openziti/ziti/controller/peermsg"
|
|
"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")
|
|
}
|
|
}
|
|
|
|
// encodeApiError serializes err for a peer controller, which rebuilds a typed error from the
|
|
// code, message and status fields. A cause that marshals to nothing useful is sent as its text.
|
|
func encodeApiError(err *errorz.ApiError) ([]byte, error) {
|
|
encodingMap := map[string]interface{}{}
|
|
encodingMap["code"] = err.AppCode
|
|
encodingMap["message"] = err.Message
|
|
encodingMap["status"] = err.Status
|
|
|
|
if err.Cause != nil {
|
|
encodingMap["causeType"] = fmt.Sprintf("%T", err.Cause)
|
|
if causeBytes, causeErr := json.Marshal(err.Cause); causeErr == nil && string(causeBytes) != "{}" {
|
|
encodingMap["cause"] = err.Cause
|
|
} else {
|
|
encodingMap["cause"] = err.Cause.Error()
|
|
}
|
|
}
|
|
|
|
return json.Marshal(encodingMap)
|
|
}
|
|
|
|
func sendApiErrorResponse(m *channel.Message, ch channel.Channel, err *errorz.ApiError) {
|
|
buf, encodeErr := 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")
|
|
}
|
|
}
|