Documentation ¶
Overview ¶
Package tserr provides a simple API for standardized error messages in the JSON format.
The tserr package provides easy-to-use functions to get standardized error messages for typical errors. The error messages are fomatted in the JSON format:
{"error":{"id":<int>,"code":<int>,"message":"<string>"}}
The root element is named "error". "id" is a consecutively numbered id. "code" is a relating HTTP status code. "message" contains the actual pre-defined error message.
The error message may contain verbs to be filled by arguments. The arguments for the verbs are provided as function arguments. A function may hold one argument used as one verb in the error message. A function may hold multiple arguments used for more than one verb in the error message. Multiple arguments are passed to a function as a pointer to a struct, e.g.,
err := tserr.EqualStr(&tserr.EqualStrArgs{X: "test1", Y: "test2"})
Output with fmt.Println(err):
{"error":{"id":6,"code":500,"message":"test1 does not equal test2"}}
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public License v3.0 that can be found in the LICENSE file.
All exported error functions are implemented here, with the exception of NilPtr, which exists in a separate source file. If the function has one argument it is directly provided as function argument. If the function has more than one argument, then the arguments are provided as a struct, e.g.,
err := tserr.EqualStr(&tserr.EqualStrArgs{X: "test1", Y: "test2"})
All error functions first check, if the pointer to the argument struct is nil. If it is nil, the error function returns NilPtr, e.g.,
if a == nil { return NilPtr() }
Otherwise, it returns the corresponding error message, e.g.,
return errorf(&errmsgEqualStr, a.X, a.Y)
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public License v3.0 that can be found in the LICENSE file.
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public License v3.0 that can be found in the LICENSE file.
Index ¶
- func Check(a *CheckArgs) error
- func Duplicate(F string) error
- func Empty(F string) error
- func Equal(a *EqualArgs) error
- func EqualStr(a *EqualStrArgs) error
- func Equalf(a *EqualfArgs) error
- func Forbidden(F string) error
- func Higher(a *HigherArgs) error
- func Locked(S string) error
- func Lower(a *LowerArgs) error
- func NilFailed(Op string) error
- func NilPtr() error
- func NonPrintable(F string) error
- func NotAvailable(a *NotAvailableArgs) error
- func NotEmpty(F string) error
- func NotEqual(a *NotEqualArgs) error
- func NotExistent(F string) error
- func NotNil(Op string) error
- func NotSet(F string) error
- func Op(a *OpArgs) error
- func Return(a *ReturnArgs) error
- func TypeNotMatching(a *TypeNotMatchingArgs) error
- type CheckArgs
- type EqualArgs
- type EqualStrArgs
- type EqualfArgs
- type HigherArgs
- type LowerArgs
- type NotAvailableArgs
- type NotEqualArgs
- type OpArgs
- type ReturnArgs
- type TypeNotMatchingArgs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Duplicate ¶ added in v1.11.0
Duplicate can be used if an object already exists, but is only allowed to exist once, for example, a key. F is the name of the object which already exists, for example, the name of a key
func Empty ¶
Empty can be used if a required object is empty but not allowed to be empty, for example, an input argument of type string. F is the name of the empty object, for example, filename
func Equal ¶ added in v1.4.0
Equal can be used if an integer tails to be equal to an expected value.
func EqualStr ¶ added in v1.10.0
func EqualStr(a *EqualStrArgs) error
EqualStr can be used if a string fails to be equal to an expected string.
func Equalf ¶ added in v1.7.0
func Equalf(a *EqualfArgs) error
Equalf can be used if a float value is not equal to an expected value
func Forbidden ¶ added in v1.1.0
Forbidden can be used if an operation on an object is forbidden. F is the name of the forbidden object, for example, a directory or filename
func Higher ¶ added in v1.3.0
func Higher(a *HigherArgs) error
Higher can be used if an integer fails to at least be equal or be higher than a defined lower bound.
func Locked ¶ added in v1.12.0
Locked can be used if a service is locked, for example, because it is still running. S is the name of the service which is locked
func Lower ¶ added in v1.9.0
Lower can be used if an integer fails to be lower than a defined higher bound.
func NilFailed ¶
NilFailed can be used if the function implementing an operation returns nil, but an error is expected. A default use case are Test functions. Op is the name of the operation, for example, ExistsFile
func NilPtr ¶
func NilPtr() error
NilPtr just provides the error message and does not have arguments.
func NonPrintable ¶ added in v1.8.0
NonPrintable can be used if a string is allowed to only contain printable runes, but actually contains non-printable runes. F is the name of the string allowed to only contain printable runes
func NotAvailable ¶ added in v1.6.0
func NotAvailable(a *NotAvailableArgs) error
NotAvailable can be used if a service is not available.
func NotEmpty ¶ added in v1.14.0
NotEmpty can be used if a required object is not empty but expected to be empty, for example, an output argument of type string. F is the name of the object expected to be empty, for example, filename
func NotEqual ¶ added in v1.10.0
func NotEqual(a *NotEqualArgs) error
NotEqual can be used if two variables are equal but not expected to be equal.
func NotExistent ¶
NotExistent can be used if an required object does not exist, for example, a file. F is the name of the object, for example, key name
func NotNil ¶ added in v1.15.0
NotNil can be used if the function implementing an operation does not return nil, but nil is expected. A default use case are Test functions. Op is the name of the operation
func NotSet ¶ added in v1.5.0
NotSet can be used if a required object is not set, for example, an environment variable. F is the name of the object, for example, the name of the environment variable
func Return ¶ added in v1.2.0
func Return(a *ReturnArgs) error
Return can be used if an operation returns an actual value, but another return value is expected.
func TypeNotMatching ¶
func TypeNotMatching(a *TypeNotMatchingArgs) error
TypeNotMatching can be used if the type of an object does not match the expected type
Types ¶
type CheckArgs ¶
type CheckArgs struct { // F is the name of the object causing the failed check, for example, a filename F string // Err is the error causing the failed check, for example, file is a directory Err error }
CheckArgs holds the required arguments for the error function Check
type EqualArgs ¶ added in v1.4.0
type EqualArgs struct { // Var is the name of the variable Var string // Actual is the actual value of Var Actual int64 // Want is the expected value of Var Want int64 }
EqualArgs holds the required arguments for the error function Equal
type EqualStrArgs ¶ added in v1.10.0
type EqualStrArgs struct { // Var is the name of the variable Var string // Actual is the actual value of Var Actual string // Want is the expected value of Var Want string }
EqualStrArgs holds the required arguments for the error function EqualStr
type EqualfArgs ¶ added in v1.7.0
type EqualfArgs struct { // Var is the name of the variable Var string // Actual is the actual value of Var Actual float64 // Want is the expected value of Var Want float64 }
EqualfArgs holds the required arguments for the error function Equalf
type HigherArgs ¶ added in v1.3.0
type HigherArgs struct { // Var is the name of the variable Var string // Actual is the actual of Var Actual int64 // LowerBound is the lower bound. Actual is expected to be equal or higher than Lowerbound LowerBound int64 }
HigherArgs holds the required arguments for the error function Higher
type LowerArgs ¶ added in v1.9.0
type LowerArgs struct { // Var is the name of the variable Var string // Actual is the actual value of Var Actual int64 // Want is the expected value of Var Want int64 }
LowerArgs holds the required arguments for the error function Lower
type NotAvailableArgs ¶ added in v1.6.1
type NotAvailableArgs struct { // S is the name of the service not available S string // Err is the error provided by the service Err error }
NotAvailableArgs holds the required arguments for the error function NotAvailable
type NotEqualArgs ¶ added in v1.10.0
type NotEqualArgs struct { // Name of the variable equal to Y X string // Name of the variable equal to X Y string }
NotEqualArgs holds the required arguments for the error function NotEqual
type OpArgs ¶
type OpArgs struct { // Op is the name of the failed operation, for example, WriteStr Op string // Fn is the name of the object passed to the operation, for example, a filename Fn string // Err is the error retrieved from the failed operation, for example, file does not exist Err error }
OpArgs holds the required arguments for the error function Op
type ReturnArgs ¶ added in v1.2.0
type ReturnArgs struct { // Op is the operation Op string // Actual is the actual return value returned by Op Actual string // Want is the expected return value from Op Want string }
ReturnArgs holds the required arguments for the error function Return
type TypeNotMatchingArgs ¶
type TypeNotMatchingArgs struct { // Actual is the name of the actual type of the object, for example, a file Actual string // Want is the name of the expected, wanted or required type the object should be, for example, a directory Want string }
TypeNotMatchingArgs holds the required arguments for the error function TypeNotMatching