eoe

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2025 License: MIT Imports: 3 Imported by: 5

README

eoe

Provide a simple function to exit the program with an error message on errors in Go.

Go Reference Go Report Card CI CodeQL

Installation

go get -u github.com/thombashi/eoe

Usage

package main

import (
    "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"))
}

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL