Revision 863193cb astakos/im/api.py

b/astakos/im/api.py
37 37
from django.http import HttpResponse
38 38
from django.utils import simplejson as json
39 39

  
40
from astakos.im.faults import BadRequest, Unauthorized, ServiceUnavailable
40
from astakos.im.faults import BadRequest, Unauthorized, InternalServerError
41 41
from astakos.im.models import AstakosUser
42 42

  
43 43
def render_fault(request, fault):
44
    if settings.DEBUG or settings.TEST:
44
    if isinstance(fault, InternalServerError) and (settings.DEBUG or settings.TEST):
45 45
        fault.details = format_exc(fault)
46 46
    
47 47
    request.serialization = 'text'
48
    data = '\n'.join((fault.message, fault.details)) + '\n'
48
    data = fault.message + '\n'
49
    if fault.details:
50
        data += '\n' + fault.details
49 51
    response = HttpResponse(data, status=fault.code)
50
    return response
51

  
52
def update_response_headers(response):
53
    response['Content-Type'] = 'application/json; charset=UTF-8'
54 52
    response['Content-Length'] = len(response.content)
53
    return response
55 54

  
56 55
def authenticate(request):
57 56
    # Normal Response Codes: 204
58
    # Error Response Codes: serviceUnavailable (503)
57
    # Error Response Codes: internalServerError (500)
59 58
    #                       badRequest (400)
60 59
    #                       unauthorised (401)
61 60
    try:
......
86 85
                     'auth_token_created':user.auth_token_created.isoformat(),
87 86
                     'auth_token_expires':user.auth_token_expires.isoformat()}
88 87
        response.content = json.dumps(user_info)
89
        update_response_headers(response)
88
        response['Content-Type'] = 'application/json; charset=UTF-8'
89
        response['Content-Length'] = len(response.content)
90 90
        return response
91 91
    except BaseException, e:
92
        fault = ServiceUnavailable('Unexpected error')
92
        fault = InternalServerError('Unexpected error')
93 93
        return render_fault(request, fault)

Also available in: Unified diff