From 6d8370a89f469e1063ea8c2b72408b6c0d4d7b19 Mon Sep 17 00:00:00 2001 From: Ivan Munic Date: Wed, 30 Nov 2016 23:52:28 +0100 Subject: [PATCH] Add exception handling --- .../sonar/client/BitbucketClient.scala | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/scala/ch/mibex/bitbucket/sonar/client/BitbucketClient.scala b/src/main/scala/ch/mibex/bitbucket/sonar/client/BitbucketClient.scala index 1b99e8b..2de24e9 100644 --- a/src/main/scala/ch/mibex/bitbucket/sonar/client/BitbucketClient.scala +++ b/src/main/scala/ch/mibex/bitbucket/sonar/client/BitbucketClient.scala @@ -1,11 +1,10 @@ package ch.mibex.bitbucket.sonar.client -import java.net.{HttpURLConnection, InetSocketAddress, URL} +import java.net.{HttpURLConnection, InetSocketAddress, Proxy, URL} import javax.ws.rs.core.MediaType -import ch.mibex.bitbucket.sonar.{SonarBBPlugin, SonarBBPluginConfig} import ch.mibex.bitbucket.sonar.utils.{JsonUtils, LogUtils} -import java.net.Proxy +import ch.mibex.bitbucket.sonar.{SonarBBPlugin, SonarBBPluginConfig} import com.sun.jersey.api.client.config.{ClientConfig, DefaultClientConfig} import com.sun.jersey.api.client.filter.LoggingFilter import com.sun.jersey.api.client.{Client, ClientResponse, UniformInterfaceException} @@ -257,12 +256,21 @@ class BitbucketClient(config: SonarBBPluginConfig) extends BatchComponent { } private def getLoggedInUserUUID(): String = { - val response = client.resource(s"https://bitbucket.org/api/2.0/user") - .accept(MediaType.APPLICATION_JSON) - .get(classOf[String]) + if (!config.isEnabled) { + return null + } - val user = JsonUtils.mapFromJson(response) - user("uuid").asInstanceOf[String] + try { + val response = client.resource(s"https://bitbucket.org/api/2.0/user") + .accept(MediaType.APPLICATION_JSON) + .get(classOf[String]) + + val user = JsonUtils.mapFromJson(response) + user("uuid").asInstanceOf[String] + } catch { + case e: UniformInterfaceException => + throw new IllegalStateException(s"${SonarBBPlugin.PluginLogPrefix} Couldn't fetch logged in user uuid, got status: ${e.getResponse.getStatus}") + } } }