Repository: ckan/ckan

Issue:
Undocumented changes breaking error_document_template
It seems that #4225 changed how the HTTP error code is passed to `error_document_template.html`.

In CKAN 2.7.3, `error_document_template.html` [says](https://github.com/ckan/ckan/blob/ckan-2.7.3/ckan/templates/error_document_template.html#L3):

    {% block subtitle %}{{ gettext('Error %(error_code)s', error_code=c.code[0]) }}{% endblock %}

In CKAN 2.8.0, however, the (BS2-) template [says](https://github.com/ckan/ckan/blob/ckan-2.8.0/ckan/templates-bs2/error_document_template.html#L3):

    {% block subtitle %}{{ gettext('Error %(error_code)s', error_code=code[0]) }}{% endblock %}

Note `c.code[0]` vs. `code[0]`.

Hence, custom variants of `error_document_template.html` need to be updated to work under CKAN 2.8.0. However, that change is not mentioned in the [change log](http://docs.ckan.org/en/2.8/changelog.html).


Discussion:
You are right @torfsen, we should have kept the `c.code` version deprecated for a version before removing it. We are gradually removing stuff from `c` in favour of passing vars explicitly to templates, but we should keep the values in `c` for at least a version.

@tino097 can you set `c.code` in the Flask error handler (or `g.code` rather) but don't use it anywhere. 
I like @torfsen 's suggestion of adding the change to the changelog.

For this issue we could support old copied and modified templates as @amercader says, and try to remember to remove the dead code later. For general template changes I don't want to be held to the same deprecation process that we have for things like plugin interfaces. When we get around to reducing the template `extends` and `snippet` nesting we can't sensibly support every customized theme at the same time.
@wardi Just to be clear, are you happy to keep support for all `c.*` vars for a version and remove them later or just this particular one on the error template? We've followed this approach so far for the admin, user and home controllers (merged) and for the group and package ones (almost merged) ([example](https://github.com/ckan/ckan/pull/4062/files#diff-2c4f7feb385fad48e72f4de013976946R1019). My plan was to list all of the deprecated `c` vars in the changelog or dedicated docs page, we just missed the one mentioned on this issue. The fix in the extension templates is quite straight-forward (`c.var -> var`) but on templates like the package ones there is a ton of them.

So in general, leaving this particular var on the error template aside when dropping support for `c` vars do you want to:

1. Keep them in `c` in master/2.9 + Announce on 2.9 Changelog + Drop them from `c` in 2.10
2. Drop them from `c` in master/2.9 + Announce on 2.9 Changelog that they have been removed

I think 1 at least will give a bit more time to people to update their extensions, and tackle the unsolved issue of how to support different CKAN versions with the same templates. But maybe I'm being too conservative and given our release schedule we can go for 2.

(Some previous discussion on https://github.com/ckan/ckan/pull/3203 and more recently in #4062)
Should have kept my mouth shut, I'm thinking about not tying our hands for larger unrelated template changes. @amercader's suggested fix is good for this issue, and I'm happy with either approach 1 or 2 above.
For what it's worth, as a user of and developer of custom templates I'm perfectly fine with having to update my templates when upgrading to a new CKAN minor version (as long as the changes are small, as in this case -- the switch to Bootstrap 3 is a bigger task, so the provision of the legacy BS2-templates is much appreciated). Hence, mentioning the change in the change log would have been enough for me.
> Should have kept my mouth shut

Please don't! Yours and @torfsen is really valuable feedback! We'll stick to 1 for now but we can revisit and go for 2 when we finish the migration of the controllers.

Resolving patch:
diff --git a/ckan/config/middleware/flask_app.py b/ckan/config/middleware/flask_app.py
--- a/ckan/config/middleware/flask_app.py
+++ b/ckan/config/middleware/flask_app.py
@@ -415,6 +415,9 @@ def _register_error_handler(app):
     def error_handler(e):
         if isinstance(e, HTTPException):
             extra_vars = {u'code': [e.code], u'content': e.description}
+            # TODO: Remove
+            g.code = [e.code]
+
             return base.render(
                 u'error_document_template.html', extra_vars), e.code
         extra_vars = {u'code': [500], u'content': u'Internal server error'}


Test patch: