tezvyn:

Design a custom Go error type with context, Is, As, and Unwrap

AI-drafted, machine-checkedSource: go.devadvanced

This tests Go 1.13 error wrapping and Unwrap conventions. Answer: struct with Err and context fields; implement Error and Unwrap; note errors.Is and errors.As walk the chain. Red flag: stringifying the error via fmt.Errorf %v, which severs unwrapping.

WHAT THIS TESTS: This tests your understanding of Go 1.13 error wrapping conventions and the Unwrap method. The interviewer wants to know if you grasp that errors.Is and errors.As rely on Unwrap to traverse chains of wrapped errors, and whether you can design a struct that preserves the underlying error value while adding structured contextual information.

A GOOD ANSWER COVERS: First, define a struct type with an Err error field plus additional fields for context such as a query string, timestamp, filename, or server address. Second, implement the Error method to return a formatted message that includes both the new context and the underlying error text. Third, implement an Unwrap method that returns the Err field so that standard library functions can inspect the chain. Fourth, note that fmt.Errorf supports a new formatting verb in Go 1.13 that wraps errors automatically, but for a custom type you must implement Unwrap yourself. Fifth, explain that errors.Is compares an error to a sentinel by walking the chain via Unwrap, and errors.As attempts type assertions at each layer until it finds a match.

COMMON WRONG ANSWERS: A critical mistake is constructing a wrapper with fmt.Errorf using percent v instead of the new wrapping verb, because that discards everything from the original error except its text and breaks the chain. Another red flag is defining a struct that stores an underlying error but omitting the Unwrap method, which silently prevents errors.Is and errors.As from working. Some candidates also suggest comparing wrapped errors with double equals, which fails because the wrapper is a different value than the original.

LIKELY FOLLOW-UPS: The interviewer may ask how errors.Is differs from direct equality comparison, or when to prefer a custom error type over fmt.Errorf with the new verb. They might ask how standard library types like os.PathError follow the same pattern, or how you would format the error verbosely without changing the Error method output. You could also be asked how to handle cases where an error wraps multiple underlying errors.

ONE CONCRETE EXAMPLE: Consider a QueryError struct with Query string and Err error fields, matching the pattern described in the Go 1.13 release. When a database query fails, you return a QueryError that contains the original error and the SQL string. Because QueryError implements Unwrap returning Err, a caller can use errors.Is to recognize the original sentinel even through the wrapper, or use errors.As to extract the QueryError and inspect the Query field directly.

Read the original → go.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.