Skip to content

Replaced manual error response deserialization with ErrorResponse#1429

Merged
NachoSoto merged 1 commit into
mainfrom
error-response-using-it
Apr 1, 2022
Merged

Replaced manual error response deserialization with ErrorResponse#1429
NachoSoto merged 1 commit into
mainfrom
error-response-using-it

Conversation

@NachoSoto

@NachoSoto NachoSoto commented Mar 30, 2022

Copy link
Copy Markdown
Contributor

Follow up to #1427.

The new way to create errors is like this:

ErrorResponse
    .from(response)
    .asBackendError(with: statusCode)

Other changes:

  • Removed UserInfoAttributeParser
  • Simplified JSONDecoder.decode implementation by simply relying on defaultJsonDecoder
  • Added support for "wrapped" error responses. This was previously handled by UserInfoAttributeParser.
  • Removed all duplicated code parsing error responses
  • The new ErrorResponse introduced in Created ErrorResponse to abstract error deserialization #1427 correctly decodes the format sent by the backend. Note that some tests had the wrong format, and UserInfoAttributeParser was parsing that wrong as well.

An upcoming PR will actually move these calls to ErrorResponse to HTTPClient.

@NachoSoto NachoSoto requested review from a team, aboedo and taquitos March 30, 2022 00:15

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This decode method mutates the instance it's called on, which isn't a great practice. At least the keyDecodingStrategy is no longer changed and we rely on a single defaultJsonDecoder everywhere. I should look at the date and data decoding strategies, we might be able to just set them on defaultJsonDecoder too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done: #1432

Comment thread Sources/Networking/HTTPResponse.swift Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was previously handled by UserInfoAttributeParser.

@NachoSoto NachoSoto force-pushed the error-response-using-it branch 2 times, most recently from 0f4ef59 to c4830a4 Compare March 30, 2022 19:06
@NachoSoto NachoSoto force-pushed the error-response-using-it branch from c4830a4 to 0f99cc1 Compare March 30, 2022 20:29
@NachoSoto NachoSoto force-pushed the error-response-using-it branch from 0f99cc1 to 09b1496 Compare March 30, 2022 21:37
NachoSoto added a commit that referenced this pull request Mar 30, 2022
Follow up to #1429. For #695.

This was duplicated in all operations.
`HTTPClient` now only forwards `Result.success` if the request actually succeeded,
leaving each `NetworkOperation` to just parse the body (which will become part of `HTTPClient` too).

Other changes:
- Simplified logic for when to retry requests in `HTTPClient` by using a new `Result.asOptionalResult`
- Removed several response handlers that don't need to do anything anymore
- Removed several tests that were basically duplicated:
  - The `finishable` checks are already tested as part of `ErrorResponse`.
  - All the checks for the error details are also part of `ErrorResponse`.
  - No longer need to have a test for each type of failed response since that's part of `HTTPClient` tests now.
