debug: raise error on 401
authorAzul <azul@riseup.net>
Fri, 2 Dec 2016 11:24:13 +0000 (12:24 +0100)
committerAzul <azul@riseup.net>
Fri, 2 Dec 2016 11:24:13 +0000 (12:24 +0100)
This will get us more debug info in the logs if it happens again

lib/nickserver/couch_db/source.rb
test/unit/couch_db/source_unit_test.rb

index 7c3ad95..dd29c2a 100644 (file)
@@ -6,17 +6,27 @@ require 'nickserver/couch_db/response'
 require 'nickserver/config'
 
 module Nickserver::CouchDB
+  class Error < StandardError; end
+
   class Source < Nickserver::Source
 
     VIEW = '/_design/Identity/_view/pgp_key_by_email'
+    UNEXPECTED_RESPONSE_CODES = [401, 500]
 
     def query(nick)
       status, body = adapter.get url, query: query_for(nick)
+      handle_unexpected_responses(status, body)
       Response.new(nick, status: status, body: body)
     end
 
     protected
 
+    def handle_unexpected_responses(status, body)
+      if UNEXPECTED_RESPONSE_CODES.include? status
+        raise Error.new("Couch responded with #{status}: #{body}")
+      end
+    end
+
     def url
       Nickserver::Config.couch_url + VIEW
     end
index fd07808..e5a4f43 100644 (file)
@@ -12,5 +12,16 @@ module Nickserver::CouchDB
       Source.new(adapter).query address
       adapter.verify
     end
+
+    def test_401
+      address = "nick@domain.tl"
+      adapter = Minitest::Mock.new
+      adapter.expect :get, [401, nil],
+        [String,  {query: { reduce: "false", key: "\"#{address}\"" }}]
+      assert_raises Error do
+        Source.new(adapter).query address
+      end
+      adapter.verify
+    end
   end
 end