Repository: mdn/kuma

Issue:
JSONDecodeError in ssr.py
Simply opening https://beta.developer.mozilla.org/ja/docs/Web/API/Window causes the 500 ISE. 
https://sentry.prod.mozaws.net/operations/mdn-prod/issues/6224701/?environment=oregon%3Aprod

```
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  File "django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "newrelic/hooks/framework_django.py", line 544, in wrapper
    return wrapped(*args, **kwargs)
  File "kuma/core/decorators.py", line 37, in _cache_controlled
    response = viewfunc(request, *args, **kw)
  File "django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "kuma/wiki/decorators.py", line 31, in _added_header
    response = func(request, *args, **kwargs)
  File "kuma/wiki/decorators.py", line 105, in process
    return func(request, *args, **kwargs)
  File "newrelic/api/function_trace.py", line 139, in literal_wrapper
    return wrapped(*args, **kwargs)
  File "ratelimit/decorators.py", line 30, in _wrapped
    return fn(*args, **kw)
  File "kuma/wiki/views/document.py", line 617, in document
    return react_document(request, document_slug, document_locale)
  File "kuma/wiki/views/document.py", line 873, in react_document
    response = render(request, 'wiki/react_document.html', context)
  File "django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "django_jinja/backend.py", line 106, in render
    return mark_safe(self.template.render(context))
  File "newrelic/api/function_trace.py", line 121, in dynamic_wrapper
    return wrapped(*args, **kwargs)
  File "jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/app/kuma/wiki/jinja2/wiki/react_document.html", line 120, in top-level template code
    document_data)|safe }}
  File "kuma/wiki/templatetags/ssr.py", line 50, in render_react
    return server_side_render(component_name, data)
  File "kuma/wiki/templatetags/ssr.py", line 133, in server_side_render
    result = response.json()
  File "requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "simplejson/__init__.py", line 516, in loads
    return _default_decoder.decode(s)
  File "simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
```

Seems what's coming back from the SSR Node service isn't a JSON response but in Python we're expecting it to. 


Discussion:
One reason this can happen is if the response from the SSR service is anything but 200. It's wrong to assume it's always 200 and that the content-type is `application/json`. 
Aha! The `response` right before it does `response.json()` in `ssr.py` is a 413.
<img width="973" alt="Screen Shot 2019-09-09 at 2 12 56 PM" src="https://user-images.githubusercontent.com/26739/64555743-fc269a00-d30b-11e9-852d-c7169f1f7d53.png">

The only reason the `ssr-server.js` Express server would yield a 413 is if the body payload is larger than ...
```javascript
const MAX_BODY_SIZE = 1024 * 1024; // 1 megabyte max JSON payload
```

Perhaps, since this is Japanese Unicode, and a really large page the data posted is over 1MB. 
data=json.dumps(data).encode('utf8'),
I can open http://beta.mdn.localhost:8000/ja/docs/Web/API/Window on my local version. the JSON body, as a string is 630KB. And it looks very similar to https://developer.mozilla.org/ja/docs/Web/API/Window

Resolving patch:
diff --git a/kuma/wiki/templatetags/ssr.py b/kuma/wiki/templatetags/ssr.py
--- a/kuma/wiki/templatetags/ssr.py
+++ b/kuma/wiki/templatetags/ssr.py
@@ -130,6 +130,7 @@ def server_side_render(component_name, data):
         #     data['documentData'].update(bodyHTML='',
         #                                 tocHTML='',
         #                                 quickLinksHTML='')
+        response.raise_for_status()
         result = response.json()
         return _render(component_name, result['html'], result['script'])
 


Test patch: