2026-03-11 13:02:37 +00:00
---
title: Errors
2026-03-21 10:05:04 +00:00
description: Structured errors, logging helpers, and panic recovery.
2026-03-11 13:02:37 +00:00
---
# Errors
2026-03-21 10:05:04 +00:00
CoreGO treats failures as structured operational data.
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
Repository convention: use `E()` instead of `fmt.Errorf` for framework and service errors.
## `Err`
The structured error type is:
2026-03-11 13:02:37 +00:00
```go
2026-03-21 10:05:04 +00:00
type Err struct {
Operation string
Message string
Cause error
Code string
2026-03-11 13:02:37 +00:00
}
```
2026-03-21 10:05:04 +00:00
## Create Errors
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
### `E`
2026-03-11 13:02:37 +00:00
```go
2026-03-21 10:05:04 +00:00
err := core.E("workspace.Load", "failed to read workspace manifest", cause)
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
### `Wrap`
2026-03-11 13:02:37 +00:00
```go
2026-03-21 10:05:04 +00:00
err := core.Wrap(cause, "workspace.Load", "manifest parse failed")
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
### `WrapCode`
```go
err := core.WrapCode(cause, "WORKSPACE_INVALID", "workspace.Load", "manifest parse failed")
```
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
### `NewCode`
2026-03-11 13:02:37 +00:00
```go
2026-03-21 10:05:04 +00:00
err := core.NewCode("NOT_FOUND", "workspace not found")
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
## Inspect Errors
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
```go
op := core.Operation(err)
code := core.ErrorCode(err)
msg := core.ErrorMessage(err)
root := core.Root(err)
stack := core.StackTrace(err)
pretty := core.FormatStackTrace(err)
```
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
These helpers keep the operational chain visible without extra type assertions.
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
## Join and Standard Wrappers
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
```go
combined := core.ErrorJoin(err1, err2)
same := core.Is(combined, err1)
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
`core.As` and `core.NewError` mirror the standard library for convenience.
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
## Log-and-Return Helpers
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
`Core` exposes two convenience wrappers:
2026-03-11 13:02:37 +00:00
```go
2026-03-21 10:05:04 +00:00
r1 := c.LogError(err, "workspace.Load", "workspace load failed")
r2 := c.LogWarn(err, "workspace.Load", "workspace load degraded")
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
These log through the default logger and return `core.Result` .
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
You can also use the underlying `ErrorLog` directly:
2026-03-11 13:02:37 +00:00
```go
2026-03-21 10:05:04 +00:00
r := c.Log().Error(err, "workspace.Load", "workspace load failed")
```
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
`Must` logs and then panics when the error is non-nil:
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
```go
c.Must(err, "workspace.Load", "workspace load failed")
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
## Panic Recovery
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
`ErrorPanic` handles process-safe panic capture.
```go
defer c.Error().Recover()
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
Run background work with recovery:
```go
c.Error().SafeGo(func() {
panic("captured")
})
2026-03-11 13:02:37 +00:00
```
2026-03-21 10:05:04 +00:00
If `ErrorPanic` has a configured crash file path, it appends JSON crash reports and `Reports(n)` reads them back.
That crash file path is currently internal state on `ErrorPanic` , not a public constructor option on `Core.New()` .
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
## Logging and Error Context
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
The logging subsystem automatically extracts `op` and logical stack information from structured errors when those values are present in the key-value list.
2026-03-11 13:02:37 +00:00
2026-03-21 10:05:04 +00:00
That makes errors created with `E` , `Wrap` , or `WrapCode` much easier to follow in logs.