gofr-dev/gofr · critical

%w : %v

Error message

%w : %v

What it means

At pkg/gofr/http_server.go:152, validateCertificateAndKeyFiles returns fmt.Errorf("%w : %v", errInvalidCertificateFile, certificateFile) when os.Stat on the certificate file reports it does not exist. The wrapped error preserves the sentinel for errors.Is checks while appending the offending path for diagnosis.

Source

Thrown at pkg/gofr/http_server.go:152

func (s *httpServer) Shutdown(ctx context.Context) error {
	s.srvMu.Lock()
	srv := s.srv
	s.srvMu.Unlock()

	if srv == nil {
		return nil
	}

	return ShutdownWithContext(ctx, func(ctx context.Context) error {
		return srv.Shutdown(ctx)
	}, func() error {
		return srv.Close()
	})
}

func validateCertificateAndKeyFiles(certificateFile, keyFile string) error {
	if _, err := os.Stat(certificateFile); os.IsNotExist(err) {
		return fmt.Errorf("%w : %v", errInvalidCertificateFile, certificateFile)
	}

	if _, err := os.Stat(keyFile); os.IsNotExist(err) {
		return fmt.Errorf("%w : %v", errInvalidKeyFile, keyFile)
	}

	return nil
}

View on GitHub (pinned to 187eb24962)

Solutions

  1. Stat/ls the exact path printed in the error message to confirm it is missing
  2. Correct the certificate path in config/env to an existing absolute path
  3. Ensure cert provisioning (cert-manager, secret mount, CI step) runs before the service starts
  4. Run the service from a working directory where relative cert paths resolve, or switch to absolute paths

Example fix

// before
certFile := "certs/server.crt" // relative; CWD differs in prod
// after
certFile := "/etc/gofr/tls/server.crt" // absolute, verified with os.Stat
if _, err := os.Stat(certFile); err != nil { log.Fatalf("missing cert: %v", err) }
Defensive patterns

Strategy: validation

Validate before calling

if _, err := os.Stat(certPath); os.IsNotExist(err) {
    return fmt.Errorf("certificate not found at %s", certPath)
}
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
    return fmt.Errorf("key not found at %s", keyPath)
}

Type guard

func isCertFileMissing(err error) bool {
    return errors.Is(err, errInvalidCertificateFile)
}

Try / catch

if err := validateCertificateAndKeyFiles(cert, key); err != nil {
    if errors.Is(err, errInvalidCertificateFile) {
        // message suffix holds the offending path
        log.Fatalf("certificate file missing: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: os.Stat(certificateFile) returns an error satisfying os.IsNotExist during server startup (run() calls validateCertificateAndKeyFiles); the formatted error names the exact missing path.

Common situations: Deploying to a fresh environment where cert provisioning step was skipped; wrong working directory making relative paths invalid; config drift between environments (staging paths hardcoded in prod).

Related errors


AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01). Data as JSON: /api/errors/6754f48abfdb5edf. Report an issue: GitHub.