diff options
author | Stan Hu <stanhu@gmail.com> | 2018-07-06 00:09:01 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-07-06 00:09:01 +0300 |
commit | a78e36abab6d30d2fc7571ab095a2c08bd52dd24 (patch) | |
tree | a38419b13447d2d5af0eef27350bdfd4745ba46d /lib/bitbucket_server | |
parent | c7198166e8790a1335de0a08bd38080873806710 (diff) |
Improve error handling of Bitbucket login errors
Diffstat (limited to 'lib/bitbucket_server')
-rw-r--r-- | lib/bitbucket_server/connection.rb | 23 | ||||
-rw-r--r-- | lib/bitbucket_server/representation/repo.rb | 4 |
2 files changed, 25 insertions, 2 deletions
diff --git a/lib/bitbucket_server/connection.rb b/lib/bitbucket_server/connection.rb index 21807c8a229..092c878c93b 100644 --- a/lib/bitbucket_server/connection.rb +++ b/lib/bitbucket_server/connection.rb @@ -1,5 +1,7 @@ module BitbucketServer class Connection + include ActionView::Helpers::SanitizeHelper + DEFAULT_API_VERSION = '1.0'.freeze attr_reader :api_version, :base_uri, :username, :token @@ -15,19 +17,36 @@ module BitbucketServer response = Gitlab::HTTP.get(build_url(path), basic_auth: auth, params: extra_query) - ## Handle failure + + check_errors!(response) response.parsed_response end def post(path, body) - Gitlab::HTTP.post(build_url(path), + response = Gitlab::HTTP.post(build_url(path), basic_auth: auth, headers: post_headers, body: body) + + check_errors!(response) + response end private + def check_errors!(response) + if response.code != 200 + error = + if response.parsed_response + sanitize(response.parsed_response.dig('errors', 0, 'message')) + end + + message = "Error #{response.code}" + message += ": #{error}" if error + raise ::BitbucketServer::Error::Unauthorized, message + end + end + def auth @auth ||= { username: username, password: token } end diff --git a/lib/bitbucket_server/representation/repo.rb b/lib/bitbucket_server/representation/repo.rb index 52dbe040a60..cf64f0ac8fa 100644 --- a/lib/bitbucket_server/representation/repo.rb +++ b/lib/bitbucket_server/representation/repo.rb @@ -5,6 +5,10 @@ module BitbucketServer super(raw) end + def project_name + raw.dig('project', 'name') + end + def owner project['name'] end |