2022-10-18 06:50:37 +01:00
// Copyright 2022 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2022-10-18 06:50:37 +01:00
package util
import (
"errors"
2022-12-31 12:49:37 +01:00
"fmt"
2022-10-18 06:50:37 +01:00
)
// Common Errors forming the base of our error system
//
2025-02-18 04:41:03 +08:00
// Many Errors returned by Gitea can be tested against these errors using "errors.Is".
2022-10-18 06:50:37 +01:00
var (
2025-02-18 04:41:03 +08:00
ErrInvalidArgument = errors . New ( "invalid argument" ) // also implies HTTP 400
ErrPermissionDenied = errors . New ( "permission denied" ) // also implies HTTP 403
ErrNotExist = errors . New ( "resource does not exist" ) // also implies HTTP 404
ErrAlreadyExist = errors . New ( "resource already exists" ) // also implies HTTP 409
2025-07-01 00:55:36 +02:00
// ErrUnprocessableContent implies HTTP 422, the syntax of the request content is correct,
// but the server is unable to process the contained instructions
2025-02-18 04:41:03 +08:00
ErrUnprocessableContent = errors . New ( "unprocessable content" )
2022-10-18 06:50:37 +01:00
)
2025-03-03 13:36:10 +08:00
// errorWrapper provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message
2022-10-18 06:50:37 +01:00
// Especially useful for "untyped" errors created with "errors.New(…)" that can be classified as 'invalid argument', 'permission denied', 'exists already', or 'does not exist'
2025-03-03 13:36:10 +08:00
type errorWrapper struct {
2022-10-18 06:50:37 +01:00
Message string
Err error
}
// Error returns the message
2025-03-03 13:36:10 +08:00
func ( w errorWrapper ) Error ( ) string {
2022-10-18 06:50:37 +01:00
return w . Message
}
// Unwrap returns the underlying error
2025-03-03 13:36:10 +08:00
func ( w errorWrapper ) Unwrap ( ) error {
2022-10-18 06:50:37 +01:00
return w . Err
}
2022-12-31 12:49:37 +01:00
2025-03-03 13:36:10 +08:00
type LocaleWrapper struct {
2025-02-11 03:05:42 +08:00
err error
TrKey string
TrArgs [ ] any
}
// Error returns the message
2025-03-03 13:36:10 +08:00
func ( w LocaleWrapper ) Error ( ) string {
2025-02-11 03:05:42 +08:00
return w . err . Error ( )
}
// Unwrap returns the underlying error
2025-03-03 13:36:10 +08:00
func ( w LocaleWrapper ) Unwrap ( ) error {
2025-02-11 03:05:42 +08:00
return w . err
}
2025-03-03 13:36:10 +08:00
// ErrorWrap returns an error that formats as the given text but unwraps as the provided error
func ErrorWrap ( unwrap error , message string , args ... any ) error {
2022-12-31 12:49:37 +01:00
if len ( args ) == 0 {
2025-03-03 13:36:10 +08:00
return errorWrapper { Message : message , Err : unwrap }
2022-12-31 12:49:37 +01:00
}
2025-03-03 13:36:10 +08:00
return errorWrapper { Message : fmt . Sprintf ( message , args ... ) , Err : unwrap }
2022-12-31 12:49:37 +01:00
}
// NewInvalidArgumentErrorf returns an error that formats as the given text but unwraps as an ErrInvalidArgument
2023-07-04 20:36:08 +02:00
func NewInvalidArgumentErrorf ( message string , args ... any ) error {
2025-03-03 13:36:10 +08:00
return ErrorWrap ( ErrInvalidArgument , message , args ... )
2022-12-31 12:49:37 +01:00
}
// NewPermissionDeniedErrorf returns an error that formats as the given text but unwraps as an ErrPermissionDenied
2023-07-04 20:36:08 +02:00
func NewPermissionDeniedErrorf ( message string , args ... any ) error {
2025-03-03 13:36:10 +08:00
return ErrorWrap ( ErrPermissionDenied , message , args ... )
2022-12-31 12:49:37 +01:00
}
// NewAlreadyExistErrorf returns an error that formats as the given text but unwraps as an ErrAlreadyExist
2023-07-04 20:36:08 +02:00
func NewAlreadyExistErrorf ( message string , args ... any ) error {
2025-03-03 13:36:10 +08:00
return ErrorWrap ( ErrAlreadyExist , message , args ... )
2022-12-31 12:49:37 +01:00
}
// NewNotExistErrorf returns an error that formats as the given text but unwraps as an ErrNotExist
2023-07-04 20:36:08 +02:00
func NewNotExistErrorf ( message string , args ... any ) error {
2025-03-03 13:36:10 +08:00
return ErrorWrap ( ErrNotExist , message , args ... )
2022-12-31 12:49:37 +01:00
}
2025-02-11 03:05:42 +08:00
2025-03-03 13:36:10 +08:00
// ErrorWrapLocale wraps an err with a translation key and arguments
func ErrorWrapLocale ( err error , trKey string , trArgs ... any ) error {
return LocaleWrapper { err : err , TrKey : trKey , TrArgs : trArgs }
2025-02-11 03:05:42 +08:00
}
2025-03-03 13:36:10 +08:00
func ErrorAsLocale ( err error ) * LocaleWrapper {
var e LocaleWrapper
2025-02-11 03:05:42 +08:00
if errors . As ( err , & e ) {
return & e
}
return nil
}