# Context Matters: Advanced Error Handling Techniques in Go

## Metadata
- Author: [[Alexander Demin]]
- Full Title: Context Matters: Advanced Error Handling Techniques in Go
- Category: #articles
- Summary: This article explores advanced error handling in Go, focusing on contextual errors that provide rich details for debugging by embedding key-value metadata into errors. The article discusses the limitations of traditional error handling in Go and introduces an approach using structured contextual errors to address those limitations. It provides examples and code snippets to demonstrate how to implement and utilize contextual errors effectively. The benefits of contextual error handling are highlighted, including improved debugging, organized logging, and post-mortem analysis. The article concludes by offering some rules of thumb for using contextual errors in Go applications.
- URL: https://medium.com/@oberonus/context-matters-advanced-error-handling-techniques-in-go-b470f763c7ec
## Highlights
- The absence of context can create significant challenges for developers and QA specialists as they attempt to investigate and understand the underlying problems in an application. ([View Highlight](https://read.readwise.io/read/01k051pr2a9ra42qkj10weay0y))
- When using traditional Go error handling, providing useful context is pretty difficult. Errors are just simple text messages without any structured information. An error is typically created in a deeply nested service (e.g. a database repository) and bubbles up through several layers before finally being handled. By the time it reaches the surface, the original context — the ‘why’ and ‘how’ of the error — is often lost. ([View Highlight](https://read.readwise.io/read/01k05316derx9j8xr0102geh61))
- However, this approach leads to its own set of problems. As an error passes through various layers, accumulating additional context, the final error string becomes increasingly complex and convoluted:
failed to process request, requestID=491002, userID=4881: doWork called,
failed to do work, entityID=482003, userID=4881: failed to fetch entity
with ID=482003 from the database: database connection lost ([View Highlight](https://read.readwise.io/read/01k0531wn22t8tjqw0t3ansdhg))
- These verbose error strings are **hard to parse** by logging analysis tools. Extracting specific pieces of information from these long strings can be problematic and error-prone. ([View Highlight](https://read.readwise.io/read/01k0532kvqw43jkk33wq622d8j))
- As errors bubble up through the application layers, the same **information often gets repeated**. For example, user IDs or entity IDs might be appended at multiple levels, leading to redundancy in the error message. ([View Highlight](https://read.readwise.io/read/01k0532gv87sbabna245c58rm7))
- in the absence of any context, an HTTP handler would not be able to log any specific details of the entity that caused the error. This information is only available in the domain layer and is not transferred to higher layers. ([View Highlight](https://read.readwise.io/read/01k053afbwakazdg7dnzeaed62))
- Structured contextual errors differ from traditional error wrapping by including additional metadata in a structured format, typically key-value pairs. This metadata can be represented by various data relevant to the error, such as entity IDs, user details, input parameters, or internal state information. By embedding this information directly into the error, we create a self-contained error object that carries its context along with it. ([View Highlight](https://read.readwise.io/read/01k053azhhy4xfsx76w5cwcvdy))