Skip to content

Cherry pick: Print invalid state, if hitting precondition (#545) #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ struct HTTP1ConnectionStateMachine {
metadata: RequestFramingMetadata
) -> Action {
guard case .idle = self.state else {
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}

var requestStateMachine = HTTPRequestStateMachine(isChannelWritable: self.isChannelWritable)
Expand All @@ -173,7 +173,7 @@ struct HTTP1ConnectionStateMachine {

mutating func requestStreamPartReceived(_ part: IOData) -> Action {
guard case .inRequest(var requestStateMachine, let close) = self.state else {
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}

return self.avoidingStateMachineCoW { state -> Action in
Expand All @@ -185,7 +185,7 @@ struct HTTP1ConnectionStateMachine {

mutating func requestStreamFinished() -> Action {
guard case .inRequest(var requestStateMachine, let close) = self.state else {
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}

return self.avoidingStateMachineCoW { state -> Action in
Expand All @@ -198,7 +198,7 @@ struct HTTP1ConnectionStateMachine {
mutating func requestCancelled(closeConnection: Bool) -> Action {
switch self.state {
case .initialized:
preconditionFailure("This event must only happen, if the connection is leased. During startup this is impossible")
preconditionFailure("This event must only happen, if the connection is leased. During startup this is impossible. Invalid state: \(self.state)")

case .idle:
if closeConnection {
Expand Down Expand Up @@ -250,7 +250,7 @@ struct HTTP1ConnectionStateMachine {
mutating func channelRead(_ part: HTTPClientResponsePart) -> Action {
switch self.state {
case .initialized, .idle:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")

case .inRequest(var requestStateMachine, var close):
return self.avoidingStateMachineCoW { state -> Action in
Expand Down Expand Up @@ -369,7 +369,7 @@ extension HTTP1ConnectionStateMachine.State {
return .forwardResponseBodyParts(parts)
case .succeedRequest(let finalAction, let finalParts):
guard case .inRequest(_, close: let close) = self else {
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self)")
}

let newFinalAction: HTTP1ConnectionStateMachine.Action.FinalStreamAction
Expand All @@ -388,7 +388,7 @@ extension HTTP1ConnectionStateMachine.State {
case .failRequest(let error, let finalAction):
switch self {
case .initialized:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self)")
case .idle:
preconditionFailure("How can we fail a task, if we are idle")
case .inRequest(_, close: let close):
Expand Down Expand Up @@ -433,7 +433,7 @@ extension HTTP1ConnectionStateMachine: CustomStringConvertible {
case .closed:
return ".closed"
case .modifying:
preconditionFailure(".modifying")
preconditionFailure("Invalid state: \(self.state)")
}
}
}
20 changes: 10 additions & 10 deletions Sources/AsyncHTTPClient/RequestBag+StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ extension RequestBag.StateMachine {
return .none

case .finished:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")

case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}

Expand Down Expand Up @@ -158,7 +158,7 @@ extension RequestBag.StateMachine {
// the request is already finished nothing further to do
break
case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}

Expand Down Expand Up @@ -205,7 +205,7 @@ extension RequestBag.StateMachine {
case .finished(error: .none):
return .failFuture(HTTPClientError.requestStreamCancelled)
case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}

Expand Down Expand Up @@ -251,7 +251,7 @@ extension RequestBag.StateMachine {
case .finished(error: _):
return .none
case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ extension RequestBag.StateMachine {
case .finished(error: .none):
preconditionFailure("How can the request be finished without error, before receiving response head?")
case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}

Expand Down Expand Up @@ -319,7 +319,7 @@ extension RequestBag.StateMachine {
case .finished(error: .none):
preconditionFailure("How can the request be finished without error, before receiving response head?")
case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}

Expand Down Expand Up @@ -371,7 +371,7 @@ extension RequestBag.StateMachine {
case .finished(error: .none):
preconditionFailure("How can the request be finished without error, before receiving response head?")
case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}

Expand All @@ -395,7 +395,7 @@ extension RequestBag.StateMachine {
private mutating func failWithConsumptionError(_ error: Error) -> ConsumeAction {
switch self.state {
case .initialized, .queued:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
case .executing(_, _, .initialized):
preconditionFailure("Invalid state: Must have received response head, before this method is called for the first time")

Expand Down Expand Up @@ -518,7 +518,7 @@ extension RequestBag.StateMachine {
// this might happen, if the stream consumer has failed... let's just drop the data
return .none
case .modifying:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}
}
6 changes: 3 additions & 3 deletions Tests/AsyncHTTPClientTests/HTTP2ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ extension TestConnectionCreator: HTTPConnectionRequester {
return .fail(promise, Error.wantedHTTP2ConnectionButGotHTTP1)

case .idle:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}
wrapper.complete()
Expand All @@ -369,7 +369,7 @@ extension TestConnectionCreator: HTTPConnectionRequester {
return .succeed(promise, connection)

case .idle:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}
wrapper.complete()
Expand Down Expand Up @@ -400,7 +400,7 @@ extension TestConnectionCreator: HTTPConnectionRequester {
return .type2(promise)

case .idle:
preconditionFailure("Invalid state")
preconditionFailure("Invalid state: \(self.state)")
}
}
wrapper.fail(error)
Expand Down