drone/internal/api/usererror/translate.go
Johannes Batzill ecb6276fa8 Add EOF to RPC File Chunk & Cleanups (#29)
Adds an EOF flag to data chunks to avoid using []byte("EOF") as end (due to potential false positives).
Furthermore, a few cleanups are done:
- Add TODOs for initial git changes
- Add missing file headers
- Fix typo for license (was licence)
- Fix make wire target
2022-10-12 10:21:53 -07:00

56 lines
1.5 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"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/types/check"
"github.com/rs/zerolog/log"
)
func Translate(err error) *Error {
var rError *Error
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.Is(err, check.ErrAny):
return New(http.StatusBadRequest, err.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
// unknown error
default:
log.Warn().Msgf("Unable to translate error: %s", err)
return ErrInternal
}
}