Go to content
Web development

Which Go Errors Should You Retry?

When a Go service or worker cannot reach another service it depends on, it must decide whether to try the call again. Retry only failures that the other service or your code identifies as temporary. Stop when the request needs different input, credentials, or permissions, or when someone must make a different business decision.

A temporary failure is a timeout, a brief network outage, a rate limit, or a busy server response. These problems can clear while the request stays the same. A permanent failure, such as invalid input or a rejected business rule, will happen again if the service repeats the same request. Go’s errors.Is and errors.As let your code identify errors by a shared sentinel value or error type instead of guessing from the message.

Separate temporary failures from rejected requests

Use the dependency’s documentation to decide which errors to retry. For example, Azure’s Go Blob Storage client retries HTTP 408, 429, 500, 502, 503, and 504 by default. Those codes apply to that client’s policy; another API may have a different policy.

Your service should make the same distinction. Try again after a temporary database or network failure. A validation error means the caller sent input the service cannot accept. A business-rule error means the request breaks a rule, such as a rule against using a closed account. If the caller sends either request unchanged, the service will reject it again. Return the error to the caller or send the job for correction instead of retrying it.

Change the credentials or permissions before retrying an authentication or authorization failure. The Mercado Pago Go SDK guidance recommends correcting credentials or permissions after authentication or forbidden responses, and correcting the request after validation or bad-request responses. For rate limits, follow RetryAfter when the response includes it and reduce the number of requests sent at the same time instead of retrying immediately.

Classify errors by identity or type

Go error messages are written for people, and their wording can change. Do not decide whether to retry by searching the message for words such as “timeout” or digits such as “503.” Instead, check stable signals in the error: a shared sentinel error, an error type that carries a status code, or an error interface the dependency documents.

errors.Is(err, target) checks whether err or an error wrapped by err matches a sentinel value, which is a shared error value used for comparison. errors.As(err, &target) checks whether err or an error it wraps has a particular type. If it finds one, it assigns that error to target. The Go error-handling discussion describes these as ways to handle errors by meaning rather than by message text. For more detail on the Go calls, see using errors.Is and errors.As.

Your service can define errors for invalid input and rejected business rules. Check for these errors before checking whether a dependency error is retryable:

var (
    ErrInvalidInput = errors.New("invalid input")
    ErrRuleRejected = errors.New("business rule rejected request")
)

type APIError struct {
    StatusCode int
}

func (e *APIError) Error() string {
    return fmt.Sprintf("dependency returned HTTP %d", e.StatusCode)
}

func shouldRetry(err error) bool {
    if err == nil {
        return false
    }

    // These failures need a corrected request or a different decision.
    if errors.Is(err, ErrInvalidInput) || errors.Is(err, ErrRuleRejected) {
        return false
    }

    if errors.Is(err, context.Canceled) {
        return false
    }

    // A deadline is a timeout signal. For writes, first determine whether
    // the dependency completed the request before the response was lost.
    if errors.Is(err, context.DeadlineExceeded) {
        return true
    }

    var apiErr *APIError
    if errors.As(err, &apiErr) {
        switch apiErr.StatusCode {
        case 408, 429, 500, 502, 503, 504:
            return true
        default:
            return false
        }
    }

    // Unknown errors do not become retryable just because they are non-nil.
    return false
}

The status codes in this example match Azure Blob Storage’s documented defaults. Check your dependency’s documentation when you set your own policy. The Azure Go retry guidance also lets a ShouldRetry function inspect a response or error. Azure’s policy checks the context and errors it does not retry before calling that function. AgenticGoKit’s retry policy example also checks errors first and treats unknown errors as permanent by default.

Check what happened before repeating a write

A timeout or network failure means the caller did not receive a successful response. It does not tell you whether the server made the requested change. If a worker times out while creating an order or charging an account, the dependency might have completed the write before the connection failed. Sending the request again can create a duplicate order or charge.

When you do not know whether a write succeeded, check the resource or operation status before sending the request again. The Mercado Pago guidance recommends checking the resource after context or network failures when the result is uncertain. A Google Cloud Go Firestore issue documents one Cloud Run case where callers received ECONNRESET and ECONNREFUSED. It suggests using errors.Is to check for syscall.ECONNRESET. This report covers a specific case, so classify network errors based on the behavior documented for your client.

More in Web development Back to top ↑