Skip to content

Commit 1dfb6b6

Browse files
committed
Send channel error with server close reason
[#119621443]
1 parent 0ab0d92 commit 1dfb6b6

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

RMQClient/RMQErrors.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ typedef NS_ENUM(NSInteger, RMQError) {
1313
RMQErrorChannelIncorrectSyncMethod,
1414
RMQErrorChannelQueueNameCollision,
1515

16-
RMQErrorInvalidPath,
1716
RMQErrorInvalidScheme,
17+
18+
RMQErrorInvalidPath = 402,
19+
RMQErrorNotFound = 404,
1820
};

RMQClient/RMQSuspendResumeDispatcher.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ - (void)sendAsyncMethod:(id<RMQMethod>)method {
114114

115115
- (void)handleFrameset:(RMQFrameset *)frameset {
116116
if (self.state != DispatcherStateClosedByServer && [self isClose:frameset.method]) {
117-
self.state = DispatcherStateClosedByServer;
118-
[self.sender sendFrameset:[[RMQFrameset alloc] initWithChannelNumber:self.channelNumber
119-
method:[RMQChannelCloseOk new]]];
117+
[self processServerClose:(RMQChannelClose *)frameset.method];
120118
} else if (self.state != DispatcherStateClosedByServer) {
121119
[self.validator fulfill:frameset];
122120
}
@@ -138,6 +136,16 @@ - (void)handleClosure:(id<RMQMethod>)method
138136
}
139137
}
140138

139+
- (void)processServerClose:(RMQChannelClose *)close {
140+
self.state = DispatcherStateClosedByServer;
141+
NSError *error = [NSError errorWithDomain:RMQErrorDomain
142+
code:close.replyCode.integerValue
143+
userInfo:@{NSLocalizedDescriptionKey: close.replyText.stringValue}];
144+
[self.delegate channel:self.channel error:error];
145+
[self.sender sendFrameset:[[RMQFrameset alloc] initWithChannelNumber:self.channelNumber
146+
method:[RMQChannelCloseOk new]]];
147+
}
148+
141149
- (void)sendChannelClosedError {
142150
NSError *error = [NSError errorWithDomain:RMQErrorDomain
143151
code:RMQErrorChannelClosed

RMQClientTests/RMQSuspendResumeDispatcherTest.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,24 @@ class RMQSuspendResumeDispatcherTest: XCTestCase {
223223
XCTAssertFalse(called)
224224
}
225225

226+
func testServerCloseSendsErrorToDelegateWithCloseReasonWhen404() {
227+
let (dispatcher, _, _, delegate, _) = setupActivated()
228+
let close = RMQChannelClose(
229+
replyCode: RMQShort(404),
230+
replyText: RMQShortstr("NOT_FOUND - no exchange 'yomoney' in vhost '/'"),
231+
classId: RMQShort(60),
232+
methodId: RMQShort(40)
233+
)
234+
dispatcher.handleFrameset(RMQFrameset(channelNumber: 123, method: close))
235+
XCTAssertEqual(RMQError.NotFound.rawValue, delegate.lastChannelError?.code)
236+
XCTAssertEqual("NOT_FOUND - no exchange 'yomoney' in vhost '/'", delegate.lastChannelError?.localizedDescription)
237+
}
238+
226239
func testServerCloseTriggersErrorsForFutureOperations() {
227240
let (dispatcher, q, _, delegate, _) = setupActivated()
228241
dispatcher.handleFrameset(RMQFrameset(channelNumber: 123, method: MethodFixtures.channelClose()))
229242
dispatcher.sendSyncMethod(MethodFixtures.basicGet())
230-
XCTAssertNil(delegate.lastChannelError)
243+
delegate.lastChannelError = nil
231244
try! q.step()
232245
XCTAssertEqual(RMQError.ChannelClosed.rawValue, delegate.lastChannelError?.code)
233246
}
@@ -243,6 +256,7 @@ class RMQSuspendResumeDispatcherTest: XCTestCase {
243256
let (dispatcher, q, sender, delegate, _) = setupActivated()
244257
dispatcher.handleFrameset(RMQFrameset(channelNumber: 123, method: MethodFixtures.channelClose()))
245258

259+
delegate.lastChannelError = nil
246260
sender.lastSentMethod = nil
247261
dispatcher.sendSyncMethodBlocking(MethodFixtures.channelClose())
248262
try! q.finish()

0 commit comments

Comments
 (0)