Skip to content

Commit 9d54e55

Browse files
committed
Refactor session management methods to improve error handling consistency and readability
1 parent 3adce03 commit 9d54e55

1 file changed

Lines changed: 60 additions & 72 deletions

File tree

examples/error_handling_example.cr

Lines changed: 60 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,45 @@ class SessionManager
6060

6161
# Example: Create session with error handling
6262
def create_user_session(user_id : Int64, username : String) : Bool
63+
session = @session.create
64+
session.data.user_id = user_id
65+
session.data.username = username
66+
session.data.last_login = Time.utc
67+
session.data.reset_login_attempts
6368

64-
session = @session.create
65-
session.data.user_id = user_id
66-
session.data.username = username
67-
session.data.last_login = Time.utc
68-
session.data.reset_login_attempts
69-
70-
Log.info { "Created session for user #{username}" }
71-
true
72-
rescue ex : Session::SessionValidationException
73-
Log.error { "Failed to create session: #{ex.message}" }
74-
false
75-
rescue ex : Session::StorageConnectionException
76-
Log.error { "Storage connection failed: #{ex.message}" }
77-
false
78-
rescue ex : Exception
79-
Log.error { "Unexpected error creating session: #{ex.message}" }
80-
false
81-
69+
Log.info { "Created session for user #{username}" }
70+
true
71+
rescue ex : Session::SessionValidationException
72+
Log.error { "Failed to create session: #{ex.message}" }
73+
false
74+
rescue ex : Session::StorageConnectionException
75+
Log.error { "Storage connection failed: #{ex.message}" }
76+
false
77+
rescue ex : Exception
78+
Log.error { "Unexpected error creating session: #{ex.message}" }
79+
false
8280
end
8381

8482
# Example: Load session with error handling
8583
def load_user_session : UserSession?
86-
87-
@session.load_from(HTTP::Cookies.new) # In real app, pass actual cookies
88-
@session.data
89-
rescue ex : Session::SessionExpiredException
90-
Log.info { "Session expired, creating new session" }
91-
create_new_session
92-
rescue ex : Session::SessionCorruptionException
93-
Log.warn { "Session corrupted, clearing and creating new session" }
94-
clear_corrupted_session
95-
create_new_session
96-
rescue ex : Session::SessionNotFoundException
97-
Log.info { "No session found, creating new session" }
98-
create_new_session
99-
rescue ex : Session::StorageConnectionException
100-
Log.error { "Storage connection failed: #{ex.message}" }
101-
nil
102-
rescue ex : Exception
103-
Log.error { "Unexpected error loading session: #{ex.message}" }
104-
nil
105-
84+
@session.load_from(HTTP::Cookies.new) # In real app, pass actual cookies
85+
@session.data
86+
rescue ex : Session::SessionExpiredException
87+
Log.info { "Session expired, creating new session" }
88+
create_new_session
89+
rescue ex : Session::SessionCorruptionException
90+
Log.warn { "Session corrupted, clearing and creating new session" }
91+
clear_corrupted_session
92+
create_new_session
93+
rescue ex : Session::SessionNotFoundException
94+
Log.info { "No session found, creating new session" }
95+
create_new_session
96+
rescue ex : Session::StorageConnectionException
97+
Log.error { "Storage connection failed: #{ex.message}" }
98+
nil
99+
rescue ex : Exception
100+
Log.error { "Unexpected error loading session: #{ex.message}" }
101+
nil
106102
end
107103

108104
# Example: Update session with retry logic
@@ -131,32 +127,28 @@ class SessionManager
131127

132128
# Example: Delete session with error handling
133129
def delete_user_session : Bool
134-
135-
@session.delete
136-
Log.info { "Deleted user session" }
137-
true
138-
rescue ex : Session::StorageConnectionException
139-
Log.warn { "Storage connection failed during deletion: #{ex.message}" }
140-
# Session will expire naturally, so this is not critical
141-
true
142-
rescue ex : Exception
143-
Log.warn { "Error deleting session: #{ex.message}" }
144-
false
145-
130+
@session.delete
131+
Log.info { "Deleted user session" }
132+
true
133+
rescue ex : Session::StorageConnectionException
134+
Log.warn { "Storage connection failed during deletion: #{ex.message}" }
135+
# Session will expire naturally, so this is not critical
136+
true
137+
rescue ex : Exception
138+
Log.warn { "Error deleting session: #{ex.message}" }
139+
false
146140
end
147141

148142
# Example: Check session health
149143
def check_session_health : Bool
150-
151-
if store = @session.as?(Session::RedisStore(UserSession))
152-
store.healthy?
153-
else
154-
true # Memory store is always healthy
155-
end
156-
rescue ex : Exception
157-
Log.warn { "Health check failed: #{ex.message}" }
158-
false
159-
144+
if store = @session.as?(Session::RedisStore(UserSession))
145+
store.healthy?
146+
else
147+
true # Memory store is always healthy
148+
end
149+
rescue ex : Exception
150+
Log.warn { "Health check failed: #{ex.message}" }
151+
false
160152
end
161153

162154
# Example: Clean up expired sessions (for memory store)
@@ -189,21 +181,17 @@ class SessionManager
189181
end
190182

191183
private def create_new_session : UserSession?
192-
193-
@session.create
194-
@session.data
195-
rescue ex : Exception
196-
Log.error { "Failed to create new session: #{ex.message}" }
197-
nil
198-
184+
@session.create
185+
@session.data
186+
rescue ex : Exception
187+
Log.error { "Failed to create new session: #{ex.message}" }
188+
nil
199189
end
200190

201191
private def clear_corrupted_session
202-
203-
@session.delete
204-
rescue ex : Exception
205-
Log.warn { "Failed to clear corrupted session: #{ex.message}" }
206-
192+
@session.delete
193+
rescue ex : Exception
194+
Log.warn { "Failed to clear corrupted session: #{ex.message}" }
207195
end
208196
end
209197

0 commit comments

Comments
 (0)