From 8ff12be4b6058d2c5717828c471bed31e1bbec64 Mon Sep 17 00:00:00 2001 From: Yanhu007 Date: Wed, 15 Apr 2026 19:36:22 +0800 Subject: [PATCH] fix: expose Err() method to check for configuration errors New() silently stores configuration errors (e.g., missing hash key, invalid block key) in a private field. Users have no way to detect these errors until Encode/Decode fails. Add a public Err() method that returns any stored configuration error, allowing callers to check for misconfiguration immediately after calling New(). Fixes #89 --- securecookie.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/securecookie.go b/securecookie.go index 4d5ea86..b1f2451 100644 --- a/securecookie.go +++ b/securecookie.go @@ -188,6 +188,13 @@ type JSONEncoder struct{} // you encoding an object upstream and do not wish to re-encode it. type NopEncoder struct{} +// Err returns any configuration error that occurred during New or subsequent +// setup calls (e.g., BlockFunc). If Err returns non-nil, all Encode and Decode +// calls will fail with this error. +func (s *SecureCookie) Err() error { + return s.err +} + // MaxLength restricts the maximum length, in bytes, for the cookie value. // // Default is 4096, which is the maximum value accepted by Internet Explorer.