From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Context errors

Context errors

- In a complex system or even in a small one, when a context is canceled, we need a way to know what caused the cancellation. It's possible that a context was canceled by a cancellation function successfully or it was canceled because of a timeout or for some other reason entirely. The context package defines a couple different error variables that can be used to check an error that was returned from the context error method. The first is context canceled, which is returned when the context is canceled through the use of a context cancellation function. This error is considered to be a successful cancellation. So here we are again creating a background context and wrapping it in a cancelable context. When we first check the context error method, it returns nil. After we call a context cancellation function provided by the context with cancel function, the context errors method returns a context canceled error. When a context is canceled due to a deadline or a timeout being exceeded…

Contents