Skip to content

Conversation

@618coffee
Copy link
Contributor

@618coffee 618coffee commented Nov 17, 2020

Description of changes:
PR is a fix for #678

  1. When using schema with unsupported region, which means the host generated cannot be reached and causes a url failure.
  2. When the device is not connected to Internet, URLError is also going to be returned

In the above cases, the Callback with error info is not triggered because it is skipping the error check.

The fix is:

  1. Check what type of error return back and avoid calling the next listen() if is of type "NSURLErrorDomain".
  2. Trigger callback in failure case.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@618coffee 618coffee self-assigned this Nov 17, 2020
@codecov
Copy link

codecov bot commented Nov 17, 2020

Codecov Report

Merging #896 (81ec2e5) into main (d29ecdd) will increase coverage by 0.06%.
The diff coverage is 75.80%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #896      +/-   ##
==========================================
+ Coverage   67.29%   67.35%   +0.06%     
==========================================
  Files         895      895              
  Lines       35389    35448      +59     
==========================================
+ Hits        23814    23875      +61     
+ Misses      11575    11573       -2     
Flag Coverage Δ
API_plugin_unit_test 62.56% <ø> (+0.07%) ⬆️
Analytics_plugin_unit_test 72.38% <ø> (ø)
Auth_plugin_unit_test 48.62% <ø> (ø)
DataStore_plugin_unit_test 83.03% <ø> (ø)
Predictions_plugin_unit_test 50.43% <75.80%> (+0.73%) ⬆️
Storage_plugin_unit_test 74.74% <ø> (ø)
build_test_amplify 63.08% <ø> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...gin/Support/Internal/NativeWebSocketProvider.swift 15.87% <0.00%> (+2.53%) ⬆️
...sPlugin/Support/Utils/PredictionsErrorHelper.swift 40.62% <61.90%> (+5.95%) ⬆️
...ctionsTest/PredictionsServiceTranscribeTests.swift 92.70% <81.25%> (-1.52%) ⬇️
...Predictions/AWSPredictionsService+Transcribe.swift 82.35% <88.88%> (+27.35%) ⬆️
...ocks/Service/MockTranscribeStreamingBehavior.swift 92.59% <100.00%> (+17.59%) ⬆️
...Hub/DefaultPluginTests/DefaultHubPluginTests.swift 83.72% <0.00%> (-4.66%) ⬇️
.../DataStore/Model/Internal/Schema/ModelSchema.swift 85.29% <0.00%> (-2.95%) ⬇️
...s/AWSHubPlugin/Internal/HubChannelDispatcher.swift 94.11% <0.00%> (-2.95%) ⬇️
...lugin/Operation/AWSAPIOperation+APIOperation.swift 83.78% <0.00%> (+5.40%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d29ecdd...f5f3689. Read the comment docs.

self.clientDelegate.connectionStatusDidChange(status, withError: error)
}

guard nsError?.domain != NSURLErrorDomain else {
Copy link
Contributor Author

@618coffee 618coffee Nov 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the error is of type NSURLErrorDomain, we do not want to keep calling listen(), we should just return.

Probably

nsError?.code == -1003

should also be checked? https://stackoverflow.com/questions/27258702/error-domain-nsurlerrordomain-code-1003-a-server-with-the-specified-hostname-c

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use -1003 as a literal, though. Use the appropriate error code

Copy link
Member

@palpatim palpatim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments below. Do you have unit tests to cover this case?

switch result {
case .failure(let error):
let status = AWSTranscribeStreamingClientConnectionStatus.closed
let nsError = error as NSError?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error isn't nil, so you should be able to do let nsError = error as NSError, and do away with the optional chaining. My recollection is that Swift.Error always successfully casts to NSError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing ? produces

'Error?' is not convertible to 'NSError'; did you mean to use 'as!' to force downcast?

and force cast is also not allowed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can't, i would go with

if let error = error as NSError? {
   
}

self.clientDelegate.connectionStatusDidChange(status, withError: error)
}

guard nsError?.domain != NSURLErrorDomain else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use -1003 as a literal, though. Use the appropriate error code

@618coffee 618coffee marked this pull request as draft November 18, 2020 19:32
switch result {
case .failure(let error):
let status = AWSTranscribeStreamingClientConnectionStatus.closed
let nsError = error as NSError?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can't, i would go with

if let error = error as NSError? {
   
}

@618coffee 618coffee force-pushed the fix/transcribewithunsupportedregion branch from 005deea to b571032 Compare November 30, 2020 18:03
618coffee and others added 2 commits December 2, 2020 09:02
…PredictionsErrorHelper.swift

Co-authored-by: Michael Law <1365977+lawmicha@users.noreply.github.com>
@618coffee 618coffee marked this pull request as ready for review December 2, 2020 18:02
}
}

static func mapError(_ error: NSError) -> PredictionsError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case NSURLErorrDomain:
   guard let urlError = error as? URLError else {
       return defaultError 
   }
    return .network(description, recovery, urlError)

case .completed(let result):
XCTFail("Should not produce result: \(result)")
case .failed(let error):
XCTAssertNotNil(error, "Should produce an error")
Copy link
Contributor

@lawmicha lawmicha Dec 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: assert it is a network error, then assert underlying error is .cannotFindHost

@618coffee 618coffee changed the title fix(Predictions): Callback is not triggered with unsupported region fix(Predictions): Callback is not triggered with URLError Dec 4, 2020
}
return AWSTranscribeStreamingErrorMessage.map(errorType) ?? defaultError
case NSURLErrorDomain:
return mapError(error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can cast to URLError now that you know it is NSURLErrorDomain, then call a specific method that takes in URLError as a parameter

}
}

static func mapError(_ nsError: NSError) -> PredictionsError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is more like mapURLError(_ urlError: URLError)

@618coffee 618coffee merged commit 17285d1 into main Dec 9, 2020
@618coffee 618coffee deleted the fix/transcribewithunsupportedregion branch December 9, 2020 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants