Documentation
¶
Overview ¶
Package eoe provides a function to exit the program with an error log when a passing error is not nil.
The ExitOnError function is useful when you want to exit the program when an error argument is not nil. The function prints an error message with a slog.Logger and exits the program with the specified exit code. You can customize the behavior of the function by using the ExitOnErrorParams struct:
- error message
- exit code
- slog.Logger for logging
- log level for logging
- context.Context
- function to call when exiting
Index ¶
- func ExitOnError(err error, params *ExitOnErrorParams)
- type ExitOnErrorParams
- func (params *ExitOnErrorParams) WithContext(ctx context.Context) *ExitOnErrorParams
- func (params *ExitOnErrorParams) WithExitCode(code int) *ExitOnErrorParams
- func (params *ExitOnErrorParams) WithExitFunc(exitFunc func(params *ExitOnErrorParams)) *ExitOnErrorParams
- func (params *ExitOnErrorParams) WithLogLevel(level slog.Level) *ExitOnErrorParams
- func (params *ExitOnErrorParams) WithLogger(logger *slog.Logger) *ExitOnErrorParams
- func (params *ExitOnErrorParams) WithMessage(msg string) *ExitOnErrorParams
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExitOnError ¶
func ExitOnError(err error, params *ExitOnErrorParams)
ExitOnError prints an error message and exits the program with the specified exit code.
Example ¶
package main import ( "errors" "log/slog" eoe "github.com/thombashi/eoe" ) func successFunc() error { return nil } func errrorFunc() error { return errors.New("an error occurred") } func main() { var err error logger := slog.Default() params := eoe.NewParams().WithLogger(logger) // should not exit if the error is nil err = successFunc() eoe.ExitOnError(err, params.WithMessage("should not exit")) // should exit if the error is not nil err = errrorFunc() eoe.ExitOnError(err, params.WithMessage("should exit with an error message")) }
Output:
Types ¶
type ExitOnErrorParams ¶
type ExitOnErrorParams struct { // Message is an error message to print. Message string // ExitCode is an exit code to use when exiting the program. // Default is 1. ExitCode int // Logger is a logger to use for logging. // Default is slog.Default(). Logger *slog.Logger // LogLevel is a log level to use for logging. // Default is slog.LevelError. LogLevel slog.Level // Context is a context to use for logging. // Default is context.Background(). Context context.Context // ExitFunc is a function to use for exiting the program. ExitFunc func(params *ExitOnErrorParams) }
ExitOnErrorParams is a struct for ExitOnError function.
func NewParams ¶
func NewParams() *ExitOnErrorParams
NewParams creates a new ExitOnErrorParams instance.
func (*ExitOnErrorParams) WithContext ¶
func (params *ExitOnErrorParams) WithContext(ctx context.Context) *ExitOnErrorParams
WithContext sets the context.
func (*ExitOnErrorParams) WithExitCode ¶
func (params *ExitOnErrorParams) WithExitCode(code int) *ExitOnErrorParams
WithExitCode sets the exit code.
func (*ExitOnErrorParams) WithExitFunc ¶
func (params *ExitOnErrorParams) WithExitFunc(exitFunc func(params *ExitOnErrorParams)) *ExitOnErrorParams
func (*ExitOnErrorParams) WithLogLevel ¶
func (params *ExitOnErrorParams) WithLogLevel(level slog.Level) *ExitOnErrorParams
WithLogLevel sets the log level.
func (*ExitOnErrorParams) WithLogger ¶
func (params *ExitOnErrorParams) WithLogger(logger *slog.Logger) *ExitOnErrorParams
WithLogger sets the logger.
func (*ExitOnErrorParams) WithMessage ¶
func (params *ExitOnErrorParams) WithMessage(msg string) *ExitOnErrorParams
WithMessage sets the error message.