How to check if a request was cancelled

You can check the context's error:

package main

import (
    "context"
    "fmt"
)

func main() {    
    ctx, cancel := context.WithCancel(context.Background())
    fmt.Println(ctx.Err())
    cancel()
    fmt.Println(ctx.Err())
}

Prints

<nil>
context canceled

The cleanest way to do this in Go 1.13+ is using the new errors.Is function.

// Create a context that is already canceled
ctx, cancel := context.WithCancel(context.Background())
cancel()

// Create the request with it
r, _ := http.NewRequestWithContext(ctx, "GET", "http://example.com", nil)

// Do it, it will immediately fail because the context is canceled.
_, err := http.DefaultClient.Do(r)
log.Println(err) // Get http://example.com: context canceled

// This prints false, because the http client wraps the context.Canceled
// error into another one with extra information.
log.Println(err == context.Canceled)

// This prints true, because errors.Is checks all the errors in the wrap chain,
// and returns true if any of them matches.
log.Println(errors.Is(err, context.Canceled))

Tags:

Go