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
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:
| Parameter | Type | Required | Description |
|---|---|---|---|
| msg | string | Yes | Error message content |
Returns: None (process terminates)
Example:
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:
| Parameter | Type | Required | Description |
|---|---|---|---|
| msg | string | Yes | Error context description |
| err | error | Yes | Original error object |
Returns: None (process terminates)
Example:
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:
| Parameter | Type | Required | Description |
|---|---|---|---|
| msg | string | Yes | Error message content |
| code | int | Yes | Process exit code, use the exit code constants defined in the module |
Returns: None (process terminates)
Example:
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
| Constant | Value | Description |
|---|---|---|
ExitOk | 0 | Normal exit |
ExitAppNameIsEmpty | -1 | Application name is empty |
ExitCreateUserHomeConfDir | -2 | Failed to create user configuration directory (~/.config/{appName}) |
ExitCreateWorkDir | -3 | Failed to create work directory |
ExitCreateDataDir | -4 | Failed to create data directory |
ExitCreateLogDir | -5 | Failed to create log directory |
ExitCreateTmpDir | -6 | Failed to create temporary directory |
ExitPackageFile | -7 | package.json file does not exist |
ExitCreateUserHomeAppDir | -8 | Failed to create user application directory (~/.{appName}) |
ExitCreateDBDir | -9 | Failed to create database directory |
Config Phase Exit Codes
| Constant | Value | Description |
|---|---|---|
ExitConfigFile | -11 | Configuration file error |
ExitConfigFileFS | -12 | Configuration filesystem error |
ExitConfigFileNotExist | -13 | Configuration file does not exist |
ExitConfigStaticErr | -14 | Static resource configuration error |
ExitConfigLogErr | -15 | Log configuration retrieval error |
ExitConfigCoreLogErr | -16 | Core log configuration retrieval error |
ExitConfigHttpErr | -17 | HTTP configuration retrieval error |
Log Phase Exit Codes
| Constant | Value | Description |
|---|---|---|
ExitConfigGenerate | -21 | Log configuration generation failed |
HTTP Phase Exit Codes
| Constant | Value | Description |
|---|---|---|
ExitPortIsOccupied | -31 | Service port is occupied |
ExitHttpStartupErr | -32 | HTTP service startup failed |
ExitListenPortErr | -33 | Port listening failed |
Example:
// 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