mirror of
https://github.com/openziti/ziti.git
synced 2026-11-25 00:41:54 +00:00
a827bfbe14
- 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
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/openziti/foundation/v2/errorz"
|
|
"github.com/openziti/ziti/v2/controller/apierror"
|
|
"github.com/openziti/ziti/v2/controller/storage/boltz"
|
|
)
|
|
|
|
func ToApiError(err error) *errorz.ApiError {
|
|
return ToApiErrorWithDefault(err, errorz.NewUnhandled)
|
|
}
|
|
|
|
func ToApiErrorWithDefault(err error, f func(err error) *errorz.ApiError) *errorz.ApiError {
|
|
var apiErr *errorz.ApiError
|
|
if errors.As(err, &apiErr) {
|
|
return apiErr
|
|
}
|
|
|
|
if boltz.IsErrNotFoundErr(err) {
|
|
result := errorz.NewNotFound()
|
|
result.Cause = err
|
|
return result
|
|
}
|
|
|
|
var fe *errorz.FieldError
|
|
if errors.As(err, &fe) {
|
|
return errorz.NewFieldApiError(fe)
|
|
}
|
|
|
|
var sve *apierror.ValidationErrors
|
|
if errors.As(err, &sve) {
|
|
return errorz.NewCouldNotValidate(sve)
|
|
}
|
|
|
|
var duplicate *boltz.UniqueIndexDuplicateError
|
|
if errors.As(err, &duplicate) {
|
|
return errorz.NewFieldApiError(&errorz.FieldError{
|
|
Reason: duplicate.Error(),
|
|
FieldName: duplicate.Field,
|
|
FieldValue: duplicate.Value,
|
|
})
|
|
}
|
|
|
|
var referenced *boltz.ReferenceExistsError
|
|
if errors.As(err, &referenced) {
|
|
return apierror.NewCanNotDeleteReferencedEntity(referenced.LocalType, referenced.RemoteType, referenced.RemoteIds, referenced.RemoteField)
|
|
}
|
|
|
|
return f(err)
|
|
}
|