Skip to content

MS-1382 Correct Response handling in backend api client#1620

Merged
luhmirin-s merged 3 commits intomainfrom
hotfix/MS-1382-reponse-handling-main
Mar 18, 2026
Merged

MS-1382 Correct Response handling in backend api client#1620
luhmirin-s merged 3 commits intomainfrom
hotfix/MS-1382-reponse-handling-main

Conversation

@luhmirin-s
Copy link
Contributor

@luhmirin-s luhmirin-s commented Mar 18, 2026

JIRA ticket
Will be released in: 2025.2.1

Root cause analysis (for bugfixes only)

First known affected version: 2024.1.5

  • When retrofit interface returns Response<T> type, then it will never throw during call execution.

Notable changes

  • Added an explicit check for Response<ResponseBody> type in the backend API client and unwrapping to follow the same rules as the regular responses - if not successful it is wrapped in ApiResponse.Failure() with the SyncCloudIntegrationException as the cause. This should remove the differences in handling of successful and unsuccessful calls in the domain layer.
  • Just to be sure, added additional layer of unwrapping on the network layer.

Testing guidance

  • Sync should work as expected.
  • There isn't really a way to reliably reproduce the issue by hand, so we will have to rely on unit tests.

Additional work checklist

  • Effect on other features and security has been considered
  • Design document marked as "In development" (if applicable)
  • External (Gitbook) and internal (Confluence) Documentation is up to date (or ticket created)
  • Test cases in Testiny are up to date (or ticket created)
  • Other teams notified about the changes (if applicable)

… responses

# Conflicts:
#	infra/event-sync/src/main/java/com/simprints/infra/eventsync/event/remote/EventRemoteDataSource.kt
@cla-bot cla-bot bot added the ... label Mar 18, 2026
@luhmirin-s luhmirin-s requested review from a team, BurningAXE, TristramN, alex-vt, alexandr-simprints, Copilot, meladRaouf and ybourgery and removed request for a team March 18, 2026 10:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes inconsistent error handling when Retrofit endpoints return Response<T> by ensuring non-2xx responses are treated like regular failed calls (throwing and being mapped consistently), improving reliability of sync/network error behavior across infra modules.

Changes:

  • Updated SimApiClientImpl to throw HttpException when executeCall returns an unsuccessful retrofit2.Response<*>.
  • Added/expanded unit tests covering Response<T> success/failure paths, retry behavior, and maintenance vs non-maintenance 503 handling.
  • Updated event-sync remote data source tests to use a lightweight BackendApiClient + SimNetwork fake instead of wrapping results manually.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
infra/network/src/main/java/com/simprints/infra/network/apiclient/SimApiClientImpl.kt Ensures unsuccessful Response<*> results throw, enabling consistent exception mapping + retry logic.
infra/network/src/test/java/com/simprints/infra/network/apiclient/SimApiClientImplTest.kt Adds coverage for Response<ResponseBody> and retry/maintenance behavior.
infra/network/src/test/java/com/simprints/infra/network/FakeRetrofitInterface.kt Adds a Retrofit endpoint returning Response<ResponseBody> for testing.
infra/backend-api/src/main/java/com/simprints/infra/backendapi/BackendApiClient.kt Adds an extra response-unwrapping layer and changes constructor visibility.
infra/backend-api/src/test/java/com/simprints/infra/backendapi/BackendApiClientTest.kt Adds tests for Response<ResponseBody> success/failure wrapping (currently with a type-mismatch issue).
infra/event-sync/src/test/java/com/simprints/infra/eventsync/event/remote/EventRemoteDataSourceTest.kt Refactors tests to use a simple BackendApiClient fake path and adds failure-response assertions.
infra/event-sync/src/main/java/com/simprints/infra/eventsync/event/remote/EventRemoteDataSource.kt Minor formatting-only change.
Comments suppressed due to low confidence (1)

infra/backend-api/src/main/java/com/simprints/infra/backendapi/BackendApiClient.kt:26

  • Changing BackendApiClient from @Inject internal constructor to a public @Inject constructor expands the module’s public API surface. Most infra-layer injected classes keep their constructors internal to discourage direct instantiation outside DI (e.g. infra/event-sync/.../DownSyncSubjectUseCase.kt:19, infra/images/.../ImageRepositoryImpl.kt:16). Consider keeping the constructor internal and adjusting tests to mock/fake at the SimNetwork/SimApiClient level (or provide a dedicated test helper) rather than making construction public.
@Singleton
class BackendApiClient @Inject constructor(
    private val simNetwork: SimNetwork,
    private val authStore: AuthStore,
    @param:DeviceID private val deviceId: String,
    @param:PackageVersionName private val versionName: String,
) {

@luhmirin-s luhmirin-s force-pushed the hotfix/MS-1382-reponse-handling-main branch from 49336ab to d8b7a55 Compare March 18, 2026 11:31
@sonarqubecloud
Copy link

@BurningAXE
Copy link
Contributor

Am I correct in my understanding that we shouldn't introduce another type similar to Response or we'll step on the same rake again?

@luhmirin-s
Copy link
Contributor Author

@BurningAXE not exactly. The Response<ResponseBody> is the only option if you need to do anything more than just parse the request body to a specific type, so it has been added for very specific reasons and there is a chance that we will have to use it in the future. This is why I added such an aggressive handling for it.

@luhmirin-s luhmirin-s merged commit fa47ab8 into main Mar 18, 2026
14 checks passed
@luhmirin-s luhmirin-s deleted the hotfix/MS-1382-reponse-handling-main branch March 18, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants