Skip to content

Commit

Permalink
B2MDError: nest PayloadTooLargeError (#998)
Browse files Browse the repository at this point in the history
Motivation:

New public NIO types must be nested in existing ones or `NIO` prefixed.
This wasn't the case for ByteToMessageDecoderPayloadTooLargeError

Modifications:

nest PayloadTooLargeError into ByteToMessageDecoderError

Result:

we stick to our rules.
  • Loading branch information
weissi authored and Lukasa committed May 9, 2019
1 parent 5d1d598 commit 0e37ada
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions Sources/NIO/Codec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public enum ByteToMessageDecoderError: Error {
case leftoverDataWhenDone(ByteBuffer)
}

// TODO (tomer): Merge into ByteToMessageDecoderError next major version
/// This error can be thrown by `ByteToMessageDecoder`s if the incoming payload is larger than the max specified.
public struct ByteToMessageDecoderPayloadTooLargeError: Error {}
extension ByteToMessageDecoderError {
// TODO: For NIO 3, make this an enum case (or whatever best way for Errors we have come up with).
/// This error can be thrown by `ByteToMessageDecoder`s if the incoming payload is larger than the max specified.
public struct PayloadTooLargeError: Error {}
}


/// `ByteToMessageDecoder`s decode bytes in a stream-like fashion from `ByteBuffer` to another message type.
///
Expand Down Expand Up @@ -468,7 +471,7 @@ extension ByteToMessageHandler {
decoderResult = try decoder.decodeLast(context: context, buffer: &buffer, seenEOF: self.seenEOF)
}
if decoderResult == .needMoreData, let maximumBufferSize = self.maximumBufferSize, buffer.readableBytes > maximumBufferSize {
throw ByteToMessageDecoderPayloadTooLargeError()
throw ByteToMessageDecoderError.PayloadTooLargeError()
}
return decoderResult
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/NIOTests/CodecTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ public final class ByteToMessageDecoderTest: XCTestCase {
var buffer = channel.allocator.buffer(capacity: max + 1)
buffer.writeString(String(repeating: "*", count: max + 1))
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertTrue(error is ByteToMessageDecoderPayloadTooLargeError)
XCTAssertTrue(error is ByteToMessageDecoderError.PayloadTooLargeError)
}
}

Expand Down

0 comments on commit 0e37ada

Please sign in to comment.