NachoSoto added a commit that referenced this pull request Mar 30, 2022
Follow up to #1429. The `decode` methods no longer mutate the underlying `JSONDecoder`, and instead the codebase uses a shared `defaultJSONDecoder` with the correct configuration.
: .failure(
ErrorUtils.backendError(withBackendCode: BackendErrorCode(code: response["code"]),
backendMessage: response["message"] as? String)
ErrorResponse

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is SO much better. I hacked on the errors a bunch just to get it working and surfacing what we wanted to surface, but this is a nice API 🧡

@taquitos taquitos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

YES. This is good.
yes-enthusiastically-yes

It's not Friday, yet, but I'm still dropping a gif.

@NachoSoto NachoSoto force-pushed the error-response-using-it branch from 09b1496 to 58eac08 Compare April 1, 2022 00:04
NachoSoto added a commit that referenced this pull request Apr 1, 2022
Follow up to #1429. For #695.

This was duplicated in all operations.
`HTTPClient` now only forwards `Result.success` if the request actually succeeded,
leaving each `NetworkOperation` to just parse the body (which will become part of `HTTPClient` too).

Other changes:
- Simplified logic for when to retry requests in `HTTPClient` by using a new `Result.asOptionalResul`
- Removed several response handlers that don't need to do anything anymore
- Removed several tests that were basically duplicated:
    - The `finishable` checks are already tested as part of `ErrorResponse`.
    - All the checks for the error details are also part of `ErrorResponse`.
    - No longer need to have a test for each type of failed response since that's part of `HTTPClient`tests now.
NachoSoto added a commit that referenced this pull request Apr 1, 2022
Follow up to #1429. The `decode` methods no longer mutate the underlying `JSONDecoder`, and instead the codebase uses a shared `defaultJSONDecoder` with the correct configuration.
@NachoSoto NachoSoto force-pushed the error-response-using-it branch from 58eac08 to 9b707ec Compare April 1, 2022 20:51
NachoSoto added a commit that referenced this pull request Apr 1, 2022
Follow up to #1429. For #695.

This was duplicated in all operations.
`HTTPClient` now only forwards `Result.success` if the request actually succeeded,
leaving each `NetworkOperation` to just parse the body (which will become part of `HTTPClient` too).

Other changes:
- Simplified logic for when to retry requests in `HTTPClient` by using a new `Result.asOptionalResul`
- Removed several response handlers that don't need to do anything anymore
- Removed several tests that were basically duplicated:
    - The `finishable` checks are already tested as part of `ErrorResponse`.
    - All the checks for the error details are also part of `ErrorResponse`.
    - No longer need to have a test for each type of failed response since that's part of `HTTPClient`tests now.
NachoSoto added a commit that referenced this pull request Apr 1, 2022
Follow up to #1429. The `decode` methods no longer mutate the underlying `JSONDecoder`, and instead the codebase uses a shared `defaultJSONDecoder` with the correct configuration.
Base automatically changed from error-response to main April 1, 2022 21:24
NachoSoto added a commit that referenced this pull request Apr 1, 2022
This is duplicated in most of the `NetworkOperation`s. This new shared implementation will be used by `HTTPClient`.

For #695.
See #1429 for how this is used.
Follow up to #1427.

Other changes:
- Removed `UserInfoAttributeParser`
- Simplified `JSONDecoder.decode` implementation by simply relying on `defaultJsonDecoder`
- Added support for "wrapped" error responses. This was previously handled by `UserInfoAttributeParser`.
- Removed all duplicated code parsing error responses
- The new `ErrorResponse` introduced in #1427 correctly decodes the format sent by the backend. Note that some tests had the wrong format, and `UserInfoAttributeParser` was parsing that wrong as well.

An upcoming PR will actually move these calls to `ErrorResponse` to `HTTPClient`.
@NachoSoto NachoSoto force-pushed the error-response-using-it branch from 9b707ec to 3ba81e4 Compare April 1, 2022 21:25
@NachoSoto NachoSoto merged commit f084b25 into main Apr 1, 2022
@NachoSoto NachoSoto deleted the error-response-using-it branch April 1, 2022 21:26
NachoSoto added a commit that referenced this pull request Apr 1, 2022
Follow up to #1429. The `decode` methods no longer mutate the underlying `JSONDecoder`, and instead the codebase uses a shared `defaultJSONDecoder` with the correct configuration.
NachoSoto added a commit that referenced this pull request Apr 1, 2022
Follow up to #1429. The `decode` methods no longer mutate the underlying `JSONDecoder`, and instead the codebase uses a shared `defaultJSONDecoder` with the correct configuration.
NachoSoto added a commit that referenced this pull request Apr 1, 2022
Follow up to #1429. For #695.

This was duplicated in all operations.
`HTTPClient` now only forwards `Result.success` if the request actually succeeded,
leaving each `NetworkOperation` to just parse the body (which will become part of `HTTPClient` too).

Other changes:
- Simplified logic for when to retry requests in `HTTPClient` by using a new `Result.asOptionalResul`
- Removed several response handlers that don't need to do anything anymore
- Removed several tests that were basically duplicated:
    - The `finishable` checks are already tested as part of `ErrorResponse`.
    - All the checks for the error details are also part of `ErrorResponse`.
    - No longer need to have a test for each type of failed response since that's part of `HTTPClient`tests now.
NachoSoto added a commit that referenced this pull request Apr 5, 2022
Follow up to #1429. For #695.

This was duplicated in all operations.
`HTTPClient` now only forwards `Result.success` if the request actually succeeded,
leaving each `NetworkOperation` to just parse the body (which will become part of `HTTPClient` too).

Other changes:
- Simplified logic for when to retry requests in `HTTPClient` by using a new `Result.asOptionalResult`
- Removed several response handlers that don't need to do anything anymore
- Removed several tests that were basically duplicated:
  - The `finishable` checks are already tested as part of `ErrorResponse`.
  - All the checks for the error details are also part of `ErrorResponse`.
  - No longer need to have a test for each type of failed response since that's part of `HTTPClient` tests now.
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.

2 participants