labstack/echo · warning

ErrCookieNotFound

ErrCookieNotFound

Error message

cookie not found

What it means

ErrCookieNotFound is an exported sentinel error representing a missing cookie. Note that Context.Cookie delegates to the standard library http.Request.Cookie, which returns http.ErrNoCookie; Echo provides this sentinel as a semantic alternative for user code and middleware that wish to use Echo's error vocabulary. Middleware like CSRF check c.Cookie and compare against this error shape.

Source

Thrown at httperror.go:33

	ErrUnauthorized                = &httpError{http.StatusUnauthorized}          // 401
	ErrForbidden                   = &httpError{http.StatusForbidden}             // 403
	ErrNotFound                    = &httpError{http.StatusNotFound}              // 404
	ErrMethodNotAllowed            = &httpError{http.StatusMethodNotAllowed}      // 405
	ErrRequestTimeout              = &httpError{http.StatusRequestTimeout}        // 408
	ErrStatusRequestEntityTooLarge = &httpError{http.StatusRequestEntityTooLarge} // 413
	ErrUnsupportedMediaType        = &httpError{http.StatusUnsupportedMediaType}  // 415
	ErrTooManyRequests             = &httpError{http.StatusTooManyRequests}       // 429
	ErrInternalServerError         = &httpError{http.StatusInternalServerError}   // 500
	ErrBadGateway                  = &httpError{http.StatusBadGateway}            // 502
	ErrServiceUnavailable          = &httpError{http.StatusServiceUnavailable}    // 503
)

// The following errors fall into 500 (InternalServerError) category
var (
	ErrValidatorNotRegistered = errors.New("validator not registered")
	ErrRendererNotRegistered  = errors.New("renderer not registered")
	ErrInvalidRedirectCode    = errors.New("invalid redirect status code")
	ErrCookieNotFound         = errors.New("cookie not found")
	ErrInvalidCertOrKeyType   = errors.New("invalid cert or key type, must be string or []byte")
	ErrInvalidListenerNetwork = errors.New("invalid listener network")
)

// HTTPStatusCoder is an interface that errors can implement to produce status code for HTTP response
type HTTPStatusCoder interface {
	StatusCode() int
}

// StatusCode returns status code from err if it implements HTTPStatusCoder interface.
// If err does not implement the interface, it returns 0.
func StatusCode(err error) int {
	var sc HTTPStatusCoder
	if errors.As(err, &sc) {
		return sc.StatusCode()
	}
	return 0
}

View on GitHub (pinned to 05489dc173)

Solutions

  1. Check the returned error and handle the missing-cookie case gracefully (redirect to login, set a new cookie)
  2. Iterate c.Cookies() to see all available cookies for debugging name mismatches
  3. Use errors.Is(err, http.ErrNoCookie) since Context.Cookie returns the stdlib error

Example fix

// before
cookie, err := c.Cookie("session")
if err == echo.ErrCookieNotFound { ... } // may not match stdlib error

// after
cookie, err := c.Cookie("session")
if errors.Is(err, http.ErrNoCookie) {
    return c.Redirect(http.StatusFound, "/login")
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Check cookie presence via Cookies() slice before Cookie()
func hasCookie(c echo.Context, name string) bool {
    for _, ck := range c.Cookies() {
        if ck.Name == name { return true }
    }
    return false
}

Try / catch

cookie, err := c.Cookie("session")
if err != nil {
    if errors.Is(err, http.ErrNoCookie) {
        return c.Redirect(http.StatusFound, "/login")
    }
    return err
}

Prevention

When it happens

Trigger: Calling c.Cookie("session") when the request contains no Cookie header with that name. The underlying stdlib returns http.ErrNoCookie; user code may compare against echo.ErrCookieNotFound when using Echo idioms.

Common situations: First-time visitor with no session cookie. Cookie name mismatch (e.g. case sensitivity, trailing whitespace). Cookie expired and browser stopped sending it.


AI-assisted analysis of labstack/echo@05489dc173 (2026-08-04). Data as JSON: /data/errors/54491081519f3c41.json. Report an issue: GitHub.