@@ -87,9 +87,30 @@ public async Task InitializeAsync(CancellationToken cancellationToken = default)
8787 activity ? . SetTag ( "receive.mode" , receiverOptions . ReceiveMode . ToString ( ) ) ;
8888 activity ? . SetStatus ( ActivityStatusCode . Ok ) ;
8989 }
90+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx )
91+ {
92+ _logger . LogError ( sbEx , "Service Bus error initializing connector: {Reason}" , sbEx . Reason ) ;
93+ activity ? . SetStatus ( ActivityStatusCode . Error , $ "Service Bus error: { sbEx . Reason } ") ;
94+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , sbEx . Reason . ToString ( ) } } ) ) ;
95+ throw ;
96+ }
97+ catch ( UnauthorizedAccessException uaEx )
98+ {
99+ _logger . LogError ( uaEx , "Unauthorized access to Service Bus - check connection string and permissions" ) ;
100+ activity ? . SetStatus ( ActivityStatusCode . Error , "Unauthorized access" ) ;
101+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "UnauthorizedAccessException" } , { "exception.message" , uaEx . Message } } ) ) ;
102+ throw ;
103+ }
104+ catch ( ArgumentException argEx )
105+ {
106+ _logger . LogError ( argEx , "Invalid Service Bus configuration" ) ;
107+ activity ? . SetStatus ( ActivityStatusCode . Error , "Invalid configuration" ) ;
108+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ArgumentException" } , { "exception.message" , argEx . Message } } ) ) ;
109+ throw ;
110+ }
90111 catch ( Exception ex )
91112 {
92- _logger . LogError ( ex , "Failed to initialize Service Bus connector" ) ;
113+ _logger . LogError ( ex , "Unexpected error initializing Service Bus connector" ) ;
93114 activity ? . SetStatus ( ActivityStatusCode . Error , ex . Message ) ;
94115 activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , ex . GetType ( ) . FullName } , { "exception.message" , ex . Message } } ) ) ;
95116 throw ;
@@ -158,9 +179,29 @@ public async Task<IReadOnlyList<ReceivedMessage>> ReceiveMessagesAsync(
158179
159180 return messages ;
160181 }
182+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx ) when ( sbEx . Reason == Azure . Messaging . ServiceBus . ServiceBusFailureReason . ServiceTimeout )
183+ {
184+ _logger . LogWarning ( sbEx , "Timeout receiving messages from Service Bus" ) ;
185+ activity ? . SetStatus ( ActivityStatusCode . Error , "Service timeout" ) ;
186+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , "ServiceTimeout" } } ) ) ;
187+ throw ;
188+ }
189+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx )
190+ {
191+ _logger . LogError ( sbEx , "Service Bus error receiving messages: {Reason}" , sbEx . Reason ) ;
192+ activity ? . SetStatus ( ActivityStatusCode . Error , $ "Service Bus error: { sbEx . Reason } ") ;
193+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , sbEx . Reason . ToString ( ) } } ) ) ;
194+ throw ;
195+ }
196+ catch ( OperationCanceledException )
197+ {
198+ _logger . LogInformation ( "Message receive operation cancelled" ) ;
199+ activity ? . SetStatus ( ActivityStatusCode . Error , "Cancelled" ) ;
200+ throw ;
201+ }
161202 catch ( Exception ex )
162203 {
163- _logger . LogError ( ex , "Error receiving messages from Service Bus" ) ;
204+ _logger . LogError ( ex , "Unexpected error receiving messages from Service Bus" ) ;
164205 activity ? . SetStatus ( ActivityStatusCode . Error , ex . Message ) ;
165206 activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , ex . GetType ( ) . FullName } , { "exception.message" , ex . Message } } ) ) ;
166207 throw ;
@@ -189,9 +230,45 @@ public async Task<MessageCompletionResult> CompleteMessageAsync(
189230
190231 return new MessageCompletionResult { Success = true } ;
191232 }
233+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx ) when ( sbEx . Reason == Azure . Messaging . ServiceBus . ServiceBusFailureReason . MessageLockLost )
234+ {
235+ _logger . LogWarning ( sbEx , "Cannot complete message {MessageId} - lock lost" , message . MessageId ) ;
236+ activity ? . SetStatus ( ActivityStatusCode . Error , "Lock lost" ) ;
237+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , "MessageLockLost" } } ) ) ;
238+
239+ return new MessageCompletionResult
240+ {
241+ Success = false ,
242+ ErrorMessage = "Message lock lost"
243+ } ;
244+ }
245+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx )
246+ {
247+ _logger . LogError ( sbEx , "Service Bus error completing message {MessageId}: {Reason}" , message . MessageId , sbEx . Reason ) ;
248+ activity ? . SetStatus ( ActivityStatusCode . Error , $ "Service Bus error: { sbEx . Reason } ") ;
249+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , sbEx . Reason . ToString ( ) } } ) ) ;
250+
251+ return new MessageCompletionResult
252+ {
253+ Success = false ,
254+ ErrorMessage = $ "Service Bus error: { sbEx . Reason } "
255+ } ;
256+ }
257+ catch ( InvalidOperationException invalidOpEx )
258+ {
259+ _logger . LogError ( invalidOpEx , "Invalid operation completing message {MessageId} - message may already be settled" , message . MessageId ) ;
260+ activity ? . SetStatus ( ActivityStatusCode . Error , "Invalid operation" ) ;
261+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "InvalidOperationException" } , { "exception.message" , invalidOpEx . Message } } ) ) ;
262+
263+ return new MessageCompletionResult
264+ {
265+ Success = false ,
266+ ErrorMessage = "Message already settled"
267+ } ;
268+ }
192269 catch ( Exception ex )
193270 {
194- _logger . LogError ( ex , "Error completing message: MessageId={MessageId}" , message . MessageId ) ;
271+ _logger . LogError ( ex , "Unexpected error completing message: MessageId={MessageId}" , message . MessageId ) ;
195272 activity ? . SetStatus ( ActivityStatusCode . Error , ex . Message ) ;
196273 activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , ex . GetType ( ) . FullName } , { "exception.message" , ex . Message } } ) ) ;
197274
@@ -225,9 +302,45 @@ public async Task<MessageCompletionResult> AbandonMessageAsync(
225302
226303 return new MessageCompletionResult { Success = true } ;
227304 }
305+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx ) when ( sbEx . Reason == Azure . Messaging . ServiceBus . ServiceBusFailureReason . MessageLockLost )
306+ {
307+ _logger . LogWarning ( sbEx , "Cannot abandon message {MessageId} - lock lost" , message . MessageId ) ;
308+ activity ? . SetStatus ( ActivityStatusCode . Error , "Lock lost" ) ;
309+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , "MessageLockLost" } } ) ) ;
310+
311+ return new MessageCompletionResult
312+ {
313+ Success = false ,
314+ ErrorMessage = "Message lock lost"
315+ } ;
316+ }
317+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx )
318+ {
319+ _logger . LogError ( sbEx , "Service Bus error abandoning message {MessageId}: {Reason}" , message . MessageId , sbEx . Reason ) ;
320+ activity ? . SetStatus ( ActivityStatusCode . Error , $ "Service Bus error: { sbEx . Reason } ") ;
321+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , sbEx . Reason . ToString ( ) } } ) ) ;
322+
323+ return new MessageCompletionResult
324+ {
325+ Success = false ,
326+ ErrorMessage = $ "Service Bus error: { sbEx . Reason } "
327+ } ;
328+ }
329+ catch ( InvalidOperationException invalidOpEx )
330+ {
331+ _logger . LogError ( invalidOpEx , "Invalid operation abandoning message {MessageId} - message may already be settled" , message . MessageId ) ;
332+ activity ? . SetStatus ( ActivityStatusCode . Error , "Invalid operation" ) ;
333+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "InvalidOperationException" } , { "exception.message" , invalidOpEx . Message } } ) ) ;
334+
335+ return new MessageCompletionResult
336+ {
337+ Success = false ,
338+ ErrorMessage = "Message already settled"
339+ } ;
340+ }
228341 catch ( Exception ex )
229342 {
230- _logger . LogError ( ex , "Error abandoning message: MessageId={MessageId}" , message . MessageId ) ;
343+ _logger . LogError ( ex , "Unexpected error abandoning message: MessageId={MessageId}" , message . MessageId ) ;
231344 activity ? . SetStatus ( ActivityStatusCode . Error , ex . Message ) ;
232345 activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , ex . GetType ( ) . FullName } , { "exception.message" , ex . Message } } ) ) ;
233346
@@ -277,11 +390,54 @@ public async Task<MessageCompletionResult> DeadLetterMessageAsync(
277390
278391 return new MessageCompletionResult { Success = true } ;
279392 }
393+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx ) when ( sbEx . Reason == Azure . Messaging . ServiceBus . ServiceBusFailureReason . MessageLockLost )
394+ {
395+ _logger . LogWarning ( sbEx , "Cannot dead-letter message {MessageId} - lock lost" , message . MessageId ) ;
396+ activity ? . SetStatus ( ActivityStatusCode . Error , "Lock lost" ) ;
397+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , "MessageLockLost" } } ) ) ;
398+
399+ return new MessageCompletionResult
400+ {
401+ Success = false ,
402+ ErrorMessage = "Message lock lost"
403+ } ;
404+ }
405+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx )
406+ {
407+ _logger . LogError (
408+ sbEx ,
409+ "Service Bus error dead-lettering message {MessageId}: {Reason}" ,
410+ message . MessageId ,
411+ sbEx . Reason ) ;
412+ activity ? . SetStatus ( ActivityStatusCode . Error , $ "Service Bus error: { sbEx . Reason } ") ;
413+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , sbEx . Reason . ToString ( ) } } ) ) ;
414+
415+ return new MessageCompletionResult
416+ {
417+ Success = false ,
418+ ErrorMessage = $ "Service Bus error: { sbEx . Reason } "
419+ } ;
420+ }
421+ catch ( InvalidOperationException invalidOpEx )
422+ {
423+ _logger . LogError (
424+ invalidOpEx ,
425+ "Invalid operation dead-lettering message {MessageId} - message may already be settled" ,
426+ message . MessageId ) ;
427+ activity ? . SetStatus ( ActivityStatusCode . Error , "Invalid operation" ) ;
428+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "InvalidOperationException" } , { "exception.message" , invalidOpEx . Message } } ) ) ;
429+
430+ return new MessageCompletionResult
431+ {
432+ Success = false ,
433+ ErrorMessage = "Message already settled"
434+ } ;
435+ }
280436 catch ( Exception ex )
281437 {
282438 _logger . LogError (
283439 ex ,
284- "Error dead-lettering message: MessageId={MessageId}, Reason={Reason}" ,
440+ "Unexpected error dead-lettering message: MessageId={MessageId}, Reason={Reason}" ,
285441 message . MessageId ,
286442 reason ) ;
287443 activity ? . SetStatus ( ActivityStatusCode . Error , ex . Message ) ;
@@ -328,9 +484,22 @@ public async Task CloseAsync(CancellationToken cancellationToken = default)
328484 _logger . LogInformation ( "Service Bus connector closed successfully" ) ;
329485 activity ? . SetStatus ( ActivityStatusCode . Ok ) ;
330486 }
487+ catch ( Azure . Messaging . ServiceBus . ServiceBusException sbEx )
488+ {
489+ _logger . LogError ( sbEx , "Service Bus error closing connector: {Reason}" , sbEx . Reason ) ;
490+ activity ? . SetStatus ( ActivityStatusCode . Error , $ "Service Bus error: { sbEx . Reason } ") ;
491+ activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , "ServiceBusException" } , { "exception.reason" , sbEx . Reason . ToString ( ) } } ) ) ;
492+ throw ;
493+ }
494+ catch ( ObjectDisposedException )
495+ {
496+ _logger . LogWarning ( "Service Bus connector already disposed" ) ;
497+ activity ? . SetStatus ( ActivityStatusCode . Ok ) ;
498+ // Don't throw - already disposed is acceptable
499+ }
331500 catch ( Exception ex )
332501 {
333- _logger . LogError ( ex , "Error closing Service Bus connector" ) ;
502+ _logger . LogError ( ex , "Unexpected error closing Service Bus connector" ) ;
334503 activity ? . SetStatus ( ActivityStatusCode . Error , ex . Message ) ;
335504 activity ? . AddEvent ( new ActivityEvent ( "exception" , tags : new ActivityTagsCollection { { "exception.type" , ex . GetType ( ) . FullName } , { "exception.message" , ex . Message } } ) ) ;
336505 throw ;
0 commit comments