Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

Member-only story

Error Handling Style Guide in Golang

Jerry An
Level Up Coding
Published in
2 min readAug 18, 2022

--

Error handling is a fundamental technique in Golang. This post will discuss the error handling code style guide in Golang.

Photo by Mark Fletcher-Brown on Unsplash

Declaring Errors

The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error.

type error interface {
Error() string
}

There are few options for declaring errors.

error.New

The functionNew returns an error that formats as the given text. Each call to it returns a distinct error value even if the text is identical.

func bar() error {
return errors.New("error: an error occurred")
}

fmt.Errorf

The function Errorf formats according to a format specifier and returns the string as a value that satisfies error.

func foo() error {
return fmt.Errorf("foo: %s", "an error occurred")
}

If the format specifier includes a %w verb with an error operand, the returned error will implement an Unwrap method returning the operand.

func foo() error {
return fmt.Errorf("foo: %w", errors.New("an error occurred"))
}

Custom error type

Sometimes, we need to create a custom error handling errors.

type NotFoundError struct {
File string
}
func (e *NotFoundError) Error() string {
return fmt.Sprintf("file %q not found", e.File)
}

Style guide

Consider the following before picking the best error option for your use case.

  • Does the caller need to match the error so they can handle it? If yes, we must support the errors.Is or errors.As functions by declaring a top-level error variable or a custom type.
  • Is the error message a static string, or is it a dynamic string that requires contextual information? For the former, we can use errors.New, but for the latter, we must use fmt.Errorf or a custom error type.
  • Are we propagating a new error returned by a downstream function? If so, use error wrapping.

--

--

No responses yet

Write a response