Add a location to each of 'NIOHTTP2Errors' (#247)

Motivation:

Without knowing where an error was thrown it can be unnecessarily
difficult to work out the reasons an application ended up in an error
state.

Modifications:

- Add a 'file' string to each of the 'NIOHTTP2Errors' denoting the file
  and line where an error was created
- Add static methods to create errors to 'NIOHTTP2Errors' defaulting
  file and line, this is unfortunately necessary as defaulting file and
  line in the `init` makes the defaulted and non-defaulted errors
  indistinguishable, and the compiler will pick the one with fewest
  defaulted values.
- Add equatable conformance manually so as to ignore 'file'
- Add backing storage to errors which no longer be stored in an existential
  container
- Add 'CustomStringConvertible' conformance to the errors using backing
  storage so that the 'file' is actually visible when converting the
  error
- Deprecate the old `init`s
- Fix all deprecation warnings

Result:

- NIOHTTP2Errors carry a 'file' with them indicating where they were
  created
This commit is contained in:
George Barnett authored and GitHub committed 2020-09-24 15:21:58 +01:00
1 parent 28d94c489d
commit 440d64d99a
33 files changed
+1598 -233

No files matched your search

+27 -19
View File
@@ -203,13 +203,13 @@ extension NIOHTTP2Handler {
do {
return try self.frameDecoder.nextFrame()
} catch InternalError.codecError(let code) {
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.UnableToParseFrame(), reason: code)
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.unableToParseFrame(), reason: code)
return nil
} catch is NIOHTTP2Errors.BadClientMagic {
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.BadClientMagic(), reason: .protocolError)
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.badClientMagic(), reason: .protocolError)
return nil
} catch is NIOHTTP2Errors.ExcessivelyLargeHeaderBlock {
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.ExcessivelyLargeHeaderBlock(), reason: .protocolError)
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.excessivelyLargeHeaderBlock(), reason: .protocolError)
return nil
} catch {
self.inboundConnectionErrorTriggered(context: context, underlyingError: error, reason: .internalError)
@@ -470,7 +470,7 @@ extension NIOHTTP2Handler {
do {
extraFrameData = try self.frameEncoder.encode(frame: frame, to: &self.writeBuffer)
} catch InternalError.codecError {
self.outboundErrorTriggered(context: context, promise: promise, underlyingError: NIOHTTP2Errors.UnableToSerializeFrame())
self.outboundErrorTriggered(context: context, promise: promise, underlyingError: NIOHTTP2Errors.unableToSerializeFrame())
return
} catch {
self.outboundErrorTriggered(context: context, promise: promise, underlyingError: error)
@@ -509,11 +509,8 @@ extension NIOHTTP2Handler {
self.inboundEventBuffer.pendingUserEvent(StreamClosedEvent(streamID: streamClosedData.streamID, reason: streamClosedData.reason))
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2WindowUpdatedEvent(streamID: .rootStream, inboundWindowSize: streamClosedData.remoteConnectionWindowSize, outboundWindowSize: streamClosedData.localConnectionWindowSize))
let failedWrites = self.outboundBuffer.streamClosed(streamClosedData.streamID)
let error = NIOHTTP2Errors.StreamClosed(streamID: streamClosedData.streamID, errorCode: streamClosedData.reason ?? .cancel)
for promise in failedWrites {
promise?.fail(error)
}
let droppedPromises = self.outboundBuffer.streamClosed(streamClosedData.streamID)
self.failDroppedPromises(droppedPromises, streamID: streamClosedData.streamID, errorCode: streamClosedData.reason ?? .cancel)
case .streamCreated(let streamCreatedData):
self.outboundBuffer.streamCreated(streamCreatedData.streamID, initialWindowSize: streamCreatedData.localStreamWindowSize.map(UInt32.init) ?? 0)
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2StreamCreatedEvent(streamID: streamCreatedData.streamID,
@@ -523,11 +520,8 @@ extension NIOHTTP2Handler {
for droppedStream in streamClosureData.closedStreams {
self.inboundEventBuffer.pendingUserEvent(StreamClosedEvent(streamID: droppedStream, reason: .cancel))
let failedWrites = self.outboundBuffer.streamClosed(droppedStream)
let error = NIOHTTP2Errors.StreamClosed(streamID: droppedStream, errorCode: .cancel)
for promise in failedWrites {
promise?.fail(error)
}
let droppedPromises = self.outboundBuffer.streamClosed(droppedStream)
self.failDroppedPromises(droppedPromises, streamID: droppedStream, errorCode: .cancel)
}
case .flowControlChange(let change):
self.outboundBuffer.connectionWindowSize = change.localConnectionWindowSize
@@ -538,11 +532,8 @@ extension NIOHTTP2Handler {
}
case .streamCreatedAndClosed(let cAndCData):
self.outboundBuffer.streamCreated(cAndCData.streamID, initialWindowSize: 0)
let failedWrites = self.outboundBuffer.streamClosed(cAndCData.streamID)
let error = NIOHTTP2Errors.StreamClosed(streamID: cAndCData.streamID, errorCode: .cancel)
for promise in failedWrites {
promise?.fail(error)
}
let droppedPromises = self.outboundBuffer.streamClosed(cAndCData.streamID)
self.failDroppedPromises(droppedPromises, streamID: cAndCData.streamID, errorCode: .cancel)
case .remoteSettingsChanged(let settingsChange):
if settingsChange.streamWindowSizeChange != 0 {
self.outboundBuffer.initialWindowSizeChanged(settingsChange.streamWindowSizeChange)
@@ -583,6 +574,23 @@ extension NIOHTTP2Handler {
context.flush()
}
}
/// Fails any promises in the given collection with a 'StreamClosed' error.
private func failDroppedPromises(_ promises: CompoundOutboundBuffer.DroppedPromisesCollection,
streamID: HTTP2StreamID,
errorCode: HTTP2ErrorCode,
file: String = #file, line: UInt = #line) {
// 'NIOHTTP2Errors.streamClosed' always allocates, if there are no promises then there's no
// need to create the error.
guard promises.contains(where: { $0 != nil }) else {
return
}
let error = NIOHTTP2Errors.streamClosed(streamID: streamID, errorCode: errorCode, file: file, line: line)
for promise in promises {
promise?.fail(error)
}
}
}