mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
Adds a repository-level **"Restrict to collaborators"** option under the Issues and Pull Request unit settings. When enabled, only users with collaborator access to the base repository (owner, team members, or explicit collaborators) can open issues or pull requests against it. Public repositories often need to be visible/cloneable by everyone, but owners may want to limit who can open pull requests (e.g. restrict to an internal team while still keeping the code public) * Fixes: https://github.com/go-gitea/gitea/issues/40015 * Fixes: https://github.com/go-gitea/gitea/issues/31890 * Fixes: https://github.com/go-gitea/gitea/issues/28021 * Fixes: https://github.com/go-gitea/gitea/issues/14801 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
139 lines
4.3 KiB
Go
139 lines
4.3 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package util
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"html/template"
|
|
)
|
|
|
|
// This file defines common errors forming the base of our error system.
|
|
// These errors can be used to classify errors and to provide a common,
|
|
// safe (no server-side sensitive information), and translatable error message for end users.
|
|
|
|
// ErrorTranslatable wraps an error with translation information
|
|
type ErrorTranslatable interface {
|
|
error
|
|
Unwrap() error
|
|
Translate(ErrorLocaleTranslator) template.HTML
|
|
}
|
|
|
|
var (
|
|
ErrInvalidArgument = errorForUser{400, "invalid argument"}
|
|
ErrPermissionDenied = errorForUser{403, "permission denied"}
|
|
ErrNotExist = errorForUser{404, "resource does not exist"}
|
|
ErrAlreadyExist = errorForUser{409, "resource already exists"} // 409 Conflict
|
|
ErrContentTooLarge = errorForUser{413, "content exceeds limit"} // 413 Request Entity Too Large
|
|
|
|
// ErrUnprocessableContent means request content is correct, but the server is unable to process the contained instructions
|
|
ErrUnprocessableContent = errorForUser{422, "unprocessable content"} // 422 Unprocessable Entity
|
|
)
|
|
|
|
type errorForUser struct {
|
|
code int // implies HTTP status code
|
|
msg string
|
|
}
|
|
|
|
func (w errorForUser) Error() string {
|
|
return w.msg
|
|
}
|
|
|
|
// TODO: introduce ErrorUnwrapForHTML in the future for web error rendering
|
|
func ErrorUnwrapForUser(err error, locale ...ErrorLocaleTranslator) (msg string, code int) {
|
|
e, ok := errors.AsType[errorForUser](err)
|
|
if ok {
|
|
msg, code = err.Error(), e.code
|
|
}
|
|
if len(locale) > 0 {
|
|
if errTr := errorAsTranslatable(err); errTr != nil {
|
|
msg = locale[0].TrString(errTr.trKey, errTr.trArgs...)
|
|
code = IfZero(code, 400)
|
|
}
|
|
}
|
|
return msg, code
|
|
}
|
|
|
|
// errorWrapper provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message
|
|
type errorWrapper struct {
|
|
msg string
|
|
err error
|
|
}
|
|
|
|
func (w errorWrapper) Error() string {
|
|
return w.msg
|
|
}
|
|
|
|
func (w errorWrapper) Unwrap() error {
|
|
return w.err
|
|
}
|
|
|
|
// ErrorWrap returns an error that formats as the given text but unwraps as the provided error
|
|
// The message should be safe (no sensitive information) to be shown to end users
|
|
func ErrorWrap(unwrap error, message string, args ...any) error {
|
|
if len(args) == 0 {
|
|
return errorWrapper{msg: message, err: unwrap}
|
|
}
|
|
return errorWrapper{msg: fmt.Sprintf(message, args...), err: unwrap}
|
|
}
|
|
|
|
// NewInvalidArgumentErrorf returns an error that formats as the given text but unwraps as an ErrInvalidArgument
|
|
func NewInvalidArgumentErrorf(message string, args ...any) error {
|
|
return ErrorWrap(ErrInvalidArgument, message, args...)
|
|
}
|
|
|
|
// NewPermissionDeniedErrorf returns an error that formats as the given text but unwraps as an ErrPermissionDenied
|
|
func NewPermissionDeniedErrorf(message string, args ...any) error {
|
|
return ErrorWrap(ErrPermissionDenied, message, args...)
|
|
}
|
|
|
|
// NewAlreadyExistErrorf returns an error that formats as the given text but unwraps as an ErrAlreadyExist
|
|
func NewAlreadyExistErrorf(message string, args ...any) error {
|
|
return ErrorWrap(ErrAlreadyExist, message, args...)
|
|
}
|
|
|
|
// NewNotExistErrorf returns an error that formats as the given text but unwraps as an ErrNotExist
|
|
func NewNotExistErrorf(message string, args ...any) error {
|
|
return ErrorWrap(ErrNotExist, message, args...)
|
|
}
|
|
|
|
type errorTranslatableWrapper struct {
|
|
err error
|
|
trKey string
|
|
trArgs []any
|
|
}
|
|
|
|
type ErrorLocaleTranslator interface {
|
|
Tr(key string, args ...any) template.HTML
|
|
TrString(key string, args ...any) string
|
|
}
|
|
|
|
func (w *errorTranslatableWrapper) Error() string { return w.err.Error() }
|
|
|
|
func (w *errorTranslatableWrapper) Unwrap() error { return w.err }
|
|
|
|
func (w *errorTranslatableWrapper) Translate(t ErrorLocaleTranslator) template.HTML {
|
|
return t.Tr(w.trKey, w.trArgs...)
|
|
}
|
|
|
|
func ErrorWrapTranslatable(err error, trKey string, trArgs ...any) ErrorTranslatable {
|
|
return &errorTranslatableWrapper{err: err, trKey: trKey, trArgs: trArgs}
|
|
}
|
|
|
|
func errorAsTranslatable(err error) *errorTranslatableWrapper {
|
|
if e, ok := errors.AsType[*errorTranslatableWrapper](err); ok {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TODO: refactor ErrorAsTranslatable in the future, use ErrorUnwrapForUser or ErrorUnwrapForHTML as much as possible
|
|
func ErrorAsTranslatable(err error) ErrorTranslatable {
|
|
e := errorAsTranslatable(err)
|
|
if e != nil { // avoid golang nil interface wrapper
|
|
return e
|
|
}
|
|
return nil
|
|
}
|