Skip to content

Commit

Permalink
SLCORE-1145 Notify client only on 401 response from server (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource authored Feb 3, 2025
1 parent 5296d38 commit 022c8bb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion API_CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* Add new method `org.sonarsource.sonarlint.core.rpc.protocol.SonarLintRpcClient#invalidToken` to notify client that WebAPI calls to SQS/SQC fails due to wrong token
* Client can implement this method to offer user to change credentials for the connection to fix the problem
* For now notification is being sent only for 403 Forbidden HTTP response code since it's corresponds to malformed/wrong token and ignores 401 Unauthorized response code since it's a user permissions problem that has to be addressed on the server
* For now notification is being sent only for 401 Unauthorized HTTP response code since it's corresponds to malformed/wrong token and ignores 403 Forbidden response code since it's a user permissions problem that has to be addressed on the server
* Also once notification sent, backend doesn't attempt to send any requests to server anymore until credentials changed

* Removed `org.sonarsource.sonarlint.core.rpc.protocol.SonarLintRpcClient#didRaiseIssue` and associated types. See `raiseIssues` and `raiseHotspots` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.sonarsource.sonarlint.core.rpc.protocol.SonarLintRpcClient;
import org.sonarsource.sonarlint.core.rpc.protocol.client.sync.InvalidTokenParams;
import org.sonarsource.sonarlint.core.serverapi.ServerApi;
import org.sonarsource.sonarlint.core.serverapi.exception.ForbiddenException;
import org.sonarsource.sonarlint.core.serverapi.exception.UnauthorizedException;

public class ServerConnection {

Expand Down Expand Up @@ -59,7 +59,7 @@ public <T> T withClientApiAndReturn(Function<ServerApi, T> serverApiConsumer) {
state = ConnectionState.ACTIVE;
lastNotificationTime = null;
return result;
} catch (ForbiddenException e) {
} catch (UnauthorizedException e) {
state = ConnectionState.INVALID_CREDENTIALS;
notifyClientAboutWrongTokenIfNeeded();
}
Expand All @@ -71,7 +71,7 @@ public void withClientApi(Consumer<ServerApi> serverApiConsumer) {
serverApiConsumer.accept(serverApi);
state = ConnectionState.ACTIVE;
lastNotificationTime = null;
} catch (ForbiddenException e) {
} catch (UnauthorizedException e) {
state = ConnectionState.INVALID_CREDENTIALS;
notifyClientAboutWrongTokenIfNeeded();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.sonarsource.sonarlint.core.rpc.protocol.SonarLintRpcClient;
import org.sonarsource.sonarlint.core.serverapi.EndpointParams;
import org.sonarsource.sonarlint.core.serverapi.ServerApi;
import org.sonarsource.sonarlint.core.serverapi.exception.ForbiddenException;
import org.sonarsource.sonarlint.core.serverapi.exception.UnauthorizedException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
Expand Down Expand Up @@ -152,7 +152,7 @@ void should_log_invalid_connection() {

// switch connection to invalid state
spy.withValidConnection(connectionId, api -> {
throw new ForbiddenException("401");
throw new UnauthorizedException("401");
});
// attempt to get connection
spy.withValidConnection(connectionId, api -> {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void it_should_sync_when_credentials_are_updated(SonarLintTestHarness harness) {

@SonarLintTest
void it_should_notify_client_if_invalid_token() {
var status = 403;
var status = 401;
var client = newFakeClient()
.withCredentials(CONNECTION_ID, "user", "pw")
.build();
Expand Down

0 comments on commit 022c8bb

Please sign in to comment.