Skip to content

Introduction

eerror is the error handling module of ee-go, providing three error throwing methods: simple error, error with original error wrapping, and error with exit code. All errors include stack trace information (using the pkg/errors library) and output to stderr before terminating the process. The module also defines exit code constants used uniformly across the framework to identify different types of abnormal exit reasons.

Import

go
import "github.com/wallace5303/ee-go/eerror"

API

Throw(msg)

Description: Print an error message with stack trace, then terminate the process with exit code 0. Suitable for scenarios that don't need to differentiate error types.

Parameters:

ParameterTypeRequiredDescription
msgstringYesError message content

Returns: None (process terminates)

Example:

go
if dbName == "" {
    eerror.Throw("Database name is required")
}

See also: ThrowWrap(), ThrowWithCode()


ThrowWrap(msg, err)

Description: Wrap the original error err as a new error with stack trace, use msg as the context description, then terminate the process with exit code 0. Suitable for scenarios that need to preserve the original error chain.

Parameters:

ParameterTypeRequiredDescription
msgstringYesError context description
errerrorYesOriginal error object

Returns: None (process terminates)

Example:

go
file, err := os.Open("config.json")
if err != nil {
    eerror.ThrowWrap("Failed to open config file", err)
}

See also: Throw(), ThrowWithCode()


ThrowWithCode(msg, code)

Description: Print an error message with stack trace, then terminate the process with the specified exit code. Suitable for scenarios that need to differentiate error types via exit code, and is the most commonly used error handling method within the framework.

Parameters:

ParameterTypeRequiredDescription
msgstringYesError message content
codeintYesProcess exit code, use the exit code constants defined in the module

Returns: None (process terminates)

Example:

go
if !ehelper.FileIsExist(pkgPath) {
    eerror.ThrowWithCode("The package.json does not exist!", eerror.ExitPackageFile)
}

if appName == "" {
    eerror.ThrowWithCode("The app name is required!", eerror.ExitAppNameIsEmpty)
}

See also: Exit code constants


Exit Code Constants

Exit codes are used to identify different types of abnormal exit reasons. All constants are defined in the eerror package and categorized as follows:

Boot Phase Exit Codes

ConstantValueDescription
ExitOk0Normal exit
ExitAppNameIsEmpty-1Application name is empty
ExitCreateUserHomeConfDir-2Failed to create user configuration directory (~/.config/{appName})
ExitCreateWorkDir-3Failed to create work directory
ExitCreateDataDir-4Failed to create data directory
ExitCreateLogDir-5Failed to create log directory
ExitCreateTmpDir-6Failed to create temporary directory
ExitPackageFile-7package.json file does not exist
ExitCreateUserHomeAppDir-8Failed to create user application directory (~/.{appName})
ExitCreateDBDir-9Failed to create database directory

Config Phase Exit Codes

ConstantValueDescription
ExitConfigFile-11Configuration file error
ExitConfigFileFS-12Configuration filesystem error
ExitConfigFileNotExist-13Configuration file does not exist
ExitConfigStaticErr-14Static resource configuration error
ExitConfigLogErr-15Log configuration retrieval error
ExitConfigCoreLogErr-16Core log configuration retrieval error
ExitConfigHttpErr-17HTTP configuration retrieval error

Log Phase Exit Codes

ConstantValueDescription
ExitConfigGenerate-21Log configuration generation failed

HTTP Phase Exit Codes

ConstantValueDescription
ExitPortIsOccupied-31Service port is occupied
ExitHttpStartupErr-32HTTP service startup failed
ExitListenPortErr-33Port listening failed

Example:

go
// Use exit code constants
eerror.ThrowWithCode("Port 8080 is already in use", eerror.ExitPortIsOccupied)

// In the Electron main process, determine Go process exit reason based on exit code
// exitCode -7 → package.json missing
// exitCode -31 → port conflict