misc: return an explicit error when database is down (#6567) #979
|
@ -1,5 +1,7 @@
|
|||
import os
|
||||
from unittest import mock
|
||||
|
||||
import psycopg2
|
||||
import pytest
|
||||
from quixote import get_request
|
||||
|
||||
|
@ -238,3 +240,9 @@ def test_invalid_site_options(pub):
|
|||
fd.write('xxx')
|
||||
with pytest.raises(Exception):
|
||||
get_app(pub).get('/', status=500)
|
||||
|
||||
|
||||
def test_postgresql_down(pub):
|
||||
with mock.patch('psycopg2.connect', side_effect=psycopg2.OperationalError()):
|
||||
resp = get_app(pub).get('/', status=503)
|
||||
assert 'Error connecting to database' in resp.text
|
||||
|
|
|
@ -20,6 +20,7 @@ import threading
|
|||
import time
|
||||
import urllib.parse
|
||||
|
||||
import psycopg2
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
|
||||
from django.template.response import TemplateResponse
|
||||
|
@ -45,6 +46,8 @@ class PublisherInitialisationMiddleware(MiddlewareMixin):
|
|||
pub.init_publish(compat_request)
|
||||
except ImmediateRedirectException as e:
|
||||
return HttpResponseRedirect(e.location)
|
||||
except psycopg2.OperationalError:
|
||||
return HttpResponse('Error connecting to database', content_type='text/plain', status=503)
|
||||
|
||||
if not pub.has_postgresql_config():
|
||||
return HttpResponse('Missing database configuration', content_type='text/plain', status=503)
|
||||
|
|
Loading…
Reference in New Issue