drone/internal/api/usererror/translate.go
Johannes Batzill 335998505e [MISC] Introduce PathStore and Move PathLogic to Controller Layer (#226)
This change is a follow up on the effort to remove transactions and sync stores from the database layer, and move them in the application layer. It is addressing the repo + space store.
The following changes are included:
- Introduce PathStore storing resource paths (repo + space)
- Add foreign key from path.repo_id/space_id to repo.id/space.id
- Add foreign key from repo/space.parent_id to space.id
- Remove repo_sync and space_sync
- Move path creation logic to repo/space controller (for both creating primary paths for new repos/spaces, as well as creating alias spaces)
- Reimplement repo/space moving on controller layer
2023-01-18 11:34:12 -08:00

72 lines
1.9 KiB
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package usererror
import (
"errors"
"net/http"
"github.com/harness/gitness/gitrpc"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/webhook"
"github.com/harness/gitness/types/check"
"github.com/rs/zerolog/log"
)
func Translate(err error) *Error {
var rError *Error
var checkError *check.ValidationError
switch {
// api errors
case errors.As(err, &rError):
return rError
// api auth errors
case errors.Is(err, apiauth.ErrNotAuthenticated):
return ErrUnauthorized
case errors.Is(err, apiauth.ErrNotAuthorized):
return ErrForbidden
// validation errors
case errors.As(err, &checkError):
return New(http.StatusBadRequest, checkError.Error())
// store errors
case errors.Is(err, store.ErrResourceNotFound):
return ErrNotFound
case errors.Is(err, store.ErrDuplicate):
return ErrDuplicate
case errors.Is(err, store.ErrPrimaryPathCantBeDeleted):
return ErrPrimaryPathCantBeDeleted
case errors.Is(err, store.ErrPathTooLong):
return ErrPathTooLong
case errors.Is(err, store.ErrNoChangeInRequestedMove):
return ErrNoChange
case errors.Is(err, store.ErrIllegalMoveCyclicHierarchy):
return ErrCyclicHierarchy
case errors.Is(err, store.ErrSpaceWithChildsCantBeDeleted):
return ErrSpaceWithChildsCantBeDeleted
// gitrpc errors
case errors.Is(err, gitrpc.ErrAlreadyExists):
return ErrDuplicate
case errors.Is(err, gitrpc.ErrInvalidArgument):
return ErrBadRequest
case errors.Is(err, gitrpc.ErrNotFound):
return ErrNotFound
// webhook errors
case errors.Is(err, webhook.ErrWebhookNotRetriggerable):
return ErrWebhookNotRetriggerable
// unknown error
default:
log.Warn().Msgf("Unable to translate error: %s", err)
return ErrInternal
}
}