# Buggy code
def _raise_304_if_not_modified(self, record=None):
    """Raise 304 if current timestamp is inferior to the one specified
    in headers.

    :raises: :exc:`~pyramid:pyramid.httpexceptions.HTTPNotModified`
    """
    if_none_match = self.request.headers.get("If-None-Match")

    if not if_none_match:
        return

    if_none_match = decode_header(if_none_match)

    try:
        if not (if_none_match[0] == if_none_match[-1] == '"'):
            raise ValueError()
        modified_since = int(if_none_match[1:-1])
    except (IndexError, ValueError):
        if if_none_match == "*":
            return
        error_details = {
            "location": "header",
            "description": "Invalid value for If-None-Match",
        }
        raise_invalid(self.request, **error_details)

    if record:
        current_timestamp = record[self.model.modified_field]
    else:
        current_timestamp = self.model.timestamp()

    if current_timestamp <= modified_since:
        response = HTTPNotModified()
        self._add_timestamp_header(response, timestamp=current_timestamp)
        raise response


# Traceback (UnicodeDecodeError)
Traceback (most recent call last):
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/tweens.py\", line 22, in excview_tween
response = handler(request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid_tm/__init__.py\", line 119, in tm_tween
reraise(*exc_info)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid_tm/__init__.py\", line 98, in tm_tween
response = handler(request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/router.py\", line 158, in handle_request
view_name
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/view.py\", line 547, in _call_view
response = view_callable(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/config/views.py\", line 182, in __call__
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 393, in attr_view
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 371, in predicate_wrapper
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 302, in _secured_view
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 442, in rendered_view
result = view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 147, in _requestonly_view
response = view(request)
File \"/data/kinto-dist/lib/python2.7/site-packages/cornice/service.py\", line 571, in wrapper
response = view_()
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/views/records.py\", line 78, in collection_get
result = super(Record, self).collection_get()
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/core/resource/__init__.py\", line 249, in collection_get
self._raise_304_if_not_modified()
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/core/resource/__init__.py\", line 744, in _raise_304_if_not_modified
if_none_match = decode_header(if_none_match)
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/core/utils.py\", line 394, in decode_header
value = value.decode(encoding)
File \"/data/kinto-dist/lib64/python2.7/encodings/utf_8.py\", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: \'utf8\' codec can\'t decode byte 0xea in position 15: invalid continuation

# Fixed code
def _raise_304_if_not_modified(self, record=None):
    """Raise 304 if current timestamp is inferior to the one specified
    in headers.

    :raises: :exc:`~pyramid:pyramid.httpexceptions.HTTPNotModified`
    """
    if_none_match = self.request.headers.get("If-None-Match")

    if not if_none_match:
        return

    error_details = {
        "location": "header",
        "description": "Invalid value for If-None-Match",
    }

    try:
        if_none_match = decode_header(if_none_match)
    except UnicodeDecodeError:
        raise_invalid(self.request, **error_details)

    try:
        if not (if_none_match[0] == if_none_match[-1] == '"'):
            raise ValueError()
        modified_since = int(if_none_match[1:-1])
    except (IndexError, ValueError):
        if if_none_match == "*":
            return
        raise_invalid(self.request, **error_details)

    if record:
        current_timestamp = record[self.model.modified_field]
    else:
        current_timestamp = self.model.timestamp()

    if current_timestamp <= modified_since:
        response = HTTPNotModified()
        self._add_timestamp_header(response, timestamp=current_timestamp)
        raise response