forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/error.go

380 lines
6.5 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"errors"
"math/rand"
)
// Error will return a random generic error
2024-04-26 19:30:35 +00:00
func Error() error {
2024-04-26 19:30:35 +00:00
return err(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// Error will return a random generic error
2024-04-26 19:30:35 +00:00
func (f *Faker) Error() error {
2024-04-26 19:30:35 +00:00
return err(f.Rand)
2024-04-26 19:30:35 +00:00
}
func err(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "generic"})))
2024-04-26 19:30:35 +00:00
}
// ErrorObject will return a random error object word
2024-04-26 19:30:35 +00:00
func ErrorObject() error {
2024-04-26 19:30:35 +00:00
return errorObject(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorObject will return a random error object word
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorObject() error {
2024-04-26 19:30:35 +00:00
return errorObject(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorObject(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "object"})))
2024-04-26 19:30:35 +00:00
}
// ErrorDatabase will return a random database error
2024-04-26 19:30:35 +00:00
func ErrorDatabase() error {
2024-04-26 19:30:35 +00:00
return errorDatabase(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorDatabase will return a random database error
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorDatabase() error {
2024-04-26 19:30:35 +00:00
return errorDatabase(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorDatabase(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "database"})))
2024-04-26 19:30:35 +00:00
}
// ErrorGRPC will return a random gRPC error
2024-04-26 19:30:35 +00:00
func ErrorGRPC() error {
2024-04-26 19:30:35 +00:00
return errorGRPC(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorGRPC will return a random gRPC error
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorGRPC() error {
2024-04-26 19:30:35 +00:00
return errorGRPC(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorGRPC(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "grpc"})))
2024-04-26 19:30:35 +00:00
}
// ErrorHTTP will return a random HTTP error
2024-04-26 19:30:35 +00:00
func ErrorHTTP() error {
2024-04-26 19:30:35 +00:00
return errorHTTP(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorHTTP will return a random HTTP error
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorHTTP() error {
2024-04-26 19:30:35 +00:00
return errorHTTP(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorHTTP(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "http"})))
2024-04-26 19:30:35 +00:00
}
// ErrorHTTPClient will return a random HTTP client error response (400-418)
2024-04-26 19:30:35 +00:00
func ErrorHTTPClient() error {
2024-04-26 19:30:35 +00:00
return errorHTTPClient(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorHTTPClient will return a random HTTP client error response (400-418)
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorHTTPClient() error {
2024-04-26 19:30:35 +00:00
return errorHTTPClient(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorHTTPClient(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "http_client"})))
2024-04-26 19:30:35 +00:00
}
// ErrorHTTPServer will return a random HTTP server error response (500-511)
2024-04-26 19:30:35 +00:00
func ErrorHTTPServer() error {
2024-04-26 19:30:35 +00:00
return errorHTTPServer(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorHTTPServer will return a random HTTP server error response (500-511)
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorHTTPServer() error {
2024-04-26 19:30:35 +00:00
return errorHTTPServer(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorHTTPServer(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "http_server"})))
2024-04-26 19:30:35 +00:00
}
// ErrorRuntime will return a random runtime error
2024-04-26 19:30:35 +00:00
func ErrorRuntime() error {
2024-04-26 19:30:35 +00:00
return errorRuntime(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorRuntime will return a random runtime error
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorRuntime() error {
2024-04-26 19:30:35 +00:00
return errorRuntime(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorRuntime(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "runtime"})))
2024-04-26 19:30:35 +00:00
}
// ErrorValidation will return a random validation error
2024-04-26 19:30:35 +00:00
func ErrorValidation() error {
2024-04-26 19:30:35 +00:00
return errorValidation(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// ErrorValidation will return a random validation error
2024-04-26 19:30:35 +00:00
func (f *Faker) ErrorValidation() error {
2024-04-26 19:30:35 +00:00
return errorValidation(f.Rand)
2024-04-26 19:30:35 +00:00
}
func errorValidation(r *rand.Rand) error {
2024-04-26 19:30:35 +00:00
return errors.New(generate(r, getRandValue(r, []string{"error", "validation"})))
2024-04-26 19:30:35 +00:00
}
func addErrorLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("error", Info{
Display: "Error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "Message displayed by a computer or software when a problem or mistake is encountered",
Example: "syntax error",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return err(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errorobject", Info{
Display: "Error object word",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "Various categories conveying details about encountered errors",
Example: "protocol",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorObject(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errordatabase", Info{
Display: "Database error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "A problem or issue encountered while accessing or managing a database",
Example: "sql error",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorDatabase(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errorgrpc", Info{
Display: "gRPC error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "Communication failure in the high-performance, open-source universal RPC framework",
Example: "client protocol error",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorGRPC(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errorhttp", Info{
Display: "HTTP error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "A problem with a web http request",
Example: "invalid method",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorHTTP(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errorhttpclient", Info{
Display: "HTTP client error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "Failure or issue occurring within a client software that sends requests to web servers",
Example: "request timeout",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorHTTPClient(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errorhttpserver", Info{
Display: "HTTP server error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "Failure or issue occurring within a server software that recieves requests from clients",
Example: "internal server error",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorHTTPServer(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errorruntime", Info{
Display: "Runtime error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "Malfunction occuring during program execution, often causing abrupt termination or unexpected behavior",
Example: "address out of bounds",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorRuntime(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("errorvalidation", Info{
Display: "Validation error",
Category: "error",
2024-04-26 19:30:35 +00:00
Description: "Occurs when input data fails to meet required criteria or format specifications",
Example: "missing required field",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return errorValidation(r), nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
